GoogleC++StyleGuide之命名规则通⽤命名规则:
函数名,变量名以及⽂件名应该是⾃描述的,应避免使⽤缩写。类型和变量应使⽤名词,⽽函数应包含动词。遵章守纪心得体会
int num_errors; // Good.
int num_completed_connections; // Good.
工商注册号查询
int n; // Bad - meaningless.
int nerr; // Bad - ambiguous abbreviation.
int n_comp_conns; // Bad - ambiguous abbreviation.
避免使⽤缩写,除⾮它们在你的项⽬外⾮常的通⽤。
// Good
// The show proper names with no abbreviations.
int num_dns_connections; // Most people know what "DNS" stands for.
int price_count_reader; // OK, price count. Makes n.
// Bad!
// Abbreviations can be confusing or ambiguous outside a small group.
int wgc_connections; // Only your group knows what this stands for.
int pc_reader; // Lots of things can be abbreviated "pc".
int error_count; // Good.
int error_cnt; // Bad.
⽂件名:
⽂件名应该包含⼩写字母以及下划线(_)或连字符(-)。建议使⽤下划线。
以下是可以接受的⽂件名:
my_
<
<
// _unittest and _regtest are deprecated.
内联函数(Inline function)必须包括在.h⽂件中。
如果你的内联函数很⼩,你应该直接写到.h⽂件中。
如果你的内联函数包括⼀定量的代码,则它们应该写到以-inl.h结尾的第三⽅⽂件中。
如果你的类有很多的内联函数,则你的类应该有三个⽂件:
url_table.h // The class declaration.
// The class definition.
url_table-inl.h // Inline functions that include lots of code.
类型名:
华硕路由器设置>奶油如何打发
家访过程类型名(包括:类,结构体,类型定义,枚举)以⼤写字母开头且每个新单词都以⼤写字母开头,不包括下划线。
// class and structs
class UrlTable { ...
class UrlTableTester { ...
struct UrlTableProperties { ...
// typedefs
typedef hash_map<UrlTableProperties *, string> PropertiesMap;
// enums
enum UrlTableErrors { ...
变量名:
变量名只包含⼩写字母并以下划线分隔每个单词。类成员变量应该以下划线结尾。
通⽤变量名:
string table_name; // OK - us underscore.
string tablename; // OK - all lowerca.
string tableName; // Bad - mixed ca.
类成员变量:
string table_name_; // OK - underscore at end.
string tablename_; // OK.
结构体成员变量:为了与类成员变量区分,结构体成员变量不以下划线结尾。
struct UrlTableProperties {
string name;
int num_entries;
}
全局变量:与通⽤变量命名相同,以g_作为前缀。
常量名:
使⽤k后⾯每个单词的⾸字母⼤写
const int kDaysInAWeek = 7;
函数名:
通⽤的函数名使⽤每个单词⾸字母⼤写的⽅式,对类成员变量存取的函数与类成员变量名匹配:MyExcitingFunction(), MyExcitingMethod(), my_exciting_member_variable(),
潇湘招考t_my_exciting_member_variable().
通⽤函数名:
AddTableEntry()
DeleteUrl()
OpenFileOrDie()
存取变量函数名:
class MyClass {
public:
...
int num_entries() const { return num_entries_; }
阅读小报简单又漂亮void t_num_entries(int num_entries) { num_entries_ = num_entries; }
private:
int num_entries_;
};
命名空间:
金堂云顶山命名空间应都是⼩写字母且基于项⽬名称以及存放路径。
枚举命名:
枚举应于常量或宏定义使⽤相同的命名⽅式,即kEnumName或ENUM_NAME
enum UrlTableErrors {
kOK = 0,
kErrorOutOfMemory,
kErrorMalformedInput,
};
enum AlternateUrlTableErrors {
OK = 0,
OUT_OF_MEMORY = 1,
MALFORMED_INPUT = 2,
};
宏命名:
通常情况下宏不应被使⽤。如果真正需要时,要以全部⼤写以及下划线进⾏命名。 #define ROUND(x) ...
#define PI_ROUNDED 3.0