1、一般性规则
函数名、变量名、文件名应当是描述性的,避免使用缩写词。
2、文件名
文件名应当全部小写,可以使用下划线'_'或者破折号'-',优先使用下划线。
C++文件应当以.cc结尾
3、类型名
类型名(包括classes、structs、typedefs、enums)以大写字母开头,每个单词首字母大写,不要使用下划线。
4、变量名
变量名全部使用小写,单词与单词之间使用下划线。类的成员变量使用trailing underscores。举例:
Common Variable names
For example:
string table_name; // OK - uses underscore. string tablename; // OK - all lowercase.
string tableName; // Bad - mixed case.
Class Data Members
Data members (also called instance variables or member variables) are lowercase with optional underscores like regular variable names, but always end with a trailing underscore.
string table_name_; // OK - underscore at end. string tablename_; // OK.Struct Variables
Data members in structs should be named like regular variables without the trailing underscores that data members in classes have.
struct UrlTableProperties { string name; int num_entries; }See Structs vs. Classes for a discussion of when to use a struct versus a class.
Global Variables
There are no special requirements for global variables, which should be rare in any case, but if you use one, consider prefixing it with
g_
or some other marker to easily distinguish it from local variables.
以字母'k'开头,后接首字母大写的大小写混合单词。如:kDaysInAWeek.
6、函数名
常规函数名:常规函数名应该首字母大写,每个单词以大写字母开头,不使用下划线。如
AddTableEntry() DeleteUrl() OpenFileOrDie()
Accessors and Mutators
Accessors and mutators (get and set functions) should match the name of the variable they are getting and setting. This shows an excerpt of a class whose instance variable is
num_entries_
.class MyClass { public: ... int num_entries() const { return num_entries_; } void set_num_entries(int num_entries) { num_entries_ = num_entries; } private: int num_entries_; };You may also use lowercase letters for other very short inlined functions. For example if a function were so cheap you would not cache the value if you were calling it in a loop, then lowercase naming would be acceptable.
7、枚举命名:
像consts或macros一样命名。尽量像consts。