C代码中的命名方式总结和改进
- 宏全部使用大写字母--------宏大写
- 结构体名字全部使用typedef,typedef之后的名字为大写-------结构体别名大写
- 函数名全部使用小写字母,单词之间使用下划线分割-------函数名小写,单词之间下划线(1)
- 函数名第一个单词全小写,后面单词的首字母大写,单词之间使用下划线---------函数名首单词小写,后面单词首字母大写(2)
- 变量名也是全部使用小写,单词之间使用下划线分割-------变量名小写,单词之间下划线(1)
- 变量名第一个单词全小写,后面单词的首字母大写,单词之间使用下划线---------变量名单词小写,后面单词首字母大写(2)
变量的命名前后缀规则如下(这是匈牙利命名法的前后缀格式,以后其他地方使用上述的总结,前后缀使用匈牙利的):
The type prefix indicates the data type of the variable.
Type prefix | Meaning | Example |
---|---|---|
b | boolean | bool bHasEffect; |
c (or none*) | class | Creature cMonster; |
ch | char (used as a char) | char chLetterGrade; |
d | double, long double | double dPi; |
e | enum | Color eColor; |
f | float | float fPercent; |
n | short, int, long char used as an integer |
int nValue; |
s | struct | Rectangle sRect; |
str | C++ string | std::string strName; |
sz | Null-terminated string | char szName[20]; |
The following type modifiers are placed before the prefix if they apply:
Type modifier | Meaning | Example |
---|---|---|
a | array on stack | int anValue[10]; |
p | pointer | int* pnValue; |
pa | dynamic array | int* panValue = new int[10]; |
r | reference | int rnValue; |
u | unsigned | unsigned int unValue; |
The following scope modifiers are placed before the type modifier if they apply:
Scope modifier | Meaning | Example |
---|---|---|
g_ | global variable | int g_nGlobalValue; |
m_ | member of class | int m_nMemberValue; |
s_ | static member of class | int s_nValue; |
补充:
- p ---------for a pointer. A pfoo would be a pointer to data item of type FOO
- pp------- for a pointer to a pointer.
- h --------for a heap handle. This is a pointer to a pointer that points at a data item recorded within a heap.
- rg -------for an unstructured array containing data items of a certain data type. An rgfoo would be an array that contains data of type foo. Individual elements would be selected by an ifoo index variable.
- mp -------for an array which is used to map from one data type to another.Eg. A mpdochdod would be indexed via a doc index. The expression mpdochdod[doc] would produce a handle to a document descriptor.
- dn-------- for an array whose elements that describes a meaningful index such as an opcode. If the meaningful index were an OP type, a data structure of type EOP (entries for OP) would be defined, and a dnop array would be defined which describes what each individual op means
Max - added to an index data instance which records the actual size of an array or data block.
eg. the declaration of a an array of type FOO in C would be: FOO rgfoo[ifooMax];
Mac - added to an index data instance to indicate the limit of an actual usage within an array.Mac stands for current maximum. It is invariant that ifooMac <= ifooMax. ifooMac == ifooMax is the condition which is true when all entries within an array are in use.
First - added to an index or pointer which designates the first element within a range that may be validly processed
Last - added to an index or pointer which designates that last entry within a range that may be validly processed.
Lim - stands for limit. An ifooLim is equal to ifooLast + 1 and designates the location where a new foo item could be recorded in an array or data block.