Software Development Methodology> Coding Standards > C++ coding guidelines

Release Information

Project: Siconos
Internal Release Number: 1.0
Last update: September 15, 2005
Related Documents:


next up previous

C++ coding guidelines

  • Header files
    • Preprocessing:
       Each header file should be wrapped with multiple inclusion protection macros (#IFNDEF ... #DEFINE ... #ENDIF).
      Use #if ...  instead of C/C++ comments for code switching.
      Group #include statements and sort them starting from lower to upper level (example: from ``myFile.h'' to <vector> STL).
    • Remove any useless include
    • Use inline functions only for short functions (no more than one instruction).
    • Implement functions in the same order as they are declared
    • Start class with private members and functions. Goes on with public ones, in the following order: constructors, destructor, getters-setters, other functions.
  Header file template

  • Constants:
    use rather:
    const double PI = 3.14;
    than pre-processor macro: 
    #define PI 3.14
  • Remove this-> (calling member members or methods) as much as possible.

  • Control flow
    • Do not change any loop variable inside a loop.
    • All switch statements should have a default clause.
    • All case clauses of a switch statement must be followed by a block to create a local scope with break at the end.
  • Functions and Methods
    • Use pass by const reference by default.
      Example: get(const double& value)
    • Use const keyword for class functions when the function do not change class members
    • Use const for return argument when its possible.
    • To sum-it up: use const keyword whenever itīs possible!
    • Do not use numeric or string literals inside any function or method implementation unless these values are part of an algorithm.
    • Use using instead of using namespace to import a definition into current context except for some very small scope.
  • Interface
    • No global variables are allowed unless required by some application framework.
    • Initialize variables in the same order as they are declared.
    • No data item should be public. Use inline method to define a property instead.
    • An overloaded assignment operator should work properly when the right operand is the same object as the left operand.
    • Avoid multiple inheritance except for implementing multiple interfaces.
  • Resource Management
    • Use constructors and destructors to manage resources.
    • Set explicitly pointers to NULL after declaration or delete.
  • Conversion and Cast: use static_cast or dynamic_cast operator rather than C-style casts.

  • Error Handling
    • Declare exception specification if all possible exceptions are known.
    • Validation of inputs should not be handled with exceptions.
    • Exceptions should be thrown only when timing cannot be predicted in the static linking stage and when the code unit throwing an exception does not know how to handle it.
    • Organize different types of exceptions into an exception tree based on the STL exception hierarchy.

  • Design by contract The use of nana library to check functions input parameters is advised.

    Basic macros are :
    • IN(message) : displays a message on standard output when the program enters the function.
    • OUT(message) : displays a message on standard output when the program exits the function.
    • ASSERT(expr) : tests if boolean expression expr is true. If expr is false, displays a message on standard output.
  • Formatting
    • Always put braces alone on a line.
    • A newline is required at the end of a file.
    • Leave no space between & or * and the variable name
       Example: SimpleVector *x 
    • Use blank lines to separate logical groups within functions and methods.
    • Put spaces on each sides of an operator.
    • ...
C++ references:
     -Thinking in C++ web site : http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html
     - C++ Coding Standard, Yongling Ding, Ph.D, CFA, FRM.

  nextup previous