×

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:

  1. Prevents naming conflicts.
  2. Enhances the code's clarity in cases of uncertainty.
  3. Helps a team become more organised and to foster consistency.
  4. Enhances the code's comprehension and readability.

Demonstration

  1. 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.
  2. 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).
  3. 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

Related Topics

Bitmasking in C++

Bitmasking is a technique that is used to access a particular bit within the data bytes. When you apply the iterative approach, this phenomenon is used. A bitmask is described...

6 minutes read.

C++ Continue in Do-while loop

Continue statement: Inside the loop, the continue statement is used to control the loop. C++ utilizes the continue keyword to implement the continue statement, which transfers the program's flow at the start...

4 minutes read.

Free vs delete() in C++

Free vs delete() in C++ In this section, we will learn about the free() function and also create a C ++ program of the delete operator. What is free() Function in C++? In...

4 minutes read.

Decimal to Octal in C++

We must create a software that converts a decimal number into an equal octal number given a decimal number as input i.e. convert a number having a base value of...

3 minutes read.

Compile Time Polymorphism in C++

What is Polymorphism? Polymorphism refers to the existence of various forms. Polymorphism can be simply defined as a message's capacity to be presented in multiple forms. One application of polymorphism in...

4 minutes read.

C++ Convert Int to String

In fact, the conversion of numbers to strings or vice versa represents a significant paradigm change. We often need to convert a number to a string or a string to...

2 minutes read.

Features of OOPS in C++

What is OOPs? The main reason programmers prefer C ++ language over C is because of the support of object-oriented programing in C++. As the name suggests, object-oriented programming or OOPs...

3 minutes read.

C++ Void Pointer

A void pointer is a general purpose pointer that can have an address of any data type but is not related to any data type. Void Pointer Syntax: void *ptr;  We can't...

2 minutes read.

Decimal to Binary in C++

What is the meaning of Decimal Numbers? Decimal numbers range from 0 to 9, there are a total of ten digits between 0 and 9. Any number with more than two...

3 minutes read.

How to Use Getline in C++

What is getline in C++? Similar to the cin function, which allows the programmer to take input from the user getline() function also takes input from the user. But in this...

4 minutes read.

C++ array to function

Arrays in C++ : Instead of defining distinct variables for each item, arrays are used to hold numerous values in a single variable. An array can be declared by specifying the variable...

4 minutes read.

C++ Iterators

What are iterators ? Iterators are among the four foundations of the C++ Standard Template Library, also known as the STL. The memory address of the STL container classes is pointed...

15 minutes read.

C++ References

C++ References An alias is a reference variable that is another name for a variable already in existence. If a relation is initialized with a variable, it is possible to use...

3 minutes read.

Name Mangling and extern in C++

Name Mangling and Function Overloading: Function overloading is a feature offered by C++. As long as each function accepts various parameters, we can use this to write many functions with the...

4 minutes read.

Static keyword in C++ vs Java

Both in C++ and Java, the static keyword is employed for essentially the same function. But there are some variations. The static keyword's similarities and differences between C++ and Java...

3 minutes read.

Classes and Objects in C++

When it comes to object-oriented programming, objects are the basic building blocks. Memory is taken up by objects, which contain data and methods or functions that operate on it. On...

3 minutes read.

C++ History

C++ is a middle-level programming language developed in 1980s by Bjarne Stroustrup at Bel Labs. C ++ development initially started in 1979, four years before its launch. It is started with...

1 minute read.

Observer Design Pattern in C++

The Observer design pattern is a behavioural design pattern that allows an object (known as the subject) to notify other objects (known as observers) when its state changes. This is...

3 minutes read.

C++ Close file

C++ File Handling close() Function A file which is opened while reading or writing in file handling must be closed after performing an action on it. The close() function is used to close...

2 minutes read.

How to enter a name in C++

A name is a string or array of characters or letters. The string is one of the most helpful data types offered by the C++ library. A string helps the...

4 minutes read.