Google C++ Style Guide之命名規則
通用命名規則:
函數名,變量名以及文件名應該是自描述的,應避免使用縮寫。類型和變量應使用名詞,而函數應包含動詞。
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
// These 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 sense.
// 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_useful_class.cc
my-useful-class.cc
myusefulclass.cc
myusefulclass_test.cc // _unittest and _regtest are deprecated.
內聯函數(Inline function)必須包括在.h文件中。
如果你的內聯函數很小,你應該直接寫到.h文件中。
如果你的內聯函數包括一定量的代碼,則它們應該寫到以-inl.h結尾的第三方文件中。
如果你的類有很多的內聯函數,則你的類應該有三個文件:
url_table.h // The class declaration.
url_table.cc // The class definition.
url_table-inl.h // Inline functions that include lots of code.
類型名:
類型名(包括:類,結構體,類型定義,枚舉)以大寫字母開頭且每個新單詞都以大寫字母開頭,不包括下劃線。
// classes and structs
class UrlTable { ...
class UrlTableTester { ...
struct UrlTableProperties { ...
// typedefs
typedef hash_map<UrlTableProperties *, string> PropertiesMap;
// enums
enum UrlTableErrors { ...
變量名:
變量名只包含小寫字母並以下劃線分隔每個單詞。類成員變量應該以下劃線結尾。
通用變量名:
string table_name; // OK - uses underscore.
string tablename; // OK - all lowercase.
string tableName; // Bad - mixed case.
類成員變量:
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(),
set_my_exciting_member_variable().
通用函數名:
AddTableEntry()
DeleteUrl()
OpenFileOrDie()
存取變量函數名:
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_;
};
命名空間:
命名空間應都是小寫字母且基於項目名稱以及存放路徑。
枚舉命名:
枚舉應於常量或宏定義使用相同的命名方式,即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
函數名,變量名以及文件名應該是自描述的,應避免使用縮寫。類型和變量應使用名詞,而函數應包含動詞。
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
// These 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 sense.
// 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_useful_class.cc
my-useful-class.cc
myusefulclass.cc
myusefulclass_test.cc // _unittest and _regtest are deprecated.
內聯函數(Inline function)必須包括在.h文件中。
如果你的內聯函數很小,你應該直接寫到.h文件中。
如果你的內聯函數包括一定量的代碼,則它們應該寫到以-inl.h結尾的第三方文件中。
如果你的類有很多的內聯函數,則你的類應該有三個文件:
url_table.h // The class declaration.
url_table.cc // The class definition.
url_table-inl.h // Inline functions that include lots of code.
類型名:
類型名(包括:類,結構體,類型定義,枚舉)以大寫字母開頭且每個新單詞都以大寫字母開頭,不包括下劃線。
// classes and structs
class UrlTable { ...
class UrlTableTester { ...
struct UrlTableProperties { ...
// typedefs
typedef hash_map<UrlTableProperties *, string> PropertiesMap;
// enums
enum UrlTableErrors { ...
變量名:
變量名只包含小寫字母並以下劃線分隔每個單詞。類成員變量應該以下劃線結尾。
通用變量名:
string table_name; // OK - uses underscore.
string tablename; // OK - all lowercase.
string tableName; // Bad - mixed case.
類成員變量:
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(),
set_my_exciting_member_variable().
通用函數名:
AddTableEntry()
DeleteUrl()
OpenFileOrDie()
存取變量函數名:
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_;
};
命名空間:
命名空間應都是小寫字母且基於項目名稱以及存放路徑。
枚舉命名:
枚舉應於常量或宏定義使用相同的命名方式,即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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.