CPP coding guidelines
From NEST
Files and names
- All header files have the extension ".h"
- All C++ files have the extension "cpp".
- The use of "cc" is deprecated and is only left for compatibility.
- Do not use macros to define literal constants. Use const or enum instead.
- All header files are protected by an #ifdef guard, See [1] sec 9.3.3, p 216. Example.
#ifndef MYHEADER_H #define MYHEADER_H // the code #endif
Indentation and whitespace
Detailed strategy with regards to indentation is still to be developed. Ideally it should be expressed in the form of switches for indent or Artistic Style. This would allow for auto-indenting on commit, which might not be a good idea. However, local pre-commit hooks might turn out to be very helpful nevertheless.
- Avoid adding new trailing whitespace; TWS is a bad thing (tm)
- Avoid mixtures of tabs and spaces; use spaces only
- When touching a file, it's a good idea to strip TWS and fix indentation, however,
- Avoid committing whitespace changes together with changes in logic
- When fixing indentation or whitespace problems, always commit these changes separately!
- When editing a file, try to stick to the local indentation style for it to remain readable
Namespaces
All symbols for the NEST kernel are declared in the namespace nest
Class names
Class names should use one of the following two schemes
- Capitalised with capitals as word separators: Example:
class MyClass - Lower case with underscores to separate words Example:
class my_class
Class members
Class members should be lower case with underscores to separate words.
Private/"undocumented" class members
Names of private members and undocumented interface functions should end in an underscore.
Example:
class MyClass
{
int get_i() const;
private:
int i_;
}
Inline functions
- Inlined functions should be placed outside the class declaration, using the keyword inline.
- Inline declarations should follow their class declaration.
- The keyword "inline" should appear on a separate line.
Example:
class MyClass
{
int get_i() const;
private:
int i_;
};
inline
int MyClass::get_i() const
{
return i;
}
Builtin Types
All code for the nest kernel should use the type aliases, defined in nest.h. Thus, use nest::float_t instead of float.
Templates
- Template class declarations follow the same style as normal class declarations. This applies in particular to inline declarations (see above).
- The keyword template followed by the list of template parameters appear on a separate line.
Example
template< typename T>
class MyClass: public T
{
// code
};
Language Features
- Use only ISO C++ language features.
- Prefer ISO C++ library functions over their ISO C library equivalents.
- Prefer ISO C++ library containers (STL).
- Prefer C++ headers over their C equivalents.
- Don't use
printfand related functions. - Use C++ style cast notation (see [1]).
- Use the
constqualifier where appropriate. Use it consistently (see [5], chapter 6)! - Use namespaces and exceptions.
- Try to avoid static class members which need a constructor (non POD).
Language of comments and identifiers
- All comments should be written in English.
- All identifiers, class and function names should be in English.
Debugging, Quality Control
Use the assert macro intensively to check program invariants. [9]
On-Line Reference Documents
- DinkumWare's Standard C++ Library Reference
- ISO C++ Press Release
Compiler
NEST compiles with any recent version of the GNU C/C++ Compiler gcc.
Books
We have found the following books to be useful.
- Stroustrup B (1997) The C++ Programming Language, 3rd Edition, Addison-Wesley
- Meyers S (1997) Effective C++, 2nd Edition, Addison Wesley
- Meyers S (1996) More Effective C++, Addison Wesley
- Coplien J O (1992) Advanced C++ programming styles and idioms, Addison-Wesley
- Eckle B (1995) Thinking in C++, Prentice Hall
- Plauger P J, Stepanov A, Lee M, and Musser D R (1998) The Standard Template Library, Comming June 1998, Prentice Hall
- Plauger P J (1995) The (draft) Standard C++ Library, Prentice Hall
- Musser D R and Saini A (1996) STL Tutorial and Reference Guide, Addison-Wesley
- Kernighan B and Ritchie D (1988) The C Programming Language, 2nd Edition, Prentice Hall
