C++ Naming Conventions: 1 Class Names
C++ Naming Conventions: 1 Class Names
C++ Naming Conventions: 1 Class Names
Names are the key to program readability. If the name is appropriate everything fits together natu-
rally, relationships are clear, meaning is derivable, and reasoning from common human expectations
works as expected. Good names save time when debugging and save time when extending.
If you find all your names could be Thing and DoIt then you should probably revisit your design.
Avoid the temptation to have short names everywhere, and avoid non-standard abbreviations.
Source code is meant to be read by humans. This is the most important thing to
remember. As well as communicating your intent to the machine, you must make it clear what
that intent is to those who will read the source code. This includes you! Code you’ve written more
than about 3 weeks ago may as well have been written by somebody else.
A cryptic example:
Dog d
Lion l
l.dvr(d)
Dog myPetDog
Lion escapedLion
escapedLion.devour(myPetDog)
1 Class Names
Name the class after what it is. If you can’t think of what it is that is a clue you have not thought
through the design well enough.
• Use upper case letters as word separators, lower case for the rest of a word
class OdeSolver
class ParameterFile
1
2 Method and Function Names
Usually every method and function performs an action, so the name should make clear what it does:
CheckForErrors() instead of ErrorCheck(), DumpDataToFile() instead of DataFile(). This will also
make functions and data objects more distinguishable. Each method/function should begin with a
verb.
• Classes are often nouns. By making function names verbs and following other naming con-
ventions programs can be read more naturally.
For example: RetryMax to mean the maximum number of retries, RetryCount to mean the
current retry count.
– is/has - to ask a question about something. Whenever someone sees Is or Has they will
know it’s a question.
– get - get a value.
– set - set a value.
class OdeSolver
{
public:
int SolveEquation();
void HandleError();
}
2
4 Pointer Variables
Pointers should be prepended by a ’p’ in most cases. Place the * close to the variable name rather
than the pointer type.
• After the ’m’ use the same rules as for class names.
• ’m’ always precedes other name modifiers like ’p’ for pointer.
class CleaningDepartment{
public:
int ComputeErrorNumber();
private:
int mCleanHouse;
int mErrorNumber;
String *mpName;
}
class Test{
public:
void TestConveyorStart(StatusInfo& rStatus);
private:
StatusInfo& mrStatus;
}
3
7 Method Argument Names
The first character should be lower case. All word beginnings after the first letter should be upper
case as with class names.
class WackyRace{
public:
int StartYourEngines( Engine& rSomeEngine,
Engine anotherEngine);
}
With this approach the scope of the variable is clear in the code. And now all variables look
different and are identifiable in the code.
9 Global Constants
Global constants should be all caps with ’ ’ separators.
10 Static Variables
Static variables should be prepended with ’s’.
private:
static StatusInfo msStatus;