Mistake on this page? Email us

C++ coding style guide

Names

Global variables

  • Global variables have names with a g_ prefix. Example: g_time_prefix_format.

Classes and Structs

  • Names of classes, structs and aliases for them are in UpperCamelCase. When the name includes an initialism, only the first letter of the initialism is capitalized. Example: MblCloudClient.

  • Use class rather than struct whenever it contains anything but public data members.

  • Names of data members are in lower_snake_case.

  • Static data member names have an s_ prefix. Example: s_instance.

Enums

  • Names of enum types and aliases for them are in UpperCamelCase. When the name includes an initialism, only the first letter of the initialism is capitalized. Example: MblError.

  • In C++98 code, enumerator names are usually prefixed with the name of the enum type and an underscore (Example: MblError_None) to mitigate the lack of scoping for enumerator names. In C++11 code that doesn't touch C/C++98 interfaces (and isn't likely to in the future), scoped enums should be used instead.

Methods and Free Functions

  • Names of methods and free functions are in lower_snake_case, except when the name includes a type - then the type name is used verbatim. Example: MblError_to_str.

Namespaces

  • Namespace names are in lower_snake_case.

  • Closing braces for namespace blocks are commented with the name of the namespace. For example:

    namespace mbl {
    
    /**
     * Initialize the log and trace mechanisms. After calling this the mbed-trace
     * library can be used for logging.
     */
    MblError log_init();
    
    } // namespace mbl
    

Files

  • Files containing code for a single class are named after that class. Examples: MblCloudClient.h and MblCloudClient.cpp.

  • Files containing code for a single function are named after that function.

  • Files containing a number of functions are given a descriptive name in lower_snake_case such as update_handlers.h and update_handlers.cpp

Whitespace

  • Indent four spaces at a time (no tabs).

  • Function/method definitions:

    ReturnType ClassName::method_name(ParamType1 param1, ParamType2 param2)
    {
        // Stuff
    }
    

    Or, if that gets too long:

    ReturnType ClassName::method_name(
        ParamType1 param1,
        ParamType2 param2,
        ParamType3 param3,
        ParamType4 param4)
    {
        // Stuff
    }
    

    Or, if there's a constructor's initialization list:

    ClassName::ClassName(ParamType1 param1, ParamType2 param2)
        : member1_(param1)
        , member2_(param2)
    {
        // Stuff
    }
    

    And possibly:

    ClassName::ClassName(
        ParamType1 param1,
        ParamType2 param2,
        ParamType3 param3,
        ParamType4 param4
    )
        : member1_(param1)
        , member2_(param2)
    {
        // Stuff
    }
    
  • C++98 enum definitions:

    enum EnumType {
        EnumType_Value1,
        EnumType_Value2,
        EnumType_Value3,
    };
    
  • Namespace blocks are not indented:

    namespace mbl {
    
    class MblCloudClient
    {
        // stuff
    };
    
    } // namespace mbl
    
  • ifs:

    if (condition) {
        // Stuff
    }
    else {
        // Other stuff
    }
    
  • fors:

    for (i = 0; i < n; ++i) {
        // Stuff
    }
    
  • whiles:

    while (condition) {
        // Stuff
    }
    
  • switches:

    switch (value) {
        case EnumValue1: return stuff;
        case EnumValue2: return other_stuff;
    }
    

    or

    switch (value) {
        case EnumValue1:
            DoStuff();
            break;
        case EnumValue2:
            DoOtherStuff();
            break;
    }
    

Other guidelines

mbl-cloud-client contains a mixture of C++98 and C++11, and in certain places must provide C-style APIs. Not all of the following guidelines will apply to both C++98 and C++11 and C-style APIs.

The list of C++ Core Guidelines below is not the complete list of the C++ Core Guidelines that apply to MBL; it is merely the highlights.

Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.