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 modern C++.
  • Use smart pointers instead of pointers.
  • using namespace x is not allowed at any level in the code as it leaks into other files and easily makes the code ambiguous.

Naming

  • Class names start with a upper case letter and then proceeds with camel case.
  • Variable names, function names and parameters all start with a lower case letter and then proceeds with camel case.
  • Constants are written in all upper case letters with _ separation words.
class Example {
public:
    Example();
    virtual ~Example() = default;

    int setFavoriteRoyality(std::string newFavoriteRoyality);

    std::string favoriteRoyality = KNUGEN;

    const std::string KNUGEN = "Carl XVI Gustaf";
}

Declaration

  • Each class should have a separate .h and .cpp file. Declaring multiple classes in the same header file is not allowed.
  • Declation order per scope: Constructor, destructor, functions, variables, constants. See the

Header files

If the .h file should be accessible from outside of Bubba3D, for instance from BubbaRogueFort, then the header file should be placed in the includes directory. If this is not the case, the .h file should be located in the same directory as the .cpp file that define it. In the case of the .h file representing an abstract interface then the .h file should be located in the same directory as the .h files that extends it.

Note: Header files are strictly used for declarations. Besides constants and default values, no definitions are allowed in the header files.

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