Naming Convention in C++
The first and most fundamental step a programmer takes to produce clean code is to name a file or a variable. This naming must be acceptable so that it serves as an easy way for any other programmer to read the code. Naming conventions in C++ are a collection of guidelines for selecting a proper name for a variable or function in a C++ program.
These are some of the rules that we need to follow to maintain the naming convention.
- Ideally, the class name will be a noun.
- Use lowercase letters for the remaining words in the class name and uppercase letters as word separators.
- A capital letter must be used to start the class name.
- There can be no underscores ('_') in the class name.
- The letter "m" should come before the private attribute name in the class.
- The same guidelines that apply to the class name will apply to the name after prepending "m."
- Other name modifiers are likewise preceded by the character "m." For pointers, use the symbol "p."
- Every method or function name needs to start with a verb.
- Names of function/method arguments should begin with a lowercase letter. Containing the exception of class names, all words with a second letter should be written in uppercase.
- An alphabet should be used to start the variable name.
- Only after the alphabet are digits allowed in the variable name.
- No other special characters may be used in variable names save the underscore ('_').
- Variable names are not permitted to contain keywords.
- The asterisk (*) should be used next to the variable name rather than the pointer type when referencing pointer variables.
- Reference variables need to have a "r" prefix. This makes it simpler to tell one method from another that returns a modifiable object from a method that returns a non-modifiable object.
- The letter "s" should be added before any static variables.
- The global constants must be separated by a - ' ' and typed in all capital letters.
- Except for underscore ('_') and dash ('-'), no other special characters are permitted in the file name.
- The file name should either finish with the.cc or.cpp extension.
- You shouldn't use any predefined header file names or filenames that are already present in the /user/include directory.
Example:
Calculate the product of two variables-
Name variable 1 as v1
Name variable 2 as v2
Name result as product or prd
This naming convention will be more practical than choosing names at random like x, y, and z.
Advantages of Naming Conventions
When writing code, adhering to a set of naming convention guidelines is beneficial in the following ways:
- Prevents naming conflicts.
- Enhances the code's clarity in cases of uncertainty.
- Helps a team become more organised and to foster consistency.
- Enhances the code's comprehension and readability.
Demonstration
- Class: In C++, give a class its own name and use uppercase letters to separate words. Ideally, the class name will be a noun. ReverseString and Multiply2Numbers are two examples. Without even looking at the content of the class, the name should be precise and suggest how the class will operate. PascalCase conventions fall under this category.
- Method: Every method and function does an action; therefore its purpose should be evident from the function name. As an illustration, writeDataToFile() is a more practical name than WriteFile (). Function names ought to be verb-based. Camel case conventions fall under this category. We may also use the snake case techniques. In the case of the snake, we must unite two distinct terms with a ' '. (Underscore).
- Constants: Constants should have '_' separators and all capital letters. Using MAX INT, TWO PI, etc. as examples.
Naming Convention in C++
C++ follows the CamelCase convention for writing the names of methods, variables, classes, packages, and constants,
CamelCase is a naming convention where a name is created from several words that are combined together as a single word with the first letter of each word capitalised.
The C++ programming naming conventions are listed below. They must be adhered to when writing C++ code for good maintenance, readability, and programme comprehension.
Type 1: Names of Classes and Class Attributes
- A noun should appear in the class name.
- Word separators should be capitalised, while the remaining characters should be written in lower case.
- Uppercase letters must begin the class name.
- The class name is not allowed to contain underscores ('_').
class PerimeterRectangle
class FingerprintScanner
- The letter 'm' should come before the private attribute name in the class.
- The same guidelines that apply to the class name will apply to the name after prepending 'm'.
- Other name modifiers are likewise preceded by the character 'm'. For pointers, use 'p' as an example.
class PerimeterRectangle
{
public:
int perimeter;
private:
int mLngth;
int mWdth;
}
Type 2: Functions and Function Argument Names
Every C++ function typically executes one or more operations, therefore the function name should make it obvious what it does. Every method or function name needs to start with a verb.
- Suffixes might be helpful on occasion. For instance
- Count: The counter's current count.
- The key value, or key.
- Prefixes can be helpful on occasion. For instance,
- set- set value
- get-get value.
The same naming convention as used for class names is employed.
int getValue();
int SolveEquation();
Names of function/method arguments should begin with a lowercase letter. Containing the exception of class names, all words with a second letter should be written in uppercase.
int PerimeterRectangle(int lengthRectangle, int widthRectangle)
Type 3: Variables
When variables are generated in a C++ programme, memory is allocated from the programme stack, unless when they are created dynamically using the new keyword or when they are defined as class attributes, in which case they take memory from the heap.
- An alphabet should be used to start the variable name.
- However, they should only be used after the alphabet.
- Except for the underscore ('_'), no other special characters may be used in variable names.
- It is prohibited to utilise keywords in variable names.
int t_cost;
int length;
Instead of using pointer type, pointer variables should be prefixed with 'p' and have an asterisk ('*') next to the variable name.
int *pName;
int *pAge, address; // Here only pAge is a pointer variable
Reference variables need to have a "r" prefix. This makes it easier to distinguish between a method returning a modifiable object and a method returning a non-modifiable object from the same method.
The letter "s" should come before any static variables.
static int sCnt;
Type 4: Constant
It is recommended to separate the global constants by a '_'.
const double TWO_PI = 6.283185;
Type 5: File Naming
- Except for underscore ('_') and dash ('-'), no special characters are permitted in the file name.
- Either the.cc or.cpp extension should be at the end of the filename.
- Use any predetermined header file name rather than filenames that are already present in /user/include.
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