Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Preprocessor, Tools and Concepts

C++ naming and formatting conventions

Naming and formatting conventions for C++: snake_case or PascalCase, constants, member variables, namespaces, headers, include order and clang-format.

C++ has no official style. The standard library uses snake_case (std::vector, push_back), but the language allows anything, and big projects each chose their own: Google's style uses PascalCase types and snake_case variables, the LLVM style uses PascalCase for most things, and Qt uses camelCase. What matters is that a project picks one convention, writes it down, and applies it everywhere, preferably with a formatter that enforces it.

This page describes the most common choices and the few rules that are not matters of taste (names the language reserves, headers, and include order). The examples follow one consistent style, and you can adapt it to your team's.

C++
class OrderBook {                 // types: PascalCase
public:
    void add_order(int quantity); // functions: snake_case (or camelCase)
private:
    int order_count_ = 0;         // members: trailing underscore
};

Try it

C++ One consistent style

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ Reserved names and safe alternatives

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Common naming choices

Entity Typical conventions Notes
Types (classes, structs, enums, aliases) PascalCase (OrderBook), or snake_case as in the standard library (order_book) Use PascalCase unless you are writing a standard-library-like template.
Functions and methods snake_case (add_order) or camelCase (addOrder) Use verbs for actions and nouns or adjectives for queries (size(), empty()).
Variables and parameters snake_case or camelCase Short names in tiny scopes (i, it), descriptive names elsewhere.
Private data members name_, m_name or _name (avoid the last) A suffix or prefix makes members easy to tell apart from locals.
Constants (constexpr, const) snake_case, kPascalCase (kMaxItems) or ALL_CAPS Reserve ALL_CAPS for macros if you can.
Enumerators PascalCase for enum class, ALL_CAPS in older C-style code Scoped enums need no prefix: OrderState::Pending.
Namespaces lowercase, short; nested as company::project Never use using namespace in a header.
Template parameters T, U, or PascalCase for meaning (Key, Value, Allocator) Name concepts like adjectives or capabilities (Sortable, std::integral).
Macros PROJECT_ALL_CAPS They ignore namespaces, so a prefix is essential.
Files snake_case.cpp / .h or .hpp, matching the main class Use .cpp for sources and .h or .hpp for headers consistently.

Reserved identifiers

These are never safe in your own code, even when they compile:

  • names containing a double underscore (my__name),
  • names beginning with an underscore followed by an uppercase letter (_Count),
  • names beginning with an underscore in the global namespace (_count),
  • names that clash with keywords, including the alternative tokens and, or, not, xor, bitand,
  • the std namespace (adding your own declarations to std is undefined behavior, apart from a few specializations).

Formatting

  • Use a formatter. clang-format with a .clang-format file (for example BasedOnStyle: Google or LLVM, with your own tweaks) ends formatting discussions. Run it in the editor and in CI.
  • Indentation: 2 or 4 spaces, never tabs mixed with spaces.
  • Braces: always use them, even for one-line if bodies (the "goto fail" bug came from a missing pair). Pick the Allman or the K&R placement and keep it.
  • Line length: 80 to 120 columns. Break long expressions after a binary operator or comma.
  • One declaration per line, and declare variables where you first use them.
  • Pointer and reference stars: int* p (attach to the type, common in C++) or int *p; the formatter should decide.
  • Includes, in this order: the related header first (so it proves it is self-contained), then project headers, then third-party headers, then standard library headers, each group sorted alphabetically.
  • Header guards: #pragma once (not standard, but supported by every major compiler) or #ifndef PROJECT_PATH_FILE_H_ guards (see Include guards).
  • Comments: explain why, not what. Document public APIs with Doxygen-style /// comments, and keep them current.

Usage notes

  • Consistency beats the "best" style. In an existing project, follow the style already used in the file you are editing.
  • Avoid Hungarian notation (iCount, strName). The compiler already checks types, and the prefix goes stale when a type changes.
  • Do not abbreviate unless the abbreviation is common in the domain (id, url, idx in a loop). usr_cnt costs the reader more than the characters save.
  • Boolean names read as questions: is_empty, has_value, should_retry. Avoid negative names (not_ready), which make !not_ready confusing.
  • Getters are often named after the property (name()), setters set_name(...). Do not write trivial getters and setters for plain data: use a struct.
  • Do not repeat the type in the name (std::vector<int> int_vector). Name what the object means (scores).
  • Named constants beat magic numbers: constexpr int max_retries = 3; is searchable and documents intent.
  • Check naming automatically with clang-tidy's readability-identifier-naming (see Static analysis).