Skip to content

Style Guide

Pirulax edited this page Jun 17, 2021 · 3 revisions
  • We use 4 spaces instead of tabs.

  • Hungarian notation for variable names. (Although, not enforced for new code)

    float         fValue;               // Local variable
    unsigned char m_ucValue;            // Class member variable
    char          ms_cValue;            // Class static member variable
    bool          g_bCrashTwiceAnHour;  // Global variable
    char*         szUsername;           // Zero terminated string
    SString       strUsername;          // String
    CVector       vecPosition;          // 3D Vector

    Sometimes a is used for arrays C-style arrays.

  • Lower camel case for variable names of types like custom structs and enums:

    SSomeStruct   valueOne;
    ESomeEnum     m_valueTwo;
  • Functions and classes use UpperCamelCase:

    void UpperCamelCase();
    class Vector;
  • Functions with no arguments are declared and defined with (), and called with ()

    void MyTest();
    void MyTest() { return; }
    MyTest();
  • Do not use nested structures without braces:

    // This is disallowed:
    for (dont)
        for (do)
            for (this)
                ...
    
    // Either of these are allowed
    if (x) {
        y;
    }
    
    if (x)
        y;
  • Use range-based for loops wherever possible (all std containers support it):

    std::vector<int> vec; // Example std container
    
    // Bad:
    for (std::vector<int>::iterator it = vec.begin(); it != vec.end(); it++)
    
    // Good:
    for (const auto& v : vec)
    for (const int& v : vec)
    
    // There are situations where you must use old style loops, in this case use `auto`
    for (auto it = vec.begin(); it != vec.end(); it++)
  • Put #pragma once preprocessor directive next to the copyright comment for new header files and the ones you are modifying for the pull request. Make sure to remove include guard if you are using #pragma once:

    /*****************************************************************************
    \
    *
    *  PROJECT:     Multi Theft Auto
    *  LICENSE:     See LICENSE in the top level directory
    *  FILE:        Client/mods/deathmatch/logic/CClientIFP.h
    *
    *  Multi Theft Auto is available from http://www.multitheftauto.com/
    *
    *****************************************************************************/
    
    #pragma once
  • Once you're done writing code, and you're about to create a pull request, apply clang formatting:

  • For anything else, follow the style of the code that already exists.

  • Tip: In Visual Studio go to Tools -> Options -> Text Editor -> C/C++ -> Formatting -> Spacing and you can configure it to automatically apply the right spacing.

Clone this wiki locally