Naming a file or a variable is the first and the very basic step that a programmer takes to write clean codes, where naming has to be appropriate so that for any other programmer it acts as an easy way to read the code. In C++, naming conventions are the set of rules for choosing the valid name for a variable and function in a C++ program.
- The class name should be a noun.
- Use upper case letters as word separators, and lower case for the rest of the word in the class name.
- The first character in the class name must be in upper case.
- No underscores ('_') are permitted in the class name.
- The private attribute name in class should be prepended with the character 'm'.
- After prepending 'm', the same rules will be followed for the name as that for the class name.
- Character 'm' also precedes other name modifiers also. For example, 'p' for pointers.
- Each method/ function name should begin with a verb.
- The first character of function/ method argument names should be lowercase. All words starting after the first letter should be in the upper case with class names.
- The variable name should begin with an alphabet.
- Digits may be used in the variable name but only after the alphabet.
- No special symbols can be used in variable names except for the underscore('_').
- No keywords can be used for variable names.
- Pointer variables should be prepended with 'p' and place the asterisk '*' close to the variable name instead of the pointer type.
- Reference variables should be prepended with 'r'. This helps to differentiate between the method returning a modifiable object and the same method returning a non-modifiable object.
- Static variables should be prepended with 's'.
- The global constants should be all capital letters separated with '_'.
- No special character is allowed in the file name except for underscore ('_') and dash ('-').
- The file name should end with the .cc extension in the end or should end with the .cpp extension.
- Do not use filenames that already exist in /user/include. or any predefined header file name.
Example:
Calculate the product of two variables-
Name variable 1 as var1
Name variable 2 as var2
Name result as product or pdt
This will be more convenient naming convention than assuming the random names like x, y, and z.
Advantages of Naming Conventions:
Following a set of rules in naming convention while writing the code helps in the following ways-
- Avoids naming conflicts.
- Improves clarity of the code in case of ambiguity.
- Helps to formalize and promote consistency within a team.
- Improves understanding and readability of the code.
Illustrations:
- Class: Name a class in C++ after what it is and use upper case letters as word separators. The class name should be a noun. For example, ReverseString, and Multiply2Numbers. The name should be specific and hint at the functioning of the class without glancing at the body of the class. This type of convention is called PascalCase.
- Methods: Every method and function performs an action, so the function name should make it clear what it does. For example, writeDataToFile() is more convenient name than WriteFile(). Function names should begin with a verb. This type of convention is called the camel case. We can also follow snake_case for methods. In the snake case, we have to join two separate words using '_' (Underscore).
- Constants: Constants should be all capital letters with '_' separators. For example, MAX_INT, TWO_PI, etc.
Naming Convention in C++
Names in the program are the key to program readability. If the name is appropriate in a program, then everything fits together and relationships are clear, meaning is derivable. C++ uses CamelCase as a practice for writing names of methods, variables, classes, packages, and constants.
CamelCase is a naming convention where a name is formed of multiple words that are joined together as a single word with the first letter of each of the word capitalized.
Below are the naming conventions of C++ programming. They must be followed while writing code in C++ for good maintenance, readability, and understanding of the program.
Type 1: Classes and Class Attributes Names
- The class name should be a noun.
- Use upper case letters as word separators, and lower case for the rest of the word.
- The first character in the class name must be in upper case.
- No underscores ('_') are permitted in the class name.
class PerimeterRectangle
class FingerprintScanner
- The private attribute name in class should be prepended with the character 'm'.
- After prepending 'm', the same rules will be followed for the name as that for the class name.
- Character 'm' also precedes other name modifiers also. For example, 'p' for pointers.
class PerimeterRectangle
{
public:
int perimeter;
private:
int mLength;
int mWidth;
}
Type 2: Functions and Function Argument Names
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.
- Suffixes are sometimes useful. For example,
- Count- the current count of the counter.
- Key- the key value.
- Prefixes are sometimes useful. For example,
- get-get value.
- set- set value.
The same name convention is used as that for the class names.
int getValue();
int SolveEquation();
The first character of function/ method argument names should be lowercase. Each word should also begin with a capital letter.
int PerimeterRectangle(int lengthRectangle, int widthRectangle)
Type 3: Variables
When the variables are declared dynamically using the new keyword or if the variables are declared as class attributes then they take memory from the heap and when the variables are created in a C++ program, the memory is allocated from the program stack.
- The variable name should begin with an alphabet.
- Digits may be used but only after the alphabet.
- No special symbols can be used in variable names except for the underscore('_').
- No keywords can be used for variable names.
int total_cost;
int length;
Pointer variables should be prepended with 'p' and place the asterisk '*' close to the variable name instead of the pointer type.
int *pName;
int *pAge, address; // Here only pAge is a pointer variable
Reference variables should be prepended with 'r'. This helps to differentiate between the method returning a modifiable object and the same method returning a non-modifiable object.
Static variables should be prepended with 's'.
static int sCount;
Type 4: Constant
The global constants should be all capital letters separated with '_'.
const double TWO_PI = 6.28318531;
Type 5: File Naming;
- No special character is allowed in the file name except for underscore ('_') and dash ('-').
- The file name should end with the .c extension in the end or should end with the .cpp extension.
- Do not use filenames that already exist in /user/include. or any predefined header file name.
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
[email protected]// Not Valid
Similar Reads
rename function in C
The rename() function is used to rename a file in C. It changes the name of the file from old_name to new_name without modifying the content present in the file. It is defined inside <stdio.h> header file. In this article, we will learn how to rename a file using the rename() function in C pro
2 min read
String Functions in C++
A string is referred to as an array of characters. In C++, a stream/sequence of characters is stored in a char array. C++ includes the std::string class that is used to represent strings. It is one of the most fundamental datatypes in C++ and it comes with a huge set of inbuilt functions. In this ar
8 min read
Converting String into Set in C++ STL
Prerequisites: String in C++Set STL in C++ A string is a collection of characters and if we are converting it into a set the only reason can be to check the characters being used without duplicate values. Example: string s="Geeks for Geeks is for Geeks" // G e k s f o r i are characters // set can s
2 min read
Calling Conventions in C/C++
In C/C++ programming, a calling convention is a set of rules that specify how a function will be called. You might have seen keywords like __cdecl or __stdcall when you get linking errors. For example: error LNK2019: unresolved external symbol "void __cdecl A(void)" (?A@@YAXXZ) referenced in functio
11 min read
Function Pointer in C++
Prerequisites: Pointers in C++Function in C++ Pointers are symbolic representations of addresses. They enable programs to simulate call-by-reference as well as to create and manipulate dynamic data structures. Iterating over elements in arrays or other data structures is one of the main use of point
4 min read
goto Statement in C++
The goto statement in C++ is a control flow statement that provides for an unconditional jump to the same function's predefined label statement. In simple terms, the goto is a jump statement transfers the control flow of a program to a labeled statement within the scope of the same function, breakin
5 min read
Enumeration in C++
In C++, enumeration is a user-defined data type that consists of a set of named integer constants. It helps in assigning meaningful names to integer values to improve code readability and maintainability.Create EnumAn enum needs to be defined before we can create its variables.C++enum enum_name { na
3 min read
std::function in C++
The std::function() in C++ is a function wrapper class which can store and call any function or a callable object. In this article, we will learn about std::function in C++ and how to use it in different cases.Table of ContentWhat is std::function in C++?Example of std::functionMember Functions of s
5 min read
Lambda Expression in C++
C++ 11 introduced lambda expressions to allow inline functions which can be used for short snippets of code that are not going to be reused. Therefore, they do not require a name. They are mostly used in STL algorithms as callback functions.Example:C++#include <bits/stdc++.h> using namespace s
4 min read
Name Mangling and extern "C" in C++
C++ supports function overloading, i.e., there can be more than one function with the same name but, different parameters. How does the C++ compiler distinguish between different functions when it generates object code - it changes names by adding information about arguments. This technique of addin
3 min read