6.1. 通用命名规则
int price_count_reader; // 无缩写
int num_errors; // “num” 本来就很常见
int num_dns_connections; // 人人都知道 “DNS” 是啥
int n; // 莫名其妙。
int nerr; // 怪缩写。
int n_comp_conns; // 怪缩写。
int wgc_connections; // 只有贵团队知道是啥意思。
int pc_reader; // "pc" 有太多可能的解释了。
int cstmr_id; // 有删减若干字母。
6.2. 文件命名
_
) 或连字符 (-
). 按项目约定来. 如果并没有项目约定,”_” 更好。* my_useful_class.cc
* my-useful-class.cc
* myusefulclass.cc
* muusefulclass_test.cc // ``_unittest`` 和 ``_regtest`` 已弃用。
.cc
结尾, 头文件以 .h
结尾. 专门插入文本的文件则以 .inc
结尾,参见 1.1. Self-contained 头文件。/usr/include
下的文件名 (Yang.Y 注: 即编译器搜索系统头文件的路径), 如 db.h
.http_server_logs.h
就比 logs.h
要好. 定义类时文件名一般成对出现, 如 foo_bar.h
和 foo_bar.cc
, 对应于类 FooBar
..h
文件中. 如果内联函数比较短, 就直接放在 .h
中.6.3. 类型命名
MyExcitingClass
, MyExcitingEnum
.typedef
), 枚举 —— 均使用相同约定. 例如:// classes and structs
class UrlTable { ...
class UrlTableTester { ...
struct UrlTableProperties { ...
// typedefs
typedef hash_map<UrlTableProperties *, string> PropertiesMap;
// enums
enum UrlTableErrors { ...
6.4. 变量命名
a_local_variable
, a_struct_data_member
, a_class_data_member_
.string table_name; // 可 - 用下划线。
string tablename; // 可 - 全小写。
string tableName; // 差 - 混合大小写。
class TableInfo {
...
private:
string table_name_; // 可 - 尾后加下划线。
string tablename_; // 可。
static Pool<TableInfo>* pool_; // 可。
};
struct UrlTableProperties {
string name;
int num_entries;
}
对全局变量没有特别要求, 少用就好, 但如果你要用, 可以用 g_
或其它标志作为前缀, 以便更好的区分局部变量.
6.5. 常量命名
k
: kDaysInAWeek. 且除去开头的 k
之外每个单词开头字母均大写。k
后接大写字母开头的单词:const int kDaysInAWeek = 7;
6.6. 函数命名
MyExcitingFunction()
, MyExcitingMethod()
, my_exciting_member_variable()
, set_my_exciting_member_variable()
.AddTableEntry()
DeleteUrl()
OpenFileOrDie()
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_;
};
6.7. 名字空间命名
google_awesome_project
.6.8. 枚举命名
kEnumName
或是 ENUM_NAME
.UrlTableErrors
(以及 AlternateUrlTableErrors
) 是类型, 所以要用大小写混合的方式.enum UrlTableErrors {
kOK = 0,
kErrorOutOfMemory,
kErrorMalformedInput,
};
enum AlternateUrlTableErrors {
OK = 0,
OUT_OF_MEMORY = 1,
MALFORMED_INPUT = 2,
};
6.9. 宏命名
MY_MACRO_THAT_SCARES_SMALL_CHILDREN
.#define ROUND(x) ...
#define PI_ROUNDED 3.0
6.10. 命名规则的特例
bigopen()
:函数名, 参照 open()
的形式
uint
:typedef
bigpos
:struct
或class
, 参照pos
的形式
sparse_hash_map
:STL 相似实体; 参照 STL 命名约定
LONGLONG_MAX
:常量, 如同 INT_MAX
译者(acgtyrant)笔记
TextQuery::TextQuery(std::string word) : word_(word) {}
, 其中 word_
自然是类内私有成员。原文始发于微信公众号(汇编语言):Google C++ 编程风格指南(六):命名约定
免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉。
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论