3 Coding_style
Moritz Strohm edited this page 9 months ago

MoeNavigatorEngine coding style

NOTE: This coding style guide is newer than the source code so that you may find some places in the code which violate the coding style!

The coding style is mostly based on PSR-2, as far as it is applicable to C++ code. Read more about PSR-2 here: https://www.php-fig.org/psr/psr-2/

The differences from and the additions to PSR-2 are described in the section "Differences from PSR-2 and additions" in this file.

PSR-2

Here are some details of the coding style:

Intendation

Four spaces per intendation level, no tabs allowed.

Class and Method naming

Class names are in UpperCamelCase, methods must be named in lowerCamelCase.

Variable and attribute naming

For variables, class attributes and function/method parameters under_score names must be used.

Constants

Constants must contain only CAPITAL_LETTERS_AND_UNDERSCORES.

Differences from PSR-2 and additions

switch-case

  • The line containing the switch statement must also contain the opening curly brace.
  • Cases must be indented with four whitespaces. The opening curly brace has to be in the same line as the case statement.
  • The next case (or the default case) are placed in a new line after the closing curly brace of the preceeding case.
  • The default case must be the last case.

Example:

switch (i) {
    case 0: {
        do_zero();
        break;
    }
    case 1: {
        do_one();
        break;
    }
    default: {
        do_nothing();
    }
}

Access labels (public, protected, private)

Access labels have to be indented by four whitespaces. Two empty lines have to be placed before and after the label and the first method/attribute, except when the label is the first thing in a class/struct declaration. In that case, no newline needs to preceed the label.

Example:

class A
{
    public:


    /**
     * Does this.
     */
    void doThis();


    /**
     * Does something else.
     */
    virtual void doSomethingElse();


    protected:


    /**
     * This actor does stuff in doThis and doSomethingElse.
     */
    Other actor;
};