c++naming-conventionsidentifier

Has Bjarne Stroustrup ever published naming guidelines for identifiers in C++?


Has Bjarne Stroustrup ever published naming guidelines for identifiers in C++ and if yes, what are they?


Solution

  • Warning: the guidelines/rules mentioned below are by no means the only possible such guidelines/rules. There are many others. In particular, if you work in a code base with approved rules, just apply them: when it comes to code style, consistency matters most.


    Bjarne Stroustrup has published the PPP Style Guide and later on updated it, in the scope of Programming: Principles and Practice using C++.

    It is mentioned in the C++ Core Guidelines.

    See also Bjarne Stroustrup's C++ Style and Technique FAQ.

    Specifically about identifiers, including namespace names, with examples, see §25.6.2 Sample rules, R302, in Programming: Principles and Practice using C++, Second Edition:

    R302: Identifiers should be given descriptive names.

    • Identifiers may contain common abbreviations and acronyms.
    • When used conventionally, x, y, i, j, etc. are descriptive.
    • Use the number_of_elements style rather than the numberOfElements style.
    • Hungarian notation shall not be used.
    • Type, template, and namespace names (only) start with a capital letter.
    • Avoid excessively long names.

    Example: Device_driver and Buffer_pool.

    Reason: Readability.

    Note: Identifiers starting with an underscore are reserved to the language implementation by the C++ standard and thus banned.

    Exception: When calling an approved library, the names from that library may be used.