Google C++ Style Guide学习笔记——命名
2013-10-01 12:53
447 查看
1、一般性规则
函数名、变量名、文件名应当是描述性的,避免使用缩写词。
2、文件名
文件名应当全部小写,可以使用下划线'_'或者破折号'-',优先使用下划线。
C++文件应当以.cc结尾
3、类型名
类型名(包括classes、structs、typedefs、enums)以大写字母开头,每个单词首字母大写,不要使用下划线。
4、变量名
变量名全部使用小写,单词与单词之间使用下划线。类的成员变量使用trailing underscores。举例:
Common Variable names
For example:
5、const 变量名
以字母'k'开头,后接首字母大写的大小写混合单词。如:kDaysInAWeek.
6、函数名
常规函数名:常规函数名应该首字母大写,每个单词以大写字母开头,不使用下划线。如
public:
...
int num_entries() const { return num_entries_; }
void set_num_entries(int num_entries) { num_entries_ = num_entries; }
private:
int num_entries_;
};[/code]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。
函数名、变量名、文件名应当是描述性的,避免使用缩写词。
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 MembersData 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 VariablesData 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 VariablesThere 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.
5、const 变量名
以字母'k'开头,后接首字母大写的大小写混合单词。如:kDaysInAWeek.
6、函数名
常规函数名:常规函数名应该首字母大写,每个单词以大写字母开头,不使用下划线。如
AddTableEntry() DeleteUrl() OpenFileOrDie()
Accessors and MutatorsAccessors 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 [code]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_;
};[/code]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。
相关文章推荐
- Google C++ Style Guide 阅读笔记 4
- Google C++ Style Guide 阅读笔记 系列
- Google C++ style guide——命名约定
- Google C++ Style Guide 阅读笔记 1
- Google C++ Style Guide 阅读笔记 3
- Google C++ Style Guide 阅读笔记 5
- Google C++ style guide——命名约定
- Google C++ Style Guide之命名规则
- Google C++Style Guide【C++编程风格指南解读】——命名约定
- Google C++ Style Guide之命名规则
- Google C++ Style Guide 笔记1
- Google C++ Style Guide学习心得
- Google C++ Style Guide 笔记
- Google C++ Style Guide 阅读笔记 2
- Google C++ Style Guide 阅读笔记 6
- Google C++ style guide
- Google C++ Style Guide的总结和摘抄
- Google C++ Style Guide的哲学
- Google C++ Style Guide
- Google C++ Style Guide 的Header File 部分 翻译