xpires: Thu, 24 Apr 2036 11:47:38 GMT ETag: "30d412a918b66e202492170aed64d37da5e76124" \documentclass{article}[12pt] \usepackage{color} \begin{document} \hrule \begin{center}\textbf{\Large{GlusterFS Coding Standards}}\end{center} \begin{center}\textbf{\large{\textcolor{red}{Z} Research}}\end{center} \begin{center}{July 14, 2008}\end{center} \hrule \vspace{8ex} \section*{$\bullet$ Structure definitions should have a comment per member} Every member in a structure definition must have a comment about its purpose. The comment should be descriptive without being overly verbose. \vspace{2ex} \textsl{Bad}: \begin{verbatim} gf_lock_t lock; /* lock */ \end{verbatim} \textsl{Good}: \begin{verbatim} DBTYPE access_mode; /* access mode for accessing * the databases, can be * DB_HASH, DB_BTREE * (option access-mode ) */ \end{verbatim} \section*{$\bullet$ Declare all variables at the beginning of the function} All local variables in a function must be declared immediately after the opening brace. This makes it easy to keep track of memory that needs to be freed during exit. It also helps debugging, since gdb cannot handle variables declared inside loops or other such blocks. \section*{$\bullet$ Always initialize local variables} Every local variable should be initialized to a sensible default value at the point of its declaration. All pointers should be initialized to NULL, and all integers should be zero or (if it makes sense) an error value. \vspace{2ex} \textsl{Good}: \begin{verbatim} int ret = 0; char *databuf = NULL; int _fd = -1; \end{verbatim} \section*{$\bullet$ Initialization should always be done with a constant value} Never use a non-constant expression as the initialization value for a variable. \vspace{2ex} \textsl{Bad}: \begin{verbatim} pid_t pid = frame->root->pid; char *databuf = malloc (1024); \end{verbatim} \section*{$\bullet$ Validate all arguments to a function} All pointer arguments to a function must be checked for \texttt{NULL}. A macro named \texttt{VALIDATE} (in \texttt{common-utils.h}) takes one argument, and if it is \texttt{NULL}, writes a log message and jumps to a label called \texttt{err} after setting op\_ret and op\_errno appropriately. It is recommended to use this template. \vspace{2ex} \textsl{Good}: \begin{verbatim} VALIDATE(frame); VALIDATE(this); VALIDATE(inode); \end{verbatim} \section*{$\bullet$ Never rely on precedence of operators} Never write code that relies on the precedence of operators to execute correctly. Such code can be hard to read and someone else might not know the precedence of operators as accurately as you do. \vspace{2ex} \textsl{Bad}: \begin{verbatim} if (op_ret == -1 && errno != ENOENT) \end{verbatim} \textsl{Good}: \begin{verbatim} if ((op_ret == -1) && (errno != ENOENT)) \end{verbatim} \section*{$\bullet$ Use exactly matching types} Use a variable of the exact type declared in the manual to hold the return value of a function. Do not use an ``equivalent'' type. \vspace{2ex} \textsl{Bad}: \begin{verbatim} int len = strlen (path); \end{verbatim} \textsl{Good}: \begin{verbatim} size_t len = strlen (path); \end{verbatim} \section*{$\bullet$ Never write code such as \texttt{foo->bar->baz}; check every pointer} Do not write code that blindly follows a chain of pointer references. Any pointer in the chain may be \texttt{NULL} and thus cause a crash. Verify that each pointer is non-null before following it. \section