C++ Naming Conventions

What is Naming Conventions?

Names are the key to program readability for human. Good name shows clear relationships, meaning is understandable. Meaningful names save us time during debugging errors in C++ Language.

Class Names

Use upper case letters as word separators, lower case for the rest of a word, First character in a name must be upper case, and No underscores (’ ’) are permitted

For example:

class StoneWood
class PropertyBook

Class Attribute Names

Private attribute names should be prepended with the character ‘m’ and always precedes other name modifiers like ’p’ for pointer.

For example:

class CleaningDepartment{
private:
int mPrice;
int mAccountNumber;
String *mpColor;
}

Method and Function Names

Usually every method and function performs an action, so the name should make clear what it does:CheckForPrice() instead of PriceCheck(). Each method/function should begin with a verb.

Tip
Classes are often nouns. By making function names verbs and following other naming conventions programs can be read more naturally.

For example: 

class PropertyBook
{
public:
int SetPrice();
void TakeTax();
}

Global Constants

Global constants should be all caps with Underscore

For example:
const double TWO_PI = 6.28;


No All Upper Case Abbreviations

When confronted with a situation where you could use an all upper case abbreviation instead use an initial upper case letter followed by all lower case letters. No matter what.

Take for example DatabaseABCKey. Notice how the C from ABC and K from key are confused.

For example:
class DatabaseAbcKey // NOT DatabaseABCKey


Pointer Variables

Pointers should be prepended by a ’p’ in most cases. Place the * close to the variable name rather than the pointer type.

For example:
String *pTax, color, code; // note, only pTax is a pointer.


    Reference Variables and Functions Returning References

References should be prepended with ’r’.This establishes the difference between a method returning a modifiable object and the same method name returning a non-modifiable object.

For example:
class Test{
public:
void TestConveyorStart(StatusInfo& rStatus);
//returns a modifiable object so requires the ’r’ prefix
StatusInfo& rGetStatus();
//returns a constant (non-modifiable) object so doesn’t have the ’r’ prefix
const StatusInfo& GetStatus() const;
private:
StatusInfo& mrStatus;
}