wiki:CodingRules

Version 8 (modified by tbretz, 11 years ago) ( diff )

--

Coding rules might sound overdone, but they are essential to keep a code clean and tidy and even more important consistent. At the end this will help everybody to read code of somebody else. These rules include a lot of experience and are adapted to modern screen (which have more than 25 lines). The order is random and has not yet any meaning.

  1. In general lines should not be longer than 80 characters.
  2. Use const qualifiers where ever possible. This includes functions, function arguments and every variable declaration. This has several advantages: Improved checking by the compiler, better optimization by the compiler, and faster understanding (You are only interested in a loop at the end of a function and an important variable is declared at the beginning, you would have to check all code in between carefully to know its value, and even worse, ever function call which might take the non-const variable as a reference and alter it).
  3. Declare variables as late as possible. To understand code, it makes sense to have all you need as close as possible which avoids scrolling forth and back.
  4. The indentation is 4. No tabs allowed.
  5. Curly braces are *always* on a single empty line (this gives a clear structure to the code), although in *switch* statements.
  6. *goto* is a no-go, it breaks the structure of the code (check google for details)
  7. In switch statements the *case* label is on the same level as the *switch*. The corresponding code or brackets are indented.
  8. There is only one command per line. In if conditions the command to be executed is always on a new line, the *else* is always on it's own empty line.
  9. Nested if's like "if () else if () else if () else ..." are written like a staircase following the previous rules. Every other formatting hides the structure!
  10. Variables definitions in class headers are at the beginning of the class definition. Usually (not always reasonable) the order of modifiers is: private, protected, public. After the variables, put the functions, in the same order.
  11. Data members always start with an f (for field), static constant members with a g (for global) and non-const static members with fg (for global field). We use capitalization for variable names, e.g. fThisIsMyDataMember. class names and function names start with a capital. Strongly Mars related classes start with a capital M. Classes which can compile without Mars, might start with a different capital. Some exceptions are very very low level classes (e.g. zfits) which are written closer to the system coding style.
  12. File names should generally have the same name as the class name.
  13. Each header has to be encapsulated in an #ifndef MARS_ClassName, #define MARS_ClassName, [...code...], #endif block to avoid double inclusion.
  14. *using namespace* is never be used globally in a header.
  15. Try to use empty lines to separate blocks (e.g. everything which belongs to a loop plus the loop from the rest)
  16. Use '\n' to finish a line not *endl* except you want to flush a line to the console (e.g. to make sure it is available if after that a crash happens). For example: You output many lines successively, use '\n'. For the last one before your code returns use *endl*
  17. Do not use curly braces in if-statements to bracket a single line.
  18. Use a white space after the semicolon in a for-statement, but not before. Keep the three blocks without white-spaces. This simplifies to see what belongs to which part.
  19. In argument lists, use a white space after the comma, not before.
  20. If you use the ?:-operator, put white spaces around the ? and the colon. Try to keep the rest without white spaces.
  21. Avoid to assign variables just to give something a different name, use comments.
  22. Use white spaces around logical operators (&&, ..) in conditionals (==, <= are conditonal operators, not logical operators). This mimics the priority of the operators and makes the condition easier to understand. Try to keep the calculations compact. Avoid unnecessary parenthesis. If you are not sure, check operator priorities. This is a language issue nothing random from the compiler!
  23. Avoid using line long variable names. They should not be too short either. *timv* doesn't tel us much, but *myVar* tells us as much as *thisIsMyVariable* but makes code more compact thus better readble. Don't use variable names to explain their contents, use comments!
  24. Whenever possible propagate const-references instead of copying the argument. This is an optimization issue.
  25. Use gRandom and do not define your own TRandom object. The global scope will take care of that. A major advantage of simulations is to be able to set a global(!) seed.
  26. For proper documentation, please have a look at other classes. Note that FACT++ and Mars style is slightly different. Mars is using THtml with some Mars specific setup, FACT++ is using doxygen.
  27. Never define constants as preprocessor directives. Generally avoid preprocessor directives.
  28. omit else in constructions like *if (...) return x; [else] ....*
  29. Source files have the extension cc, header files the extension h, C source files just c.
  30. Operators +=, -=, etc. are preferable over constructions like x=x+, x=x-, ...
  31. If you omit intentionally a break in a switch statement (*fallthru*) add a comment
  32. Sometimes including system headers is not identical for all operating systems, comments might help
  33. Try to avoid using dynamic allocation whenever possible: "int i=5; /* do something with i*/; /* i runs out of scope */" is preferred over "int *i = new int; *i=5; /* do something with i */; delete i;" Although this example sounds ridiculous, the same is true for every class instance you use.

Example

#ifndef MARS_Complex
#define MARS_Complex

#include <math.h> // needed for sqrt on Ubuntu 10.14

class Complex
{
private:
    double fRe;
    double fIm;

public:
    Complex(double re, double im)
        : fRe(re), fIm(im)
    {
    }
    
    double Modulus() const
    {
        return sqrt(fRE * fRe + fIm * fIm);
    }

    bool Foo(int a, int b)
    {
        for (int i=0; i<b-a; i++)
        {
            if (i<b)
                bar(i);
            else
            {
                bar(i);
                bar(b);
            }
        }

        const int c = a + b;
        switch (c)
        { 
        case 0:
           // Code goes here
           return false;
        case 1:
        case 2:
           // Code goes here
           return false;
        default:
           // Code goes here
           return true;
        }

        return true;
     }
};

#endif 
Note: See TracWiki for help on using the wiki.