VIII Comments
1.注释风格
可以使用//或/* */,但是要保持一致。
2.文件注释
每个文件头部应该有注释说明当前文件的用途,但是不要在.h文件和.cc文件中重复注释。
3.类注释
每个类定义应有注释说明类的用途和用法,如果类注释与文件注释内容相同,则只需在类注释中指向文件注释。
4.函数注释
每一个函数声明都要有注释,对函数的用途、参数和返回值进行说明。
5.变量注释
对变量的用途进行说明。如果变量可能取一些特殊值来表述特殊的状态,也应该进行说明。
6.Implementation Comments
对复杂的或使用了trick的代码进行说明。
注释应该说明代码“做什么”而不是“怎么做”。
行注释范例:
|
DoSomething(); // Comment here so the comments line up. DoSomethingElseThatIsLonger(); // Comment here so there are two spaces between // the code and the comment. { // One space before comment when opening a new scope is allowed, // thus the comment lines up with the following comments and code. DoSomethingElse(); // Two spaces before line comments normally. } |
当你向一个函数传入空指针、布尔值或者字面常量时,你应该对相应的参数进行说明,或者使用常量使得代码self-documenting。
|
bool success = CalculateSomething(interesting_value, 10, // Default base value. false, // Not the first time we're calling this. NULL); // No callback.
const int kDefaultBaseValue = 10; const bool kFirstTimeCalling = false; Callback *null_callback = NULL; bool success = CalculateSomething(interesting_value, kDefaultBaseValue, kFirstTimeCalling, null_callback); |
7.注释的文法
注释应该是规范的、完整的句子。
8.TODO Comments
TODO注释后面应该标明你的姓名、邮件或其它标识。注释中应该明确指明要做的事情及实施日期。
|
// TODO(kl@gmail.com): Use a "*" here for concatenation operator. // TODO(Zeke) change this to use relations. |
9.Deprecation Comments
对过时的接口使用DEPRECATED进行注释。DEPRECATED后面应紧跟一个括号注明你的姓名、邮件或其它标识。
IX Formatting
1.代码行长度
每行不超过80个字符。
2.非ASCII字符
尽量避免使用非ASCII字符,文件编码必须使用UTF-8。
3.Spaces vs. Tabs
使用2个空格进行缩进,不使用TAB。
4.函数声明和定义
有如下规则:
-
返回类型和函数名在同一行。
-
左小括号和函数名在同一行。
-
函数名和括号之间没有空格。
-
括号和参数之间没有空格。
-
左大括号和最后一个参数在同一行。
-
右大括号独占一行或者(在某些特殊规则下)与左大括号在同一行。
-
右小括号和左大括号之间有一个空格。
-
所有参数都要写出参数名,并且名称在声明和定义中要保持一致。
例:
|
ReturnType ClassName::FunctionName(Type par_name1, Type par_name2) { DoSomething(); ... }
ReturnType ClassName::ReallyLongFunctionName(Type par_name1, Type par_name2, Type par_name3) { DoSomething(); ... }
ReturnType LongClassName::ReallyReallyReallyLongFunctionName( Type par_name1, // 4 space indent Type par_name2, Type par_name3) { DoSomething(); // 2 space indent ... } |
5.函数调用
与函数声明和定义的规则相似。
6.分支
示例:
|
if (condition) { // no spaces inside parentheses ... // 2 space indent. } else if (...) { // The else goes on the same line as the closing brace. ... } else { ... } |
|
if (x == kFoo) return new Foo(); if (x == kBar) return new Bar(); |
7.循环和switch
Switch语句的每一个代码块应使用大括号。空循环体应该使用{}或continue。
|
switch (var) { case 0: { // 2 space indent ... // 4 space indent break; } case 1: { ... break; } default: { assert(false); } } |
|
while (condition) { // Repeat test until it returns false. } for (int i = 0; i < kSomeNumber; ++i) {} // Good - empty body. while (condition) continue; // Good - continue indicates no logic. while (condition); // Bad - looks like part of do/while loop. |
8.Pointer and Reference Expressions
|
// These are fine, space preceding. char *c; const string &str;
// These are fine, space following. char* c; // but remember to do "char* c, *d, *e, ...;"! const string& str; char * c; // Bad - spaces on both sides of * const string & str; // Bad - spaces on both sides of & |
9.布尔表达式
|
if (this_one_thing > this_other_thing && a_third_thing == a_fourth_thing && yet_another && last_one) { ... } |
10.返回值
只在必要时(如较长的表达式)才将返回值放在括号中。
11.Variable and Array Initialization
可以选择使用=或()。
|
int x = 3; int x(3); string name("Some Name"); string name = "Some Name"; |
12.Preprocessor Directives
#应该总是在行首。
|
// Good - directives at beginning of line if (lopsided_score) { #if DISASTER_PENDING // Correct -- Starts at beginning of line DropEverything(); # if NOTIFY // OK but not required -- Spaces after # NotifyClient(); # endif #endif BackToNormal(); } // Bad - indented directives if (lopsided_score) { #if DISASTER_PENDING // Wrong! The "#if" should be at beginning of line DropEverything(); #endif // Wrong! Do not indent "#endif" BackToNormal(); } |
13.Class Format
有如下规则:
-
基类名称与子类名称在同一行。
-
public:等关键字应缩进1个空格。
-
按public, protected, private的顺序进行定义。
14.Constructor Initializer Lists
|
// When it all fits on one line: MyClass::MyClass(int var) : some_var_(var), some_other_var_(var + 1) {} |
|
// When it requires multiple lines, indent 4 spaces, putting the colon on // the first initializer line: MyClass::MyClass(int var) : some_var_(var), // 4 space indent some_other_var_(var + 1) { // lined up ... DoSomething(); ... } |
15.Namespace Formatting
命名空间中的内容不缩进。
|
namespace {
void foo() { // Correct. No extra indentation within namespace. ... }
} // namespace |
16.Vertical Whitespace
不要滥用空行。
通常情况下,一屏能展示的代码越多越容易理解代码的流程,但是也需要适当使用空行增加可读性。
References:
1.Google C++ Style Guide. http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml
