Spacing and Indentation

Basic Rules

  • Use TAB based indentation with TAB size of 4. Make sure your IDE or text editor is configured accordingly.

Control Statements

  • Do not insert whitespace after keywords:

    // Right
    while(true)
    for(int i = 0; i < count; i++)
    
    // Wrong
    while (true)
    for (int i=0;i<count;i++)
    
    // Right
    if(a == b)
    switch(a)
    {
    case kCase1 :
      break;
    }
    
    // Wrong
    if (a==b)
    switch (a)
    {
      case kCase1 :
      break;
    }
    
  • Linebreak before opening brackets of a conditional expression:

    // Right
    if(expression)
    {
    }
    
    // Wrong
    if(expression) {
    }
    

Functions

  • Insert whitespace before opening parenthesis of function arguments:

    // Right
    void saySomething (const char* what);
    saySomething ("Hi!");
    
    // Wrong
    void saySomething(const char*what);
    saySomething( "Hi!" );
    
  • Linebreak before opening brackets of functions:

    // Right
    void myFunction ()
    {
    }
    
    // Wrong
    void myFunction () {
    }
    

Arrays

  • Do not add whitespace before square brackets:

    // Right
    char string[256];
    string[0] = 'a';
    string[i + 1] = 'x';
    
    // Wrong
    char string [256];
    string [0]='a';
    string[i+1] = 'x';
    

Classes

  • Linebreak before opening brackets of class declarations:

    // Right
    class MyClass
    {
    };
    
    // Wrong
    class MyClass {
    };
    

Namespaces

  • Forward declarations: do not linebreak namespace opening bracket, do not indent inside the namespace, put closing } on same line as last forward declaration:

    // Right
    namespace MyNamespace {
    class MyClass;
    interface IMyInterface; }
    
    // Wrong
    namespace MyNamespace
    {
      class MyClass;
      interface IMyInterface;
    }
    
  • Indent namespaces that are used like classes:

    // Right
    namespace MyNamespace
    {
      static const int kConstant;
    };
    
    // Wrong
    namespace MyNamespace {
      static const int kConstant;
    };
    

Macros

  • Iteration macros for container classes require spacing and indentation:

    // Right
    VectorForEach (data, Type, value)
      print (value);
    EndFor
    
    // Wrong
    VectorForEach(data,Type,value)
    print (value);
    EndFor
    

Types

  • For pointer and reference types the asterisk and ampersand are part of the type:

    // Right
    Type* var
    Type& var
    
    // Wrong
    Type *var
    Type & var
    

Templates

  • Do not insert whitespace before template type opening angle bracket:

    // Right
    MyClass<T> t;
    
    // Wrong
    MyClass <T> t;
    

Comments

  • In comments, insert a space character before the text:

    // Right
    // A comment
    /* Another comment */
    
    // Wrong
    //Comment
    /*Another comment*/
    

Preprocessor Directives

  • Align preprocessor directives with the code

    // Right
    void myFunction ()
    {
        if(condition)
        {
            #if PREPROCESSOR_DEFINE
            callFunction ();
            #else
            callSomethingElse ();
            #endif
        }
    }
    
    // Wrong
    void myFunction ()
    {
        if(condition)
        {
    #if PREPROCESSOR_DEFINE
            callFunction ();
    #else
            callSomethingElse ();
    #endif
        }
    }