Naming Conventions for member variables in C++
In this tutorial, we will discuss C++ Naming Conventions for member variables and what are the benefits of following the naming convention.
Firstly, Learn about the Naming Conventions and their use. In computer programming, a naming convention is a set of rules used by programmers for giving a name to the identifiers and other entities present in the source code.
Secondly, Why do we use naming conventions? What are the benefits?
Lets us suppose that there is not any rule for giving a name to the identifiers. In this case, the programmers will be able to choose any character sequence for naming variables, types, functions, and other entities in source code according to their convenience. As a result, it makes source code difficult to understand by the other programmer. Whereas following the same rule of the naming convention will reduce the conflict. It will reduce the effort required by the programmer to understand and discuss the code. And, it also saves the time of the programmer and increases the understandability of the code.
Now we are going to see the naming conventions for member variables with the help of simple C++ code.
Although Here the statement is syntactically correct, its purpose is not clear. It can be anything simple multiplication, price, or salary…
a = b * c;
On the other hand, the use of the naming convention makes it clear and increases the understandability.
weekly_pay = hours_worked * hourly_pay_rate;
The exact rules of a naming convention depend on the type of context or Identifier. Some rules are:
- Use CamelCase or PascalCase for all names. We start functions and variables with a lowercase letter.
- Filenames should consist of all lowercase only. It can include underscores (
_) or dashes (
-) for making the name clear. For example:
my_fathers_name.cc my-fathers-name.cc myfathersname.cc
- The names of all types ( like classes, structs, type aliases, enums) follow the same rules of naming convention. Apart from starting from the capital letter, the type name has a capital letter for each new word. Underscore is not allowed. For example:
- We use all lowercase for naming variables and data members.
- It may contain underscores between words to increase clarity.
- An underscore ( _ ) or number can’t be used in the starting place.
- No other symbols are allowed except underscore.
- Global variables, Static class, and Global constants are named with a g_, s_, and c_ prefix respectively.
a_local_variable, number, max_number.
- Constant name is followed by the letter ‘K’.
- or, You can all Capital letters.
Thank You for reading and Keep Learning