Naming conventions represent an important part of coding. It is the process of choosing suitable names for functions, variables, classes, and other program entities. Naming conventions improve code readability and comprehension, allowing it to be simpler to maintain and adapt in the future. The next section will go through C++ naming conventions.
What is Naming Convention in C++?
Naming standards in C++ often involve the use of certain prefixes or suffixes, camel case, capital letters for variables, and beginning names of classes with uppercase letters. The goal of these conventions aims to keep the code more consistent and easier to read so that other programmers can quickly and easily grasp it.
Different Naming Convention of Variables
In C++, some typical variable name practices are:
1: Variable names ought to be descriptive and significant, describing exactly what the variable represents.
2: Camel case: It is a style in which the initial letter of a word is lowercase, and the initial letter of each succeeding word is capitalized, with no empty spaces between words. In C++, this convention is often used for variable names.
3: Using “is” to prefix Boolean variables: It is usual to prefix a variable’s name with “is” or “has” to indicate that it represents a Boolean value.
4: The constants must be named via all uppercase letters and underscore among words to indicate the fact that they are not intended to be updated.
5: Pascal Case: This case is similar to the camel case. The only distinction between both is that the starting letter of the initial word must likewise be capitalized in Pascal’s case. In opposition to the camel case, in which the initial word is in lowercase, if you use Pascal case, each word begins with an uppercase letter.
Below is an example of naming conventions in C++, so that you can easily understand the concept of naming convention.
Example: C++ Program to Display Variables with Different Naming Conventions
Following is a simple C++ program implementation that shows the above variable naming conventions:
using namespace std;
int main() {
// with descriptive names of variables
int totalNumber = 100;
// with camel-case of variable names
string nameOfStudent = "Sam";
// Prefixing boolean variables "is"
bool isEmployed = false;
bool isChecked = true;
// Appling all uppercase letters for constant variables
const int HIGHEST_ASSIGNMENT= 100;
const double PI_VALUE = 3.14;
//naming convention of the variable through pascal case
string FinalResultOfStudent = "Pass";
cout << "--[Different Naming Convention of variables in C++]--"<< endl;
cout << "1: With descriptive names of variables"<< endl;
cout << "Total Number of students: " << totalNumber << endl;
cout << "2: With Camel-Case of variable names"<< endl;
cout << "Student name: " << nameOfStudent << endl;
cout << "3: With Prefixing boolean variables "<< endl;
cout << "Is employed: " << isEmployed << endl;
cout << "Is checked: " << isChecked << endl;
cout << "4: With appling all uppercase letters naming convention for constant variables"<< endl;
cout << "The Highest Number of Assignments: " << HIGHEST_ASSIGNMENT << endl;
cout << "The value of PI: " << PI_VALUE << endl;
cout << "5: Variable Convention With Pascal-Case"<< endl;
cout << "Final Result: " << FinalResultOfStudent << endl;
return 0;
}
This program simply declared variable syntax according to the above five mentioned naming conventions. In the main function, the first variable is totalNumber which is according to the descriptive naming convention which prints 100 values as output. Next nameOfStudent variable is initialized with Mickle Steve which shows the Camel Case naming convention.
The isEmployed and isChecked variables showed the Boolean result as output that represents the Prefixing naming convention. After this, HIGHEST_ASSIGNMENT and PI_VALUE variables are initialized with respected values as 100 and 3.14 which defines the uppercase letter of the naming convention.
In the end, the FinalResultOfStudent variable represents the Pasal case convention of naming variables. This simple program used the naming convention one by one that are mentioned above and print them on the console using cout as shown in the following output:
Note: These naming conventions assist other programmers to comprehend the source code more quickly and readily by making it standardized and less difficult to read.
Conclusion
Naming conventions are critical in programming since they aid in code comprehension and maintainability. To guarantee uniformity and clarity, C++ developers should adhere to specified naming patterns. Following these rules may render code easier to read and alter, lowering the chances of errors and defects. By following certain naming conventions, programmers can produce more efficient and maintainable code.