![]() |
VOOZH | about |
Naming conventions are recommended guidelines for choosing names for classes, functions, variables, constants, and files in C++. While they are not enforced by the language, following consistent naming practices improves code readability, maintainability, and collaboration.
Example: Consider a program that calculates the product of two numbers:
int var1 = 10;
int var2 = 20;
int product = var1 * var2;
Using descriptive names such as product makes the code easier to understand than using generic names like x, y, and z.
Consistent naming makes code easier to read, understand, and maintain. Although C++ does not enforce any specific naming style, most projects follow common conventions to keep code organized and readable.
Class names should represent what an object is and are typically written using PascalCase.
class PerimeterRectangle
class FingerprintScanner
class PerimeterRectangle
{
public:
int perimeter;
private:
int mLength;
int mWidth;
}
Usually, every function in C++ performs one or more actions, so the name of the function should clearly hint at what it does. Each method/ function name should begin with a verb.
Function names commonly follow camelCase, although some projects may use PascalCase or snake_case.
int getValue();
int solveEquation();
Parameter names are typically written in camelCase and should clearly describe the value they represent.
int PerimeterRectangle(int lengthRectangle, int widthRectangle)
Variable names should clearly describe the data they store and follow a consistent naming style to improve code readability and maintainability.
int total_cost;
int length;
Prefixing pointer variables with 'p' (e.g., pValue) is an optional naming convention (Hungarian notation) and not required in C++. The placement of '' (e.g., int ptr or int ptr) is a style preference.
int *pName;
int *pAge, address; // Here only pAge is a pointer variable
Using 'r' as a prefix for reference variables is an optional convention and not a standard C++ requirement. References are already indicated by '&', so such prefixes are generally unnecessary in modern C++ code.
Static variables should be prepended with 's'.
static int sCount;
The global constants should be all capital letters separated with '_'.
const double TWO_PI = 6.28318531;
File names should be simple, descriptive, and easy to identify.
helloworld.c // Valid
hello_world.cpp // Valid
hello-world.cpp // Valid
hel-lo_world.cpp // Valid
hello* world.cpp // Not Valid
iostream.cpp // Not Valid
hello123@world.cpp// Not Valid
Following a consistent naming style provides several advantages: