BOREALIS CODING STANDARDS
Nesime Tatbul, Don Carney, Bradley Berg
June 27, 2005
This document contains basic C and C++ coding standards for the Borealis project with the intension of having an easily-readable and homogeneous-looking Borealis code-base. Borealis coding standards mostly inherit from Aurora coding standards. Hence, this document builds upon an earlier document prepared for Aurora by Don Carney [1].
In general, use one file per class. A class file may contain related trivial classes. To avoid very large files it is reasonable to move some class methods to a separate file.
The file name should be the same as the class name (mixed case). The extensions are .h for C and C++ header files, .cc for C++ files, and .c for C files.
Declarations widely used in Borealis source code are declared in common.h. These include namespace definitions. We use a common namespace for Borealis. Your code should be enclosed between:
BOREALIS_NAMESPACE_BEGIN : BOREALIS_NAMESPACE_END
The MEDUSA namespace was used in code inherited from the Medusa project. Over time legacy code should be converted to the BOREALIS namespace. Use the BOREALIS namespace in any new project code.
The NMSTL namespace is used within the threads library. NMSTL is the Networking, Messaging, Servers, and Threading Library for C++ written by Jon Salz [5]. It provides many nice features to facilitate developing code in C++, like class serializers, smart pointers, and macros for code debugging. As a general guideline, we should use this library, to the possible extent, for convenience in developing code in Borealis.
While it is good to use tabs while editing, replace tabs with spaces when you save a file. Set your tab spacing to 4 spaces. Use tabs for indenting and for lining up names in class declarations. The code should look spiffy. If not, your tab settings are wrong. To test whether your using the right tab spacing, you can run this command:
> pr -e4 -t file_name
Make sure that all tabs are replaced with spaces after you finish editing source files. Some editors automatically do this for you. Otherwise, you can use substitute commands. For example, if you are using a vi editor, you can simply do the following:
:%s/<TAB>/<4-spaces>/g
Try to limit source code lines to a width of 80 columns. It is highly recommended to use a C/C++ Beautifier, such as bcpp [4]. bcpp indents C/C++ source programs, replacing tabs with spaces or the reverse.
Put opening curly braces immediately below the first character of the previous line. Keep opening and closing braces aligned in the same columns. This makes it easy to visually match braces. All code within a pair of braces should be indented. Never code a block of code below a condition or loop construct (if, else, for, or do) without braces.
if (condition) neverDoThis(argument);
If the conditional statement takes up only one line you may put the following statement on the same line as the left brace. Otherwise the following line should only contain the brace. By convention we will no put spaces after sets of left parenthesis or before sets of right parenthesis. It also helps to put a blank lines before and after any block of code.
if (condition) { doThis(argument); : }
while ((compound condition) && (on multiple lines)) { doThis(); : }
for (int i = 0; i < 10; i++) { ... }
switch(condition) { ... }
Methods declarations should be enclosed in comment boxes. Unless there is a compelling reason, all variable declarations should be in the declaration section, not in the method body. For example, a variable may be declared in the body if a type has no default constructor (e.g. Status) or a declaration requires local scoping.
//////////////////////////////////////////////////////////////////////// // // A terse description of the method. // ResultType test_method(// Description of the parameter. ParameterType parameter ) { VariableType variable; // Description of the variable. ResultType result; // //...................................................................... some statements; return(result); }
#ifndef FILENAME_H #define FILENAME_H
#endif // FILENAME_H
We use Doxygen [2] to automatically generate API documentation from source code comments. Doxygen can generate documentation in HTML/RFT/LaTeX format from code comments that follow certain conventions. Comments used to generate documentation are confined to header files. We have adapted one of the comment formats supported by Doxygen referred to as the JavaDoc style [3]. That is, single-line comments are introduced using three slashes, "///", and comment blocks use:
/** * ... detailed description here ... */
Comments that are not part of the API documentation are intended for class developers. These comments shall use two slashes, "//", to introduce single-line comments and "/*" to introduce comment blocks or to comment out blocks of code. Note that this comment for should be used with private declarations as these do not get incorporated in the generated API documentation.
The quickest way to see how to comment header files is to look at the example file, SampleClass.h.
To run doxygen go to the borealis/src/doc/ directory in your sandbox and type:
borealis/src/doc> doxygen doxygen.conf
You can add CVS log message pages to the pages generated by doxygen by starting the ssh-agent and running make from the borealis/src/doc/ directory.
A copy of this document is available in the Borealis CVS repository. Please email bb@cs.brown.edu for any comments about this document. Happy coding!