[Home]CppCodingStandards/CppNamingConventions

TheSourcery | CppCodingStandards | RecentChanges | Preferences | Index | RSS

Difference (from prior minor revision) (major diff)

Changed: 1,293c1,297

Naming Conventions




Names should be readable and self-documenting. Abbreviations and contractions are discouraged, unless they follow common usage within the domain. All names should begin with a letter. Individual words in compound names are differentiated by capitalizing the first letter of each word or by separating with an underscore. The use of special characters other than letters, digits, or underscores is discouraged. The first 31 characters of a name (including the prefix) should be unique and the uniqueness must not be due solely to a difference in case.

The following conventions are followed depending on the context of the identifier you are naming:


Convention 1



Individual words within compound names are differentiated by capitalizing the first letter of each word.

Example:


FindFile
ReadDatabaseHeader
PriorityQueue



Convention 2



Individual words within compound names are differentiated by separating words using underscores and lower case only.

Example:


find_file
read_database_header
priority_queue



Convention 3



Individual words within compound names are differentiated by separating words using underscores and upper case only.

Example:


FIND_FILE
READ_DATABASE_HEADER
PRIORITY_QUEUE


Abbreviations:

When confronted with a situation where you could use an all upper case abbreviation in [Convention 1], use an initial upper case letter followed by all lower case letters.

Example:


WeightOz not WeightOZ


Use suffixes consistently:

* Min, _min, or _MIN - to mean the minimum value something can have.
* Max, _max, or _MAX - to mean the maximum value something can have.
* Cnt, _cnt, or _CNT - the current count of a running count variable.
* Key, _key, or _KEY - a key value.
* Avg, _avg, or _AVG - an average value.
* Total, _total, or _TOTAL - a total value.

Example:


RecordCnt
population_avg


Use prefixes consistently:

* Is, is_, or IS_ - to ask a question about something.
* Get, get_, or GET_ - get a value.
* Set, set_, or SET_ - set a value.

Examples:


IsRetryLimit
get_record


The following opposites should be used consistently:


|| add/remove || begin/end || create/destroy ||
|| insert/delete || first/last || acquire/release ||
|| increment/decrement || put/get || up/down ||
|| lock/unlock || get/set || next/previous ||
|| old/new || open/close || show/hide ||
|| source/destination || source/target || start/stop ||

The following special purpose prefixes and suffixes are used:

|| p or p_ || pointer prefix ||
|| m || class attribute prefix ||
|| s || static class attribute prefix ||
|| r or r_ || reference prefix ||
|| g_ || global variable prefix ||
|| _T || typedef suffix ||
|| _E || enumerated type suffix ||
|| _C || constant variable suffix ||

Class Names




Class names will be assigned using [Convention 1]. 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. Compound names of over three words are a clue the design may be confusing various entities in your system. Avoid the temptation of bringing the name of the class a class derives from into the derived class's name. A class should stand on its own. It doesn't matter what it derives from. Suffixes are sometimes helpful. For example, if the system uses agents then naming something <nop>DownloadAgent? conveys real information.

Example:


class WorkQueue;

Class Library Names




Generally namespaces will be used to prevent class name conflicts when using external libraries from different vendors or sources. In many cases, instead of using namespaces, class name collisions will be prevented by assigning a two-character upper case prefix to library wrappers. This convention also follows common java package naming. This is an exception to the all caps abbreviation rule stated earlier.

Example:


class TMString {
}

Class Attribute Names




Attribute names will assigned using [Convention 1]. They will be prepended with the lower case character 'm'. The 'm' prefix modifier will always precede other name modifiers, such as 'p' for pointer.

Example:


class Car {
private:
int mSpeed;
TMString* mpName;
}

Static Attributes




Static attribute names will be assigned using [Convention 1]. They will be prepended with the lower case character 's'. The 's' prefix modifier will always precede other name modifiers, such as 'p' for pointer.

Example:


class TestFile {
public:
private:
static int sStatus;
static char* spFileName;
}

Class Method Names




Method names will be assigned using [Convention 1]. Procedural type methods will use strong verb-nouns combinations describing the action performed. Functional type methods will be descriptive of the return value. The name should describe everything the method does, so wishy-washy verbs like 'process' should be avoided.

Example:


class Socket {
public:
void HandleError();
}

Function Names




Function names will be assigned using [Convention 2]. Procedural type functions will use strong verb-nouns combinations describing the action performed. Functional type functions will be descriptive of the return value. The name should describe everything the function does, so wishy-washy verbs like 'process' should be avoided.

Example:


void handle_error(int error_number);
float todays_date();

Argument Names




Function or method argument names will be assigned using [Convention 2].

Example:


class Train {
public:
int StartEngines( Engine& engine, int operator_badge_id);
}

Variable Names on the Stack




Local variables will be assigned using [Convention 2]. Boolean names should imply true or false (done, error, found, success) and should be positive (not contain 'not'). Variable names should be descriptive nouns showing what the data represents, not how it was derived. Put derived qualifiers as suffixes (such as _max, _avg, _min, _total).

Example:


int record_cnt;
char* p_character_name;
float response_time;

Pointer Variables




Pointers should be prepended by a 'p' in most cases. Place the '*' operator close to the pointer type not the variable name.

Example:


String* pName = new String;
char* pName;

Reference Variables and Functions Returning References




References variables, arguments, or functions returning references should be prepended with 'r' or 'r_'.

Example:


class Test {
public:
void DoSomething(StatusInfo& r_status);
StatusInfo& rStatus();
private:
StatusInfo& mrStatus;
}

Global Variables




Global variables should be prepended with a 'g_'. The 'g_' prefix modifier will always precede other name modifiers, such as 'p' for pointer.

Example:


String* gp_log;
int g_record_cnt;

Global Constants




Global constants names will be assigned using [Convention 3]. Global constants are also appended with the '_C' suffix.

Example:


const int SLEEP_TIME_C = 5;

Type Names




Type names will be assigned using [Convention 1]. When possible, native types will be accessed via typedefs. Typedef names will be appended with the '_T' suffix.

Example:


typedef short Int16_T;
typedef unsigned int SystemTime_T;

#define and Macro Names




Pre-processor #defines and macro names will be assigned using [Convention 3].

Example:


#define STRING_LIMIT 512
#define IS_ERR(err) definition

Enum Names




Enumerated type names will be assigned using [Convention 1]. Individual enumerated value names will be assigned using [Convention 3]. Value names will have the '_E' suffix appended and will be preprended with a common prefix (i.e. COLOR, FONT, etc. ).

Example:


enum PinState {
PIN_OFF_E,
PIN_ON_E
}


The first enumerated value will be reserved for the default or error state.

Example:


enum { STATE_ERR_E, STATE_OPEN_E, STATE_RUNNING_E, STATE_DYING_E };



Naming Conventions




Names should be readable and self-documenting. Abbreviations and contractions are discouraged, unless they follow common usage within the domain. All names should begin with a letter. Individual words in compound names are differentiated by capitalizing the first letter of each word or by separating with an underscore. The use of special characters other than letters, digits, or underscores is discouraged. The first 31 characters of a name (including the prefix) should be unique and the uniqueness must not be due solely to a difference in case.

The following conventions are followed depending on the context of the identifier you are naming:


Convention 1




Individual words within compound names are differentiated by capitalizing the first letter of each word.

Example:


FindFile
ReadDatabaseHeader
PriorityQueue



Convention 2




Individual words within compound names are differentiated by separating words using underscores and lower case only.

Example:


find_file
read_database_header
priority_queue




Convention 3



Individual words within compound names are differentiated by separating words using underscores and upper case only.

Example:


FIND_FILE
READ_DATABASE_HEADER
PRIORITY_QUEUE


Abbreviations:

When confronted with a situation where you could use an all upper case abbreviation in [Convention 1], use an initial upper case letter followed by all lower case letters.

Example:


WeightOz not WeightOZ


Use suffixes consistently:

* Min, _min, or _MIN - to mean the minimum value something can have.
* Max, _max, or _MAX - to mean the maximum value something can have.
* Cnt, _cnt, or _CNT - the current count of a running count variable.
* Key, _key, or _KEY - a key value.
* Avg, _avg, or _AVG - an average value.
* Total, _total, or _TOTAL - a total value.

Example:


RecordCnt
population_avg


Use prefixes consistently:

* Is, is_, or IS_ - to ask a question about something.
* Get, get_, or GET_ - get a value.
* Set, set_, or SET_ - set a value.

Examples:


IsRetryLimit
get_record


The following opposites should be used consistently:


|| add/remove || begin/end || create/destroy ||
|| insert/delete || first/last || acquire/release ||
|| increment/decrement || put/get || up/down ||
|| lock/unlock || get/set || next/previous ||
|| old/new || open/close || show/hide ||
|| source/destination || source/target || start/stop ||

The following special purpose prefixes and suffixes are used:

|| p or p_ || pointer prefix ||
|| m || class attribute prefix ||
|| s || static class attribute prefix ||
|| r or r_ || reference prefix ||
|| g_ || global variable prefix ||
|| _T || typedef suffix ||
|| _E || enumerated type suffix ||
|| _C || constant variable suffix ||

Class Names




Class names will be assigned using [Convention 1]. 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. Compound names of over three words are a clue the design may be confusing various entities in your system. Avoid the temptation of bringing the name of the class a class derives from into the derived class's name. A class should stand on its own. It doesn't matter what it derives from. Suffixes are sometimes helpful. For example, if the system uses agents then naming something <nop>DownloadAgent? conveys real information.

Example:


class WorkQueue;

Class Library Names




Generally namespaces will be used to prevent class name conflicts when using external libraries from different vendors or sources. In many cases, instead of using namespaces, class name collisions will be prevented by assigning a two-character upper case prefix to library wrappers. This convention also follows common java package naming. This is an exception to the all caps abbreviation rule stated earlier.

Example:


class TMString {
}

Class Attribute Names




Attribute names will assigned using [Convention 1]. They will be prepended with the lower case character 'm'. The 'm' prefix modifier will always precede other name modifiers, such as 'p' for pointer.

Example:


class Car {
private:
int mSpeed;
TMString* mpName;
}

Static Attributes




Static attribute names will be assigned using [Convention 1]. They will be prepended with the lower case character 's'. The 's' prefix modifier will always precede other name modifiers, such as 'p' for pointer.

Example:


class TestFile {
public:
private:
static int sStatus;
static char* spFileName;
}

Class Method Names




Method names will be assigned using [Convention 1]. Procedural type methods will use strong verb-nouns combinations describing the action performed. Functional type methods will be descriptive of the return value. The name should describe everything the method does, so wishy-washy verbs like 'process' should be avoided.

Example:


class Socket {
public:
void HandleError();
}

Function Names




Function names will be assigned using [Convention 2]. Procedural type functions will use strong verb-nouns combinations describing the action performed. Functional type functions will be descriptive of the return value. The name should describe everything the function does, so wishy-washy verbs like 'process' should be avoided.

Example:


void handle_error(int error_number);
float todays_date();

Argument Names




Function or method argument names will be assigned using [Convention 2].

Example:


class Train {
public:
int StartEngines( Engine& engine, int operator_badge_id);
}

Variable Names on the Stack




Local variables will be assigned using [Convention 2]. Boolean names should imply true or false (done, error, found, success) and should be positive (not contain 'not'). Variable names should be descriptive nouns showing what the data represents, not how it was derived. Put derived qualifiers as suffixes (such as _max, _avg, _min, _total).

Example:


int record_cnt;
char* p_character_name;
float response_time;

Pointer Variables




Pointers should be prepended by a 'p' in most cases. Place the '*' operator close to the pointer type not the variable name.

Example:


String* pName = new String;
char* pName;

Reference Variables and Functions Returning References




References variables, arguments, or functions returning references should be prepended with 'r' or 'r_'.

Example:


class Test {
public:
void DoSomething(StatusInfo& r_status);
StatusInfo& rStatus();
private:
StatusInfo& mrStatus;
}

Global Variables




Global variables should be prepended with a 'g_'. The 'g_' prefix modifier will always precede other name modifiers, such as 'p' for pointer.

Example:


String* gp_log;
int g_record_cnt;

Global Constants




Global constants names will be assigned using [Convention 3]. Global constants are also appended with the '_C' suffix.

Example:


const int SLEEP_TIME_C = 5;

Type Names




Type names will be assigned using [Convention 1]. When possible, native types will be accessed via typedefs. Typedef names will be appended with the '_T' suffix.

Example:


typedef short Int16_T;
typedef unsigned int SystemTime_T;

#define and Macro Names




Pre-processor #defines and macro names will be assigned using [Convention 3].


Example:


#define STRING_LIMIT 512
#define IS_ERR(err) definition

Enum Names




Enumerated type names will be assigned using [Convention 1]. Individual enumerated value names will be assigned using [Convention 3]. Value names will have the '_E' suffix appended and will be preprended with a common prefix (i.e. COLOR, FONT, etc. ).

Example:


enum PinState {
PIN_OFF_E,
PIN_ON_E
}


The first enumerated value will be reserved for the default or error state.

Example:


enum { STATE_ERR_E, STATE_OPEN_E, STATE_RUNNING_E, STATE_DYING_E };




Naming Conventions

Names should be readable and self-documenting. Abbreviations and contractions are discouraged, unless they follow common usage within the domain. All names should begin with a letter. Individual words in compound names are differentiated by capitalizing the first letter of each word or by separating with an underscore. The use of special characters other than letters, digits, or underscores is discouraged. The first 31 characters of a name (including the prefix) should be unique and the uniqueness must not be due solely to a difference in case.

The following conventions are followed depending on the context of the identifier you are naming:

Convention 1

Individual words within compound names are differentiated by capitalizing the first letter of each word.

Example:

  FindFile
  ReadDatabaseHeader
  PriorityQueue

Convention 2

Individual words within compound names are differentiated by separating words using underscores and lower case only.

Example:

  find_file
  read_database_header
  priority_queue

Convention 3

Individual words within compound names are differentiated by separating words using underscores and upper case only.

Example:

  FIND_FILE
  READ_DATABASE_HEADER
  PRIORITY_QUEUE

Abbreviations:

When confronted with a situation where you could use an all upper case abbreviation in [Convention 1], use an initial upper case letter followed by all lower case letters.

Example:

  WeightOz not WeightOZ

Use suffixes consistently:

Example:

  RecordCnt
  population_avg

Use prefixes consistently:

Examples:

  IsRetryLimit
  get_record

The following opposites should be used consistently:

add/remove begin/end create/destroy
insert/delete first/last acquire/release
increment/decrement put/get up/down
lock/unlock get/set next/previous
old/new open/close show/hide
source/destination source/target start/stop

The following special purpose prefixes and suffixes are used:

p or p_ pointer prefix
m class attribute prefix
s static class attribute prefix
r or r_ reference prefix
g_ global variable prefix
_T typedef suffix
_E enumerated type suffix
_C constant variable suffix

Class Names

Class names will be assigned using [Convention 1]. 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. Compound names of over three words are a clue the design may be confusing various entities in your system. Avoid the temptation of bringing the name of the class a class derives from into the derived class's name. A class should stand on its own. It doesn't matter what it derives from. Suffixes are sometimes helpful. For example, if the system uses agents then naming something <nop>DownloadAgent? conveys real information.

Example:

  class WorkQueue;

Class Library Names

Generally namespaces will be used to prevent class name conflicts when using external libraries from different vendors or sources. In many cases, instead of using namespaces, class name collisions will be prevented by assigning a two-character upper case prefix to library wrappers. This convention also follows common java package naming. This is an exception to the all caps abbreviation rule stated earlier.

Example:

  class TMString {
  }

Class Attribute Names

Attribute names will assigned using [Convention 1]. They will be prepended with the lower case character 'm'. The 'm' prefix modifier will always precede other name modifiers, such as 'p' for pointer.

Example:

  class Car {
  private:
    int        mSpeed;
    TMString*  mpName;
  }

Static Attributes

Static attribute names will be assigned using [Convention 1]. They will be prepended with the lower case character 's'. The 's' prefix modifier will always precede other name modifiers, such as 'p' for pointer.

Example:

  class TestFile {
  public:
  private:
    static int   sStatus;
    static char* spFileName;
  }

Class Method Names

Method names will be assigned using [Convention 1]. Procedural type methods will use strong verb-nouns combinations describing the action performed. Functional type methods will be descriptive of the return value. The name should describe everything the method does, so wishy-washy verbs like 'process' should be avoided.

Example:

  class Socket {
  public:
    void   HandleError();
  }

Function Names

Function names will be assigned using [Convention 2]. Procedural type functions will use strong verb-nouns combinations describing the action performed. Functional type functions will be descriptive of the return value. The name should describe everything the function does, so wishy-washy verbs like 'process' should be avoided.

Example:

  void   handle_error(int error_number);
  float  todays_date();

Argument Names

Function or method argument names will be assigned using [Convention 2].

Example:

  class Train {
  public:
    int StartEngines( Engine& engine, int operator_badge_id);
  }

Variable Names on the Stack

Local variables will be assigned using [Convention 2]. Boolean names should imply true or false (done, error, found, success) and should be positive (not contain 'not'). Variable names should be descriptive nouns showing what the data represents, not how it was derived. Put derived qualifiers as suffixes (such as _max, _avg, _min, _total).

Example:

  int    record_cnt;
  char*  p_character_name;
  float  response_time;

Pointer Variables

Pointers should be prepended by a 'p' in most cases. Place the '*' operator close to the pointer type not the variable name.

Example:

  String*  pName = new String;
  char*    pName;

Reference Variables and Functions Returning References

References variables, arguments, or functions returning references should be prepended with 'r' or 'r_'.

Example:

  class Test {
  public:
    void               DoSomething(StatusInfo& r_status);
    StatusInfo&        rStatus();
  private:
    StatusInfo&        mrStatus;
  }

Global Variables

Global variables should be prepended with a 'g_'. The 'g_' prefix modifier will always precede other name modifiers, such as 'p' for pointer.

Example:

  String*   gp_log;
  int       g_record_cnt;

Global Constants

Global constants names will be assigned using [Convention 3]. Global constants are also appended with the '_C' suffix.

Example:

  const int   SLEEP_TIME_C = 5;

Type Names

Type names will be assigned using [Convention 1]. When possible, native types will be accessed via typedefs. Typedef names will be appended with the '_T' suffix.

Example:

  typedef short          Int16_T;
  typedef unsigned int   SystemTime_T;

#define and Macro Names

Pre-processor #defines and macro names will be assigned using [Convention 3].

Example:

  #define STRING_LIMIT   512
  #define IS_ERR(err)    definition

Enum Names

Enumerated type names will be assigned using [Convention 1]. Individual enumerated value names will be assigned using [Convention 3]. Value names will have the '_E' suffix appended and will be preprended with a common prefix (i.e. COLOR, FONT, etc. ).

Example:

  enum PinState {
    PIN_OFF_E,
    PIN_ON_E
  }

The first enumerated value will be reserved for the default or error state.

Example:

  enum { STATE_ERR_E, STATE_OPEN_E, STATE_RUNNING_E, STATE_DYING_E };


TheSourcery | CppCodingStandards | RecentChanges | Preferences | Index | RSS
Edit text of this page | View other revisions
Last edited September 15, 2005 8:47 pm by JonLambert (diff)
Search:
All material on this Wiki is the property of the contributing authors.
©2004-2006 by the contributing authors.
Ideas, requests, problems regarding this site? Send feedback.