General Whitespace

Consistency is Key

In our opinion, consistency is the main building block that the entire coding standard builds on.

We think that clear communication in code starts with text layout, with this page guiding you through what that looks like for us.

Word Wrapping

Disable this feature in your editor.

Reasoning

  • Aim for predictable text positioning.
  • Formatting is inconsistent when using word wrapping.
  • You make it difficult for others to read your code if they do not use that feature.
  • Aim to have all of the precious text you spend your valuable time on looking the exact same, no matter where you bring it.

Tabs vs Spaces

It's simple: no tabs. Instead, use 4 spaces.

Reasoning

To allow for this standard's particular formatting and placement styles, and enforcing consistency across all text editors and IDEs.

The entire purpose of a coding standard, in our opinion, is consistency.

Newlines - Line Endings

Configure all of your systems to use CRLF. This is to be absolutely sure your code will compile with Windows compilers. Most (if not all) other compilers are compatible with it. Don't forget that git has some settings to help cope with it.

How to Change Line Endings in Popular Editors

Column Limitation

Although this risks breaking consistency at a pedantic level, there is no column limit. Simply prefer keeping code about a half-window's space in an HD resolution view to allow viewing code side-by-side in tabs.

If a group of lines are too long, it may indicate a higher-order breakdown; splitting the code into multiple lines, multiple functions, etc...

Don't feel forced to break words down unnecessarily to fulfill column limits, like in functions or variable names, especially if it means improved clarity and intentions based on identifier names.

Braces

Allman style, where all curly braces are always on a separate line.

static int getBestFoo()
{
    if (somethingImportantHappened())
    {
        static int foo = 0;
        ++foo;
        return foo;
    }

    return -1;
}
static const char* daysOfTheWeek[] =
{
    NEEDS_TRANS ("Sunday"),
    NEEDS_TRANS ("Monday"),
    NEEDS_TRANS ("Tuesday")
    //[...]
};
do
{
}
while (someCondition);

Functions and Methods

When writing a function or method:

  • Write the name on the same line as the return type.
  • Insert only one single space after the return type.
  • Insert only one single space after an explicit calling convention specifier.
void __stdcall foo()
{
}

If you want to distinguish a group of parameters, or if the line is too long, break multiple parameters up into a coherent set thereof.

Also, always line up the first parameter's type on the next line with the previous line's first type.

void foo (int firstX, int firstY,
          int secondX, int secondY)
{
    //Do stuff with these parameters
}

void bar (ComplexType& a,
          OtherType& b,
          Something& c)
{
    //Do stuff with these parameters
}

Templates

To start, note that the function and method parameter rules apply the same to template parameters.

Never insert a space before or after a template declaration's chevrons (ie: <>).

template<typename Type>
void foo (Type a)
{
    //Achieve something grand with 'a'
}

Place templates on the line before a function, method, class or struct declaration.

template<typename Type, typename OtherType>
void foo (Type a, OtherType otherA,
          Type b, OtherType otherB)
{
    //Do stuff with these parameters
}

Casting

C-style

Insert 1 space after the cast's ):

auto result = (double) value;

C++ style

Follow the parentheses rules as you would with methods/functions and templates:

auto result = static_cast<double> (value);