Differences between revisions 3 and 4
Revision 3 as of 2010-11-15 18:28:18
Size: 2739
Editor: MalteHelmert
Comment: "+" is escaped by browser; looks ugly
Revision 4 as of 2010-11-15 18:28:27
Size: 2691
Editor: MalteHelmert
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
## page was renamed from ForDevelopers/C++Code

Back to developer page.

C++ coding conventions and style guide

This is not meant to be a complete description of our coding conventions. When in doubt, follow the example of the existing code.

See also the information on how and where to put ../Whitespace.

General recommendations

We generally follow the recommendations in the book C++ Coding Standards: 101 Rules, Guidelines, and Best Practices by Herb Sutter and Andrei Alexandrescu. In the tracker or elsewhere, a mark of the form [SA x] is a reference to a rule in that book. For example, [SA 9] refers to Sutter and Alexandrescu's rule 9: "Don’t pessimize prematurely".

Namespaces

  • Every subdirectory should correspond to a namespace.
  • Avoid nested namespaces or subdirectories of subdirectories.
  • Namespaces follow the same camel-case naming convention as classes, while directory names follow the naming conventions for methods and variables. For example, file my_subdir/my_class.h would be expected to contain a definition of class MySubdir::MyClass.

  • Namespaces should have short, single-word names if possible. (The previous example only uses an underscore in the subdirectory name for sake of illustration.)

Header file guards

Macro names for header file guards follow this algorithm:

  • Take the filename, including subdirectory name if in a subdirectory.
  • Convert to uppercase.
  • Replace all "." and "/" with "_".

Example: learning/state_space_sample.h becomes LEARNING_STATE_SPACE_SAMPLE_H.

Guard blocks should look like this:

   1 #ifndef LEARNING_STATE_SPACE_SAMPLE_H
   2 #define LEARNING_STATE_SPACE_SAMPLE_H
   3 // ...
   4 #endif
   5 

That's all. In particular, don't add comments to the preprocessor directives and don't add further underscores.

Function signatures

  • Use const methods whenever appropriate.

  • Pass strings by const reference.

  • When overriding a virtual method, mention virtual again in the declaration (i.e., virtual int foo(); rather than int foo();).

Anti-idioms

  • Don't write NULL for null pointers. Write 0. (The latter is slightly preferred in C++, unlike C. C++ 0x will change that, but we're not there yet. In any case, it's not good to mix styles.)

  • Don't write (ptr != 0). Write (ptr).

  • Don't write (ptr == 0). Write (!ptr).

  • Don't write (seq.size() == 0). Write (seq.empty()).

  • Don't write (seq.size() != 0). Write (!seq.empty()).

FastDownward: ForDevelopers/CodingConventions (last edited 2023-09-15 10:41:07 by RemoChristen)