Skip to content

General Whitespace

Joël R. Langlois edited this page Jan 29, 2025 · 6 revisions

General Idea

Be consistent and predictable. That's it! Aim to have all of the precious text you spend your valuable time on looking the exact same, no matter where you bring it.

Make the formatting and placement styles the same across all editors and IDEs.

Tabs vs Spaces

Use 4 spaces - no tabs.

Word Wrapping

Disable this feature in your editor.

Newline/Line Ending

CRLF.

This is to be absolutely sure your code will compile on Windows compilers. Most (if not all) *nix IDEs are compatible with it. Don't forget that git has some settings to help cope with it.

Changing Line Endings in Popular IDEs

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);

Column Limitation

There is no 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...

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);
Clone this wiki locally