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:
Individual words within compound names are differentiated by capitalizing the first letter of each word.
Example:
FindFile ReadDatabaseHeader PriorityQueue
Individual words within compound names are differentiated by separating words using underscores and lower case only.
Example:
find_file read_database_header priority_queue
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 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;
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 { }
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 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; }
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 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();
Function or method argument names will be assigned using [Convention 2].
Example:
class Train { public: int StartEngines( Engine& engine, int operator_badge_id); }
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;
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;
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 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 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 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;
Pre-processor #defines and macro names will be assigned using [Convention 3].
Example:
#define STRING_LIMIT 512 #define IS_ERR(err) definition
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 };