Skip to content

Code style and structure

DunderRoffe edited this page Oct 9, 2016 · 10 revisions

Code style and structure

CMake

  • Use lower case letters on all functions.
  • Each CMakeLists.txt file should only manage files in the same directory and connections to subdirectories.

C++:

General

  • Keep all lines shorter than 100 characters.
  • Indentation is 4 spaces.
  • Use smart pointers instead of pointers.
  • using namespace x is not allowed at any level in the code. This leas to other files and easily makes the code ambiguous.

Declaration

  • Each class should have a separate .h and .cpp file. Declaring multiple classes in the same header file is not allowed.

Constructors

For every new line, align the first parameter with the first parameter on the line above. This also applies to member definitions, with difference that the : should be placed such that the first member definition aligns with first parameter on the line above.

SomeLongClassName::SomeLongClassName(int argument1, int argument2, float argument3,
                                     float argument4, float argument5)
                                   : member1(argument1), member2(argument2)
{
    member3 = (argument3 + argument4) * argument5;
}

Functions

When splitting the parameter listing over several lines:

  • Align the first parameter on each line with the first parameter of the previous line while.
  • Keep the , on the same line, that is do not prepend parameters on a new line with ,.
  • Put the function block start bracket on a new line to give some distance between the function code and parameters.
int SomeLongClassName::someFunction(int argument1, int argument2, float argument3,
                                    float argument4, float argument5)
{
    // Code goes here ...
}

If statements

  • Always use brackets
  • Return as soon as possible, use expressions to capture return cases:
int fun(int x) {
    if (x < 5) {
        return 5;
    }
    // Rest of the function code.
}

Binary operators

  • Always have a whitespace before and after each operator
  • While splitting a line using operators, keep the first argument on the same line and align the operator on the new line with the first argument.
int fun() {
    float rotationAngle = 4.3f;
    float rotationMagnitude = 10.0f;
    int answerToTheUltimateQuestionOfLifeTheUniverseAndEverything = 42;

    return someFunctionCall(rotationAngle * rotationMagnitude
                            * answerToTheUltimateQuestionOfLifeTheUniverseAndEverything);
}

Python

  • Follow PEP8
  • Keep all code python3 compatible, python 2 is approaching EOL.