Get the best programming course avaliable.

Naming Conventions in C++

Naming conventions are rules for naming declared symbols. They help us keep consistent looking code. The aim is to make the purpose and functionality of a component clear through its name.

Naming variations

This list details the most common ways to name symbols.

Variation Description Common Use
snake_case All lower-case. Separate words with underscores. Everything
camelCase Uses capitalization to separate words. The first word is not capitalized. Primarily variables.
PascalCase Capitalizes the first letter of each word. Functions, classes, enums
SCREAMING_SNAKE_CASE All upper-case. Separate words with underscores. Constants

It's normal to use many naming conventions on one project. If you name structs with PascalCase and functions with camelCase, you can tell that Jumper() is a constructor call, not a function. This is good for code analysis, but your intuition also benefits from this. It makes code easier to understand (once you're used to a naming convention).

Prefixes

Prefixes are often used to add context to symbols.

Here are some common prefixes.

Prefix Description
g or g_ Used for global variables.
m or m_ Used for member variables.
s or s_ Used for static variables.
p or p_ Used for pointers.
E or e_ Used for Enums
I or i_ Used for class interfaces.
T or t_ Used for typedefs or structs.

Hungarian notation

This prefix-heavy notation method includes type data as part of the name.

A float would have the prefix f, and std::string has the prefix str. Arrays have the prefix arr, and stacks with the data type prefix: float arrfExample[4];.

Due to modern IDEs revealing type information, Hungarian notation is mostly redundant and verbose.

Files

It's nice to have consistency in your file naming. snake_case and PascalCase are the most common. Files are often named the same as the main component inside them, in which case they adopt the name from the namespace or class notation.

C++ lets you use various file exertions for the same file types:

  • .h, .hpp, and .hh for header files.
  • .cpp, and .cc for source files.

Creating your naming convention

Creating a document with your naming conventions can be helpful when working on a project with others. It can also serve as a tool for increasing the consistency of your code. You can find many examples of design documents used by large companies like Google online.

Below is an example ruleset for the naming conventions of a project. Feel free to copy it or make your own with your own rules.

Example:

Item Convention
Namespaces snake_case, preferably single name
Types, Classes & Enums CamelCase
Interfaces & Abstract Classes CamelCase with I prefix              
Functions & Methods snake_case
Variables snake_case
Member variables snakecase with `m` prefix            
Static member variables snake_case with no prefix
Constants and Enumerators SCREAMING_SNAKE_CASE
Macros SCREAMING_SNAKE_CASE
File names snake case with .hpp and .cpp
Type parameters concise CamelCase, usually single letter

In a team

Many teams have a design document detailing the naming conventions they wish to use. If there isn't one, you must look at the code and figure out the naming conventions used. Hopefully, this article has given you enough insight to identify the standards they use.