if you leavedo not leave handle depencies 的英文缩写 谢了

1 Naming 命名规则
The mostimportant consistency rules are those that govern naming. The style of a nameimmediately informs us what sort of thing the named entity is: a type, avariable, a function, a constant, a macro, etc., without requiring us to searchfor the declaration
of that entity. The pattern-matching engine in our brainsrelies a great deal on these naming rules.
最重要的一致性规则是命名管理。命名的风格能立刻告诉我们这个名字代表的实体到底是什么东西:类型,变量,函数,常量,宏,等等,而不需要我们去寻找该实体的声明。我们头脑中的模式匹配引擎大量依赖于这些命名规则。
Naming rules arepretty arbitrary, but we feel that consistency is more important thanindividual preferences in this area, so regardless of whether you find themsensible or not, the rules are the rules.
命名规则是很随意的,但我们觉得这方面一致性要比个人喜好重要得多,所以不管你怎么想,规则就是规则。
通用命名规则
Function names,variable names, and filenames s eschew abbreviation. Typesand variables should be nouns, while functions should be &command&verbs.
函数名,变量名,和文件名应该是描述性的;不要过度缩写。类型名和变量名应该是名词,而函数名应该是命令式的动词。
How to Name 怎么命名
Give asdescriptive a name as possible, within reason. Do not worry about savinghorizontal space as it is far more important to make your code immediatelyunderstandable by a new reader. Examples of well-chosen names:
尽量有理由的起一个描述性的名字。别担心行空间的问题&,让你的代码能被新读者很快理解远远比这更重要。好的例子:
int num_&&&&&&&&&&&&&&&&& // Good.
int num_completed_&& // Good.
Poorly-chosennames use ambiguous abbreviations or arbitrary characters that do not conveymeaning:
不好的名字使用模棱两可的缩写或难以传达其含义的任意字符:
&&&&&&&&&&&&&&&&&&&&&&&&&& // Bad - meaningless.无含义
&&&&&&&&&&&&&&&&&&&&&&& // Bad - ambiguousabbreviation.模糊的缩写
int n_comp_&&&&&&&&&&&&&&& // Bad - ambiguousabbreviation.模糊的缩写
Type andvariable names should typically be nouns: e.g., FileOpener, num_errors.
类型名和变量名通常应该是名词:如FileOpener,num_errors。
Function namesshould typically be imperative (that is they should be commands): e.g.,OpenFile(), set_num_errors(). There is an exception for accessors, which,described more completely in Function Names, should be named the same as thevariable they access.
函数名通常应该是祈使语气的(即命令性的):如OpenFile(),set_num_errors()。取值函数&是个特例,函数名应该与变量名相同,这样函数名的含义更完整。
Abbreviations 缩写
Do not useabbreviations unless they are extremely well known outside your project. Forexample:
不要使用缩写,除非这个缩写在你的项目之外也被广泛使用。例如:
// These show proper names with noabbreviations.
int num_dns_& // Most people know what &DNS&stands for.
int price_count_&& // OK, price count. Makes sense.
// Abbreviations can be confusing orambiguous outside a small group.
int wgc_& // Only your group knows what this standsfor.
int pc_&&&&&&& // Lots of things can be abbreviated&pc&.
Never abbreviateby leaving out letters:
永远不要用省略字母的缩写。
int error_& // Good.
int error_&&& // Bad.
Filenames shouldbe all lowercase and can include underscores (_) or dashes (-). Follow theconvention that your project uses. If there is no consistent local pattern tofollow, prefer &_&.
文件名应该全都用小写,中间用“-”或“_”当分隔符。根据你的项目的惯例。如果没有固定的习惯的话,推荐用“_”。
Examples ofacceptable file names:
可接受的文件名示例:
my_useful_class.cc
my-useful-class.cc
myusefulclass.cc
myusefulclass_test.cc // _unittest and_regtest are deprecated.这两个后缀不建议使用了。
C++ files shouldend in .cc and header files should end in .h.
C++文件应该以.cc结尾,头文件以.h结尾。
Do not usefilenames that already exist in /usr/include, such as db.h.
不要用系统头文件中已经存在的名字,如db.h。
In general, makeyour filenames very specific. For example, use http_server_logs.h rather thanlogs.h. A very common case is to have a pair of files called, e.g., foo_bar.hand foo_bar.cc, defining a class called FooBar.
通常来说,让你的文件名非常的具体。例如用http_server_logs.h就比logs.h更好。定义类时文件名经常成对出现,如foo_bar.h和foo_bar.cc,对应类FooBar。
Inline functionsmust be in a .h file. If your inline functions are very short, they should godirectly into your .h file. However, if your inline functions include a lot ofcode, they may go into a third file that ends in -inl.h. In a class with a lotof inline
code, your class could have three files:
内联函数必须放在.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 ofcode.代码较多的内联函数的定义
See also thesection -inl.h Files
Type names startwith a capital letter and have a capital letter for each new word, with nounderscores: MyExcitingClass, MyExcitingEnum.
类型名以大写字母开头,且其中每个词第一个字母都大写,不用下划线:MyExcitingClass,MyExcitingEnum。
The names of alltypes — classes, structs, typedefs, and enums — have the same namingconvention. Type names should start with a capital letter and have a capitalletter for each new word. No underscores. For example:
所有类型——类,结构体,自定义类型,和枚举——都有着相同的命名惯例。类型名以大写字母开头,且其中每个词也都以大写字母开头。不用下划线。例如:
// classes and structs
class UrlTable { ...
class UrlTableTester { ...
struct UrlTableProperties { ...
// typedefs
typedef hash_map&UrlTableProperties *,string& PropertiesM
enum UrlTableErrors { ...
Variable namesare all lowercase, with underscores between words. Class member variables havetrailing underscores. For instance: my_exciting_local_variable,my_exciting_member_variable_.
变量名都是小写的,每个词之间用下划线连接。类的成员变量名结尾有个下划线。例如:my_exciting_local_varialbe;my_exciting_member_variable_。
Common Variable names 普通变量名
For example:
string table_& // OK - uses underscore.
&& // OK - all lowercase.
string tableN&& // Bad - mixed case.
Class Data Members类的数据成员
Data members(also called instance variables or member variables) are lowercase with optionalunderscores like regular variable names, but always end with a trailingunderscore.
数据成员(也被称为实例变量或成员变量)的名字都是小写的,可以像通常的变量名一样带下划线,但结尾总要有个下划线。
string table_name_;& // OK - underscore at end.
string tablename_;&& // OK.
Struct Variables 结构体变量
Data members instructs should be named like regular variables without the trailing underscoresthat data members in classes have.
结构体中的数据成员要像常规变量那么命名,结尾不像类的数据成员那样有下划线。
struct UrlTableProperties {
See Structs vs.Classes for a discussion of when to use a struct versus a class.
Global Variables 全局变量
There are nospecial 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 toeasily distinguish it from local variables.
对于全局变量没有特别要求,少用就好,但如果你要用,考虑加个g_前缀,或其它能很容易的区分全局变量与局部变量的标记。
Use a k followedby mixed case: kDaysInAWeek.
在名称前加k,其后与变量相同:kDaysInAWeek。
All compile-timeconstants, whether they are declared locally, globally, or as part of a class,follow a slightly different naming convention from other variables. Use a kfollowed by words with uppercase first letters:
所有的编译期变量,不管是局部变量,全局变量,还是类的一部分,都要遵守一个与其它变量不同的惯例。在名称前加k,后面接第一个字母大写的各个单词:
const int kDaysInAWeek = 7;
Regularfunct accessors and mutators match the name of thevariable: MyExcitingFunction(), MyExcitingMethod(),my_exciting_member_variable(), set_my_exciting_member_variable().
常规函数的名字大小写混合;取值和设值函数要与对应的变量名匹配:MyExcitingFunction(),MyExcitingMethod(),my_exciting_member_variable(),set_my_exciting_member_variable()。
Regular Functions 常规函数
Functions shouldstart with a capital letter and have a capital letter for each new word. Nounderscores.
函数名中每个单词首字母都大写。没有下划线。
If your functioncrashes upon an error, you should append OrDie to the function name. This onlyapplies to functions which could be used by production code and to errors thatare reasonably likely to occur during normal operation.
如果你的函数可能出现错误,你应该在函数名后面加上OrDie。这只适用于能用于生产&,且在正常操作中可能会发生有原因的错误的函数上。
AddTableEntry()
DeleteUrl()
OpenFileOrDie()
Accessors and Mutators 取值和设值函数
Accessors andmutators (get and set functions) should match the name of the variable they aregetting and setting. This shows an excerpt of a class whose instance variableis num_entries_.
取值和设值函数应该与相关的变量名匹配。这里摘录一个有着名为num_entries_的成员的类。
class MyClass {
& intnum_entries() const { return num_entries_; }
& voidset_num_entries(int num_entries) { num_entries_ = num_ }
& intnum_entries_;
You may also uselowercase letters for other very short inlined functions. For example if afunction were so cheap you would not cache the value if you were calling it ina loop, then lowercase naming would be acceptable.
其它的一些非常短的内联函数也可以全用小写。例如,如果一个函数的开销小到了你在循环中调用它都不需要将结果缓存起来的话,用小写的名字就很合适了。
命名空间的名字
Namespace namesare all lower-case, and based on project names and possibly their directorystructure: google_awesome_project.
命名空间的名字都是小写的,与项目名,可能还包括目录结构,相关。
See Namespacesfor a discussion of namespaces and how to name them.
Enumeratorsshould be named either like constants or like macros: either kEnumName orENUM_NAME.
枚举的命名应当和常量或宏一致:kEnumName或是ENUM_NAME。
Preferably, theindividual enumerators should be named like constants. However, it is alsoacceptable to name them like macros. The enumeration name, UrlTableErrors (andAlternateUrlTableErrors), is a type, and therefore mixed case.
单独的枚举应该优先采用常量的命名方式。但宏方式的命名也可以接受。枚举名,UrlTableErrors (和 AlternateUrlTableErrors),是类型,因此要大小写混合。
enum UrlTableErrors {
&kErrorOutOfMemory,
&kErrorMalformedInput,
enum AlternateUrlTableErrors {
&OUT_OF_MEMORY = 1,
&MALFORMED_INPUT = 2,
Until January2009, the style was to name enum values like macros. This caused problems withname collisions between enum values and macros. Hence, the change to preferconstant-style naming was put in place. New code should prefer constant-stylenaming if possible.
However, there is no reason to change old code to useconstant-style names, unless the old names are actually causing a compile-timeproblem.
2009年1月前,我们的建议是枚举名的风格和宏一致。这导致了一些枚举与宏的命名冲突。因此,现在改为采用常量风格的命名。新代码应该尽量采用常量风格。但也不必将旧代码改成常量风格的,除非旧的名字确实会导致编译错误。
You're notreally going to define a macro, are you? If you do, they're like this:MY_MACRO_THAT_SCARES_SMALL_CHILDREN.
你不会真的要定义宏吧?非得用宏的话,要这么命名:MY_MACRO_THAT_SCARES_SMALL_CHILDREN。
Please see thed in general macros should not be used. However, if theyare absolutely needed, then they should be named with all capitals andunderscores.
请看宏的描述;通常不该用宏。但是,如果真的需要宏,宏名应该全是大写,词之前用下划线分隔。
#define ROUND(x) ...
#define PI_ROUNDED 3.0
命名规则中的例外
If you arenaming something that is analogous to an existing C or C++ entity then you canfollow the existing naming convention scheme.
如果你命名的实体与已有C/C++实体类似,可参考现有命名策略。
&&& function name, follows form of open() 函数名,参考open()
&&& typedef 自定义类型
&&& struct or class, follows form of pos 结构体或类,参考pos
sparse_hash_map
&&& STL- follows STL namingconventions STL风格实体;参考STL命名惯例
LONGLONG_MAX
&&& a constant, as in INT_MAX 常量,参考INT_MAX
2 Comments 注释
Though a pain towrite, comments are absolutely vital to keeping our code readable. Thefollowing rules describe what you should comment and where. But remember: whilecomments are very important, the best code is self-documenting. Giving sensiblenames to types
and variables is much better than using obscure names that youmust then explain through comments.
注释尽管写起来很痛苦,但在保持代码的可读性方面至关重要。下面的规则描述了你应该在哪注释什么。但要记住:注释很重要,但最好的代码本身就是文档。给类型和变量起有意义的名字要比用难懂的名字导致必须得用注释解释强得多。
When writingyour comments, write for your audience: the next contributor who will need tounderstand your code. Be generous — the next one may be you!
在写注释时,目标是你的观众:下一个需要看懂你代码的作者。要好好写&——下一个作者可能就是你!
注释风格
Use either the// or /* */ syntax, as long as you are consistent.
用//和/**/都行,尽量保持一致就行。
You can useeither the // or the /* */ however, // is much more common. Beconsistent with how you comment and what style you use where.
你既可以用//也可以用/**/;但//更常用。保持注释风格和注释位置与内容的一致性。
Start each filewith license boilerplate, followed by a description of its contents.
每个文件的开始是版权公告,其后是文件内容描述。
Legal Notice and Author Line 法律声明和作者信息
Every fileshould contain license boilerplate. Choose the appropriate boilerplate for thelicense used by the project (for example, Apache 2.0, BSD, LGPL, GPL).
每个文件都应包括许可证信息。为项目选择适合的许可证版本(如Apache2.0、BSD、LGPL、GPL)。
If you makesignificant changes to a file with an author line, consider deleting the authorline.
如果你对一个有作者信息的文件进行了大修改,考虑删掉作者信息。
File Contents 文件内容
Every fileshould have a comment at the top describing its contents.
每个文件都应在开头用注释描述文件内容。
Generally a .hfile will describe the classes that are declared in the file with an overviewof what they are for and how they are used. A .cc file should contain moreinformation about implementation details or discussions of tricky algorithms.If you feel
the implementation details or a discussion of the algorithms wouldbe useful for someone reading the .h, feel free to put it there instead, butmention in the .cc that the documentation is in the .h file.
通常来说.h文件会描述文件内声明的类的用途和用法。.cc文件应该包括更多关于实现细节或算法细节讨论等信息。如果你觉得实现细节或算法细节讨论会对阅读.h文件的人有帮助,就可以将这些描述放到.h文件里,但要在.cc中注明文档在.h文件中。
Do not duplicatecomments in both the .h and the .cc. Duplicated comments diverge.
不要在.h和.cc中重复注释。这种做法偏离了注释的意义。
Every classdefinition should have an accompanying comment that describes what it is forand how it should be used.
每个类定义都应伴随着描述用途和用法的注释。
// Iterates over the contents of aGargantuanTable.& Sample usage:
//&&&GargantuanTableIterator* iter = table-&NewIterator();
//&&&for (iter-&Seek(&foo&); !iter-&done(); iter-&Next()){
//&&&&&process(iter-&key(), iter-&value());
class GargantuanTableIterator {
If you havealready described a class in detail in the comments at the top of your filefeel free to simply state &See comment at top of file for a completedescription&, but be sure to have some sort of comment.
如果你已经在文件开头描述了类的细节,可以简单的标注“见文件开头的完整描述”,但务必确保确实有这类注释。
Document thesynchronization assumptions the class makes, if any. If an instance of theclass can be accessed by multiple threads, take extra care to document therules and invariants surrounding multithreaded use.
如果类有任何同步前提,要文档说明。如果类的实例需要在多线程环境下被访问,要特别注意文档说明多线程使用中的规则和常量使用。
Declarationcomments describe comments at the definition of a functiondescribe operation.
声明处的注释描述了函数的用途;定义处的注释描述了具体的操作。
Function Declarations 函数声明
Every functiondeclaration should have comments immediately preceding it that describe whatthe function does and how to use it. These comments should be descriptive(&Opens the file&) rather than imperative (&Open thefile&); the comment describes the function,
it does not tell the functionwhat to do. In general, these comments do not describe how the functionperforms its task. Instead, that should be left to comments in the functiondefinition.
每个函数声明前面都应有注释来描述函数的用途和用法。这些注释应该是叙述性的(“打开了文件”)而不是命令式的(“打开文件”);注释是描述函数的,而不是命令函数去做什么。通常这些注释不会描述函数如何工作的,这些内容留给函数定义处的注释去做。
Types of thingsto mention in comments at the function declaration:
1.&&&&What the inputs and outputsare.
2.&&&&For class member functions:whether the object remembers reference arguments beyond the duration of themethod call, and whether it will free them or not.
3.&&&&If the function allocatesmemory that the caller must free.
4.&&&&Whether any of the argumentscan be a null pointer.
5.&&&&If there are any performanceimplications of how a function is used.
6.&&&&If the function is re-entrant.What are its synchronization assumptions?
在函数声明处注释要提及的事物:
1.&&&&输入和输出是什么。
2.&&&&对于类的成员函数:在函数调用之外对象是否保持对参数的引用,是否会释放这种引用。
3.&&&&函数是否进行了调用者必须释放的内存分配。
4.&&&&函数的参数可不可以是空指针。
5.&&&&函数的使用方法上有没有性能隐患。
6.&&&&如果函数是可重入的,其同步前提是什么?
Here is anexample:
这里有个例子:
// Returns an iterator for this table.& It is the client's
// responsibility to delete the iteratorwhen it is done with it,
// and it must not use the iterator once theGargantuanTable object
// on which the iterator was created hasbeen deleted.
// The iterator is initially positioned atthe beginning of the table.
// This method is equivalent to:
//&&&Iterator* iter = table-&NewIterator();
//&&&iter-&Seek(&&);
// If you are going to immediately seek toanother place in the
// returned iterator, it will be faster touse NewIterator()
// and avoid the extra seek.
Iterator* GetIterator()
However, do notbe unnecessarily verbose or state the completely obvious. Notice below that itis not necessary to say &returns false otherwise& because this isimplied.
但不必太啰嗦,或做些显而易见的说明。注意下面的注释没有必要加上“returns false otherwise “,因为这是显然的。
// Returns true if the table cannot hold anymore entries.
bool IsTableFull();
When commentingconstructors and destructors, remember that the person reading your code knowswhat constructors and destructors are for, so comments that just say somethinglike &destroys this object& are not useful. Document whatconstructors do with their
arguments (for example, if they take ownership ofpointers), and what cleanup the destructor does. If this is trivial, just skipthe comment. It is quite common for destructors not to have a header comment.
在注释构造函数和析构函数时,记住看你代码的人知道什么是构造函数什么是析构函数,所以如“会销毁此对象“之类的注释都是废话。文档中要说明构造函数对参数做了什么(例如是否取得指针所有权),和析构函数都清理了什么。如果这些都无关紧要,就省略注释。析构函数没有头注释是很常见的。
Function Definitions 函数定义
Each functiondefinition should have a comment describing what the function does if there'sanything tricky about how it does its job. For example, in the definitioncomment you might describe any coding tricks you use, give an overview of thesteps you go through,
or explain why you chose to implement the function in theway you did rather than using a viable alternative. For instance, you mightmention why it must acquire a lock for the first half of the function but why itis not needed for the second half.
每个函数定义处都应该有注释描述函数功能和实现要点。例如你用的编程技巧,实现的大致步骤,或解释为什么你用这种方法实现这个函数而不是用另一种方法。例如你可以提一下为什么函数的前一半需要锁而后一半不需要。
Note you shouldnot just repeat the comments given with the function declaration, in the .hfile or wherever. It's okay to recapitulate briefly what the function does, butthe focus of the comments should be on how it does it.
要注意你不能只是重复函数声明处的注释。可以简短的概述一下函数做了什么,但注释的目的是要说明它是怎么做的。
In general theactual name of the variable should be descriptive enough to give a good idea ofwhat the variable is used for. In certain cases, more comments are required.
通常变量的名字就应该足够描述清楚变量的用途的了。在某些场合时,也需要额外的注释。
Class Data Members 类数据成员
Each class datamember (also called an instance variable or member variable) should have acomment describing what it is used for. If the variable can take sentinelvalues with special meanings, such as a null pointer or -1, document this. Forexample:
每个类的数据成员(也被称为实例变量或成员变量)应该有描述其用途的注释。如果这个变量可以取到有特殊含义的值,如空指针或-1,要文档说明。例如:
&//Keeps track of the total number of entries in the table.
&//Used to ensure we do not go over the limit. -1 means
&//that we don't yet know how many entries the table has.
&intnum_total_entries_;
Global Variables 全局变量
As with datamembers, all global variables should have a comment describing what they areand what they are used for. For example:
与数据成员一样,所有的全局变量都应该用注释来描述变量的含义和用途。例如:
// The total number of tests cases that we runthrough in this regression test.
const int kNumTestCases = 6;
In yourimplementation you should have comments in tricky, non-obvious, interesting, orimportant parts of your code.
对于代码中巧妙的、晦涩的、有趣的、或重要的部分,应用注释说明。
Class Data Members 类数据成员
Tricky orcomplicated code blocks should have comments before them. Example:
巧妙的或复杂的代码段应该在前面注释。例如:
// Divide result by two, taking into accountthat x
// contains the carry from the add.
for (int i = 0; i & result-&size();i++) {
& x =(x && 8) + (*result)[i];
&(*result)[i] = x && 1;
Line Comments 行注释
Also, lines thatare non-obvious should get a comment at the end of the line. These end-of-linecomments should be separated from the code by 2 spaces. Example:
同样的,含义不明的行应该在行尾注释。这些行尾的注释应该与代码保持2个空格的间隙。如:
// If we have enough memory, mmap the dataportion too.
mmap_budget = max&int64&(0,mmap_budget - index_-&length());
if (mmap_budget &= data_size_ &&!MmapData(mmap_chunk_bytes, mlock))
&& // Error already logged.
Note that thereare both comments that describe what the code is doing, and comments thatmention that an error has already been logged when the function returns.
注意到有两条注释,一条描述了代码在做什么,另一条提到了在函数因为错误而返回时错误已经被记录在日志中了。
If you haveseveral comments on subsequent lines, it can often be more readable to linethem up:
如果你需要连续进行多行注释,可以使之对齐获得更好的可读性:
DoSomething();&&&&&&&&&&&&&&&&& // Comment here so thecomments line up.
DoSomethingElseThatIsLonger();& // Comment here so there are two spacesbetween
&&&&&&&&&&&&&&&&&&&&&&&&&&& &&&&// the code and the comment.
{ // One space before comment when opening anew scope is allowed,
& //thus the comment lines up with the following comments and code.
&DoSomethingElse();& // Two spacesbefore line comments normally.
nullptr/NULL, true/false, 1, 2, 3... 直接量
When you pass ina null pointer, boolean, or literal integer values to functions, you shouldconsider adding a comment about what they are, or make your codeself-documenting by using constants. For example, compare:
当你向函数传递了一个空指针,布尔量,或直接量时,应考虑增加一个注释说明它们是什么,或用常量来让你的代码自文档化。例如,比如:
bool success =CalculateSomething(interesting_value,
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 10,
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& false,
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& NULL);& // What are these arguments??
bool success =CalculateSomething(interesting_value,
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 10,&&&& // Default base value.
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& false,& // Not the first time we're calling this.
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& NULL);& // No callback.
Oralternatively, constants or self-describing variables:
或者用常量或自描述型的变量:
const int kDefaultBaseValue = 10;
const bool kFirstTimeCalling =
Callback *null_callback = NULL;
bool success =CalculateSomething(interesting_value,
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&kDefaultBaseValue,
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&kFirstTimeCalling,
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&null_callback);
Don'ts 不要做的事
Note that youshould never describe the code itself. Assume that the person reading the codeknows C++ better than you do, even though he or she does not know what you aretrying to do:
要注意你永远都不该自然语言翻译代码。要假设看你代码的人比你更懂C++,即便他或她不知道你的用意:
// Now go through the b array and make surethat if i occurs,
// the next element is i+1.
...&&&&&&&// Geez.& What a useless comment.
标点,拼写和语法
Pay attention topunctuation, spelling, it is easier to read well-written commentsthan badly written ones.
要注意标点、拼写和语法;写的好的注释要比写的差的更容易阅读。
Comments shouldbe as readable as narrative text, with proper capitalization and punctuation.In many cases, complete sentences are more readable than sentence fragments.Shorter comments, such as comments at the end of a line of code, can sometimesbe less
formal, but you should be consistent with your style.
注释应该和叙事文本一样可读,有着适当的大小写和标点。在许多情况下,完整的句子要比句子碎片更易阅读。比较短的注释,如在行尾的注释,有时可能不够正式,但你应该和你的风格保持一致。
Although it canbe frustrating to have a code reviewer point out that you are using a commawhen you should be using a semicolon, it is very important that source codemaintain a high level of clarity and readability. Proper punctuation, spelling,and grammar
help with that goal.
虽然被别人指出该用分号时却用了逗号多少有些尴尬,但清晰易读的代码还是很重要的。合适的标点、拼写和语法能帮助你实现这一目标。
Use TODOcomments for code that is temporary, a short-term solution, or good-enough butnot perfect.
将TODO注释用在临时性的、短期解决方案、或足够好但还不完美的代码上。
TODOs shouldinclude the string TODO in all caps, followed by the name, e-mail address, orother identifier of the person who can best provide context about the problemreferenced by the TODO. A colon is optional. The main purpose is to have aconsistent TODO
format that can be searched to find the person who can providemore details upon request. A TODO is not a commitment that the personreferenced will fix the problem. Thus when you create a TODO, it is almostalways your name that is given.
TODO注释应该包括全大写的“TODO”,接着是能提供最多信息的人的名字,邮箱地址,或其它个人信息。冒号是可选的。主要目的是能让添加注释的人(或可提供最多信息的人)按统一的TODO格式搜索到。TODO注释不表示相关的人承诺会修复此问题。因此当你新建了一个TODO注释,它给出的总是你的名字。
// TODO(): Use a &*& herefor concatenation operator.
// TODO(Zeke) change this to use relations.
If your TODO isof the form &At a future date do something& make sure that you eitherinclude a very specific date (&Fix by November 2005&) or a veryspecific event (&Remove this code when all clients can handle XMLresponses.&).
如果你的TODO注释是“未来某时会做某事&”的格式,要确保你要么包括了一个具体的时间(“2005年11月修复”)要么包括具体的事件(“当所有客户端都能处理XML反馈后移除此代码”)。
过期内容的注释
Mark deprecatedinterface points with DEPRECATED comments.
用DEPRECATED注释来标记过期的接口。
You can mark aninterface as deprecated by writing a comment containing the word DEPRECATED inall caps. The comment goes either before the declaration of the interface or onthe same line as the declaration.
你可以通过给某接口加上DEPRECATED的注释来标记其为过期的。可以在接口声明之前注释,也可在同一行注释。
After the wordDEPRECATED, write your name, e-mail address, or other identifier inparentheses.
在DEPRECATED单词后的括号里写上你的名字、邮箱或其它个人信息。
A deprecationcomment must include simple, clear directions for people to fix theircallsites. In C++, you can implement a deprecated function as an inlinefunction that calls the new interface point.
过期内容的注释必须包括简单清晰的用于指导用户修正调用的提示。在C++中,你可以将一个过期的函数实现为内联函数,在其中调用新的接口点。
Marking aninterface point DEPRECATED will not magically cause any callsites to change. Ifyou want people to actually stop using the deprecated facility, you will haveto fix the callsites yourself or recruit a crew to help you.
用DEPRECATED标记一个接口不会魔术般的导致任何调用被改变。如果你想要用户真的停止使用过期的设施,你需要自己修复调用或招几个人来帮你。
New code shouldnot contain calls to deprecated interface points. Use the new interface pointinstead. If you cannot understand the directions, find the person who createdthe deprecation and ask them for help using the new interface point.
新代码中不应包含对过期接口点的调用。改为使用新接口点。如果你看不懂提示,就找添加了这个过期说明的人,然后请求他帮你使用新接口点。
3 Formatting 格式
Coding style andformatting are pretty arbitrary, but a project is much easier to follow ifeveryone uses the same style. Individuals may not agree with every aspect ofthe formatting rules, and some of the rules may take some getting used to, butit is important
that all project contributors follow the style rules so thatthey can all read and understand everyone's code easily.
代码风格与格式确实很随意,但如果所有人都使用同样的风格,项目就能更容易进行。个体可能不同意格式规则中的每一方面,一些规则也可能需要一些时间来适应,但所有的项目成员都遵守相同的风格规则是很重要的,这样他们就可以很容易的看懂每个人的代码了。
To help youformat code correctly, we've created a settings file for emacs.
Each line oftext in your code should be at most 80 characters long.
你每行代码的文本最多只允许80个字符宽。
We recognizethat this rule is controversial, but so much existing code already adheres toit, and we feel that consistency is important.
我们承认这条规则有争议,但有这么多现存的代码已经遵守它了,我们感觉一致性更重要。
Those who favorthis rule argue that it is rude to force them to resize their windows and thereis no need for anything longer. Some folks are used to having several codewindows side-by-side, and thus don't have room to widen their windows in anycase. People
set up their work environment assuming a particular maximum windowwidth, and 80 columns has been the traditional standard. Why change it?
喜欢这条规则的人主张强迫他们改变编辑器窗口大小很野蛮,而且更长的代码也没什么必要。一些人习惯多个代码窗口挨着排,因此没办法让他们的窗口再宽一点点了。人们都将他们的工作环境设为某个特定的窗口宽度,而80列就是传统的标准。为什么要改变它?
Proponents ofchange argue that a wider line can make code more readable. The 80-column limitis an hidebound throwback to 1960 modern equipment has widescreens that can easily show longer lines.
支持改变此规则的人认为更宽的行能令代码更有可读性。80列的限制可以回溯到古老的1960年代的主机;现代机器有着更宽的屏幕,能轻易的显示更宽的行。
80 characters isthe maximum.
80个字符,不能再多了。
Exception: if acomment line contains an example command or a literal URL longer than 80characters, that line may be longer than 80 characters for ease of cut andpaste.
例外:如果注释行包括了一个示例命令或URL的文本超过了80个字符,这行就可以超过80个字符,以便复制剪切。
Exception: an#include statement with a long path may exceed 80 columns. Try to avoidsituations where this becomes necessary.
例外:#include语句如果路径很长的话可能超过80列。试着避免出现这种情况。
Exception: youneedn't be concerned about header guards that exceed the maximum length.
例外:你不必关心头文件的守卫是否超过了最大长度。
非ASCII字符
Non-ASCIIcharacters should be rare, and must use UTF-8 formatting.
要少用非ASCII字符,即使用的话也必须用UTF-8格式。
You shouldn'thard-code user-facing text in source, even English, so use of non-ASCIIcharacters should be rare. However, in certain cases it is appropriate toinclude such words in your code. For example, if your code parses data filesfrom foreign sources,
it may be appropriate to hard-code the non-ASCIIstring(s) used in those data files as delimiters. More commonly, unittest code(which does not need to be localized) might contain non-ASCII strings. In suchcases, you should use UTF-8, since that is an encoding
understood by most toolsable to handle more than just ASCII. Hex encoding is also OK, and encouragedwhere it enhances readability — for example, &\xEF\xBB\xBF& is theUnicode zero-width no-break space character, which would be invisible ifincluded in the source
as straight UTF-8.
你不应将用户界面文本硬编码进源文件中,即使是英文,这样就不怎么需要用非ASCII字符了。但是,有些场合就适合将非ASCII单词放进代码中。例如,如果你的代码要解析国外来源的数据文件,将在这些文件中充当定界符的非ASCII字符串硬编码可能会比较合适。更常见的情况是单元测试代码(不需要本地化)可能会包含非ASCII字符。这些场合你都应该用UTF-8格式,因为这种编码被大多数能处理不止ASCII这一种编码的工具所支持。16进制编码也可以,在能增强可读性的地方推荐这种编码——例如,“\xEF\xBB\xBF”是Unicode零宽度无间断空格字符,而如果直接按UTF-8格式加进源文件中就看不到这个字符&了。
空格还是制表符
Use only spaces,and indent 2 spaces at a time.
只使用空格,每次缩进2个空格&。
We use spacesfor indentation. Do not use tabs in your code. You should set your editor toemit spaces when you hit the tab key.
我们用空格进行缩进。不要在你的代码中用tab。你应该将你的编辑器设置为点击tab键时输出空格。
函数声明和定义
Return type onthe same line as function name, parameters on the same line if they fit.
返回值类型要与函数名同行,参数名如果合适的话也在同行。
Functions looklike this:
像这样的函数:
ReturnType ClassName::FunctionName(Typepar_name1, Type par_name2) {
&DoSomething();
If you have toomuch text to fit on one line:
如果你有太多东西写在了同一行:
ReturnTypeClassName::ReallyLongFunctionName(Type par_name1, Type par_name2,
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&Type par_name3) {
&DoSomething();
or if you cannotfit even the first parameter:
或如果你连第一个参数都放不进同一行:
ReturnTypeLongClassName::ReallyReallyReallyLongFunctionName(
&&&Type par_name1,& // 4 space indent
&&&Type par_name2,
&&&Type par_name3) {
&DoSomething();& // 2 space indent
Some points tonote:
1.&&&&The return type is always onthe same line as the function name.
2.&&&&The open parenthesis is alwayson the same line as the function name.
3.&&&&There is never a space betweenthe function name and the open parenthesis.
4.&&&&There is never a space betweenthe parentheses and the parameters.
5.&&&&The open curly brace is alwaysat the end of the same line as the last parameter.
6.&&&&The close curly brace is eitheron the last line by itself or (if other style rules permit) on the same line asthe open curly brace.
7.&&&&There should be a space betweenthe close parenthesis and the open curly brace.
8.&&&&All parameters should be named,with identical names in the declaration and implementation.
9.&&&&All parameters should bealigned if possible.
10.& Default indentation is 2 spaces.
11.& Wrapped parameters have a 4 space indent.
一些要记住的要点:
1.&&&&返回值类型总是与函数名在同一行。
2.&&&&开括号&总要与函数名在同一行。
3.&&&&在函数名与开括号间不要有空格。
4.&&&&在括号和参数间不要有空格。
5.&&&&开的大括号&总与最后一个参数同行。
6.&&&&闭的大括号可以自己在最后一行或(如果其它规则允许)与开的大括号在同一行。
7.&&&&在闭括号与开大括号间应该有个空格。
8.&&&&所有的参数都要有名字,声明和实现中的名字要相同。
9.&&&&如果可能,所有参数都应该对齐。
10.& 默认缩进是2个空格。
11.& 换行后的参数有4个空格的缩进。
If someparameters are unused, comment out the variable name in the functiondefinition:
如果一些参数没被用到,在函数定义处将参数名注释起来。
// Always have named parameters ininterfaces.
class Shape {
&virtual void Rotate(double radians) = 0;
// Always have named parameters in thedeclaration.
class Circle : public Shape {
&virtual void Rotate(double radians);
// Comment out unused named parameters indefinitions.
void Circle::Rotate(double /*radians*/) {}
// Bad - if someone wants to implementlater, it's not clear what the
// variable means.
void Circle::Rotate(double) {}
O otherwise, wrap arguments at the parenthesis.
尽量放在同一行,否则,将实参封装在圆括号中。
Function callshave the following format:
函数调用遵循下面的格式:
bool retval = DoSomething(argument1,argument2, argument3);
If the argumentsdo not all fit on one line, they should be broken up onto multiple lines, witheach subsequent line aligned with the first argument. Do not add spaces afterthe open paren or before the close paren:
如果实参放不进一行,就将它们分成多行,分出的每行都要和第一个参数对齐。不要在开括号后和闭括号前加空格:
bool retval =DoSomething(averyveryveryverylongargument1,
&&&&&&&&&&&&&&&&&&&&&&&&& argument2,argument3);
If the functionhas many arguments, consider having one per line if this makes the code morereadable:
如果函数有很多参数,考虑每行写一个参数,可以增强可读性:
bool retval = DoSomething(argument1,
&&&&&&&&&&&&&&&&&&&&&&&&& argument2,
&&&&&&&&&&&&&&&&&&&&&&&&& argument3,
&&&&&&&&&&&&&&&&&&&&&&&&& argument4);
If the functionsignature is so long that it cannot fit within the maximum line length, you mayplace all arguments on subsequent lines:
如果函数原型太长,以至于超过了行的最大长度,你可以将所有参数独立成行:
if (...) {
& if(...) {
&&&DoSomethingThatRequiresALongFunctionName(
&&&&&&&very_long_argument1,& // 4 spaceindent
&&&&&&&argument2,
&&&&&&&argument3,
&&&&&&&argument4);
Prefer no spacesinside parentheses. The else keyword belongs on a new line.
建议括号内没有空格。else关键字放在新行中。
There are twoacceptable formats for a basic conditional statement. One includes spacesbetween the parentheses and the condition, and one does not.
有两种基本的条件语句格式都可使用。一种在括号和条件式之间有空格,另一种没有。
The most commonform is without spaces. Either is fine, but be consistent. If you are modifyinga file, use the format that is already present. If you are writing new code,use the format that the other files in that directory or project use. If indoubt and
you have no personal preference, do not add the spaces.
最常见的格式是没有空格的。用哪种都可以,但要保持一致。如果你修改一个文件,就用当前的格式。如果你在写新代码,用项目或目录中其它文件的格式。如果不知道用哪个,你也没有个人倾向的话,不要加空格。
if (condition) {& // no spaces inside parentheses
&...& // 2 space indent.
} else if (...) {& // The else goes on the same line as theclosing brace.
If you preferyou may add spaces inside the parentheses:
如果你喜欢的话,也可以在括号与条件式之间加空格:
if ( condition ) {& // spaces inside parentheses - rare
&...& // 2 space indent.
} else {&// The else goes on the same line as the closing brace.
Note that in allcases you must have a space between the if and the open parenthesis. You mustalso have a space between the close parenthesis and the curly brace, if you'reusing one.
注意任何情况下你都要在if和开括号间放一个空格。你也必须在闭括号和大括号间放空格。
if(condition)&&&& // Bad - space missing after IF.
if (condition){&& // Bad - space missing before {.
if(condition){&&& // Doubly bad.
if (condition) {& // Good - proper space after IF and before {.
Shortconditional statements may be written on one line if this enhances readability.You may use this only when the line is brief and the statement does not use theelse clause.
短的条件语句也可以写在同一行内,如果不影响可读性的话。只有在行很短且没有else分句时才能这么用。
if (x == kFoo) return new Foo();
if (x == kBar) return new Bar();
This is notallowed when the if statement has an else:
如果if语句包含else的话不许如下这么用:
// Not allowed - IF statement on one linewhen there is an ELSE clause
if (x) DoThis();
else DoThat();
In general,curly braces are not required for single-line statements, but they are all conditional or loop statements with complex conditions orstatements may be more readable with curly braces. Some projects require thatan if must always
always have an accompanying brace.
通常开的大括号不需要单独一行,但你喜欢的话也可以;有着复杂的条件或状态的条件语句和循环语句后面跟大括号的话,可能会更有可读性。一些项目要求if后面必须跟着大括号。
if (condition)
&DoSomething();& // 2 space indent.
if (condition) {
&DoSomething();& // 2 space indent.
However, if onepart of an if-else statement uses curly braces, the other part must too:
但是,如果if-else语句中的一部分用大括号了,另一部分也必须用:
// Not allowed - curly on IF but not ELSE
if (condition) {
// Not allowed - curly on ELSE but not IF
if (condition)
// Curly braces around both IF and ELSErequired because
// one of the clauses used braces.
if (condition) {
循环和switch语句
Switchstatements may use braces for blocks. Empty loop bodies should use {} orcontinue.
switch语句可以用大括号来分段。空循环体应该用{}或continue。
case blocks inswitch statements can have curly braces or not, depending on your preference.If you do include curly braces they should be placed as shown below.
switch的case块可以用大括号包起来,也可以不用,取决于你的喜好。如果你引入大括号了,应该参照下面的用法。
If notconditional on an enumerated value, switch statements should always have adefault case (in the case of an enumerated value, the compiler will warn you ifany values are not handled). If the default case should never execute, simplyassert:
如果有不满足case条件的枚举值,switch语句应该总是包含一个默认case块(如果有输入值没有case条件去处理,编译器会警告)。如果默认case永远都不该执行,就放一个assert。
switch (var) {
& case0: {& // 2 space indent
&&&...&&&&& // 4 space indent
& case1: {
&default: {
&&&assert(false);
Empty loopbodies should use {} or continue, but not a single semicolon.
空的循环体应该用{}或continue,而不是单独的一个分号。
while (condition) {
& //Repeat test until it returns false.
for (int i = 0; i & kSomeN ++i){}& // Good - empty body.
while (condition)& // Good - continue indicates no logic.
while (condition);& // Bad - looks like part of do/while loop.
指针和引用表达式
No spaces aroundperiod or arrow. Pointer operators do not have trailing spaces.
在句点和箭头前后没有空格。指针操作符后面没有尾随的空格。
The followingare examples of correctly-formatted pointer and reference expressions:
下面是格式正确的指针和引用表达式:
Note that:
1.&&&&There are no spaces around theperiod or arrow when accessing a member.
2.&&&&Pointer operators have no spaceafter the * or &.
1.&&&&在访问成员时,句点和箭头的前后没有空格。
2.&&&&指针操作符中*和&后面没有空格。
When declaring apointer variable or argument, you may place the asterisk adjacent to either thetype or to the variable name:
当声明指针变量或参数时,星号可以挨着类型也可以挨着变量名:
// These are fine, space preceding.
const string &
// These are fine, space following.
char*&&&// but remember to do &char* c, *d, *e, ...;&!
const string&
char *&// Bad - spaces on both sides of *
const string && // Bad - spaces on both sides of &
You should dothis consistently within a single file, so, when modifying an existing file,use the style in that file.
你应该在同一个文件中保持一致,所以在修改一个已有的文件时,用文件原有的风格。
布尔表达式
When you have aboolean expression that is longer than the standard line length, be consistentin how you break up the lines.
如果一个布尔表达式比标准行宽还要长,你要像其它情况一样将它分成多行。
In this example,the logical AND operator is always at the end of the lines:
这个例子中,与操作符总是在行尾&:
if (this_one_thing & this_other_thing&&
&&&a_third_thing == a_fourth_thing &&
&&&yet_another && last_one) {
Note that whenthe code wraps in this example, both of the && logical AND operatorsare at the end of the line. This is more common in Google code, though wrappingall operators at the beginning of the line is also allowed. Feel free to insertextra parentheses
judiciously because they can be very helpful in increasingreadability when used appropriately. Also note that you should always use thepunctuation operators, such as && and ~, rather than the word operators,such as and and compl.
注意到本例中代码被分成多行,其中的&&操作符都在行尾。尽管也可以将所有操作符都对齐到行首,本例中的用法在谷歌的代码中更常见。这样可以方便插入括号,适当使用对提升可读性很有帮助。同样注意,你应该总是用符号表示的操作符,如&&和~,而不是用单词表示&,如and和compl。
返回值
Do notneedlessly surround the return expression with parentheses.
不要用不必要的括号括起返回表达式。
Use paren only where you would use them in x =.
返回表达式expr只有在令它为赋值式时会引起歧义的地方才能用括号括起:
&&&&&&&&&&&&&&&&& // No parentheses in thesimple case.简单场合不用括号
return (some_long_condition &&& // Parentheses ok to make a complex复杂表达式用括号可读性更好
&&&&&&&another_condition);&&&& //&&&& expression more readable.
return (value);&&&&&&&&&&&&&&& // You wouldn't write var =(value);你不会写var=(value)这样的式子的&!
return(result);&&&&&&&&&&&&&&& // return is not a function!return不是个函数!
变量和数组初始化
Your choice of =or ().
可以用=也可以用()。
You may choosebetween = and (); the following are all correct:
你可以在=和()中选一种形式&;下面的例子都是正确的:
int x = 3;
string name(&Some Name&);
string name = &Some Name&;
预处理指令
The hash markthat starts a preprocessor directive should always be at the beginning of theline.
预处理指令应该总在行首,不要缩进。
Even whenpreprocessor directives are within the body of indented code, the directivesshould start at the beginning of the line.
即使预处理指令位于缩进的代码块中,它们也不缩进而是总在行首。
// 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 -- Spacesafter #可以但不需要#后面有空格
&&&NotifyClient();
&&&BackToNormal();
// Bad - indented directives
& if(lopsided_score) {
&&&#if DISASTER_PENDING& //Wrong!& The &#if& should be atbeginning of line
&&&DropEverything();
&&&#endif&&&&&&&&&&&&&&& //Wrong!& Do not indent &#endif&
&&&BackToNormal();
类格式
Sections inpublic, protected and private order, each indented one space.
类中各部分按public、protected和private的顺序,每个关键字缩进1个空格。
The basic formatfor a class declaration (lacking the comments, see Class Comments for adiscussion of what comments are needed) is:
类声明的基本格式(省略了注释)是:
class MyClass : public OtherClass {
&public:&&&&&// Note the 1 space indent!
&MyClass();& // Regular 2 spaceindent.
&explicit MyClass(int var);
&~MyClass() {}
& voidSomeFunction();
& voidSomeFunctionThatDoesNothing() {
& voidset_some_var(int var) { some_var_ = }
& intsome_var() const { return some_var_; }
& boolSomeInternalFunction();
& intsome_var_;
& intsome_other_var_;
&DISALLOW_COPY_AND_ASSIGN(MyClass);
Things to note:
1.&&&&Any base class name should beon the same line as the subclass name, subject to the 80-column limit.
2.&&&&The public:, protected:, andprivate: keywords should be indented one space.
3.&&&&Except for the first instance,these keywords should be preceded by a blank line. This rule is optional insmall classes.
4.&&&&Do not leave a blank line afterthese keywords.
5.&&&&The public section should befirst, followed by the protected and finally the private section.
6.&&&&See Declaration Order for ruleson ordering declarations within each of these sections.
注意事项:
1.&&&&任何基类的名字都应与子类名字在同一行,遵循80列宽的限制。
2.&&&&public、protected、private关键字应该缩进1个空格。
3.&&&&除了第一个出现的关键字,其后的关键字前面应该有个空行。此规则对于较小的类是可选的。
4.&&&&不要在这些关键字后留空行。
5.&&&&public段应在最前,其后是protected段,最后是private段。
构造函数初始化列表
Constructorinitializer lists can be all on one line or with subsequent lines indented fourspaces.
构造函数初始化列表可以都放在一行中也可以分成多行,每行缩进4个空格。
There are twoacceptable formats for 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 4spaces, putting the colon on
// the first initializer line:
MyClass::MyClass(int var)
&&& :some_var_(var),&&&&&&&&&&&& // 4 spaceindent
&&&&&some_other_var_(var + 1) {& //lined up
&DoSomething();
命名空间格式
The contents ofnamespaces are not indented.
命名空间的内容不缩进。
Namespaces donot add an extra level of indentation. For example, use:
命名空间不会增加一层缩进。例如:
namespace {
void foo() {&// Correct.& No extra indentationwithin namespace.
}& //namespace
Do not indentwithin a namespace:
命名空间内不要缩进:
namespace {
& //Wrong.& Indented when it should not be.
& voidfoo() {
}& //namespace
When declaringnested namespaces, put each namespace on its own line.
声明嵌套命名空间时,每个命名空间单独一行。
namespace foo {
namespace bar {
Use of horizontalwhitespace depends on location. Never put trailing whitespace at the end of aline.
分情况使用水平留白。永远不要在行尾放空格。
General 通常情况
void f(bool b) {& // Open braces should always have a spacebefore them.开括号前应该有一个空格
int i = 0;&// Semicolons usually have no space before them.分号前通常没有空格
int x[] = { 0 };& // Spaces inside braces for arrayinitialization are数组初始化时括号内的空格可选
int x[] = {0};&&& // optional.& If you use them, put them on both sides!用的时候要两边都加空格
// Spaces around the colon in inheritanceand initializer lists.继承和初始化列表中的冒号前后要有空格
class Foo : public Bar {
& //For inline function implementations, put spaces between the braces
& //and the implementation itself.内联函数的实现中,要在括号和实现本身前都加空格
&Foo(int b) : Bar(), baz_(b) {}& //No spaces inside empty braces.空括号内没有空格
& voidReset() { baz_ = 0; }& // Spacesseparating braces from implementation.空格分开了括号和实现
Adding trailingwhitespace can cause extra work for others editing the same file, when theymerge, as can removing existing trailing whitespace. So: Don't introducetrailing whitespace. Remove it if you're already changing that line, or do itin a separate clean-up
operation (preferably when no-one else is working on thefile).
添加冗余的留白会给其他人编辑时造成额外负担,合并行时还需要去除多余的空格。所以:不要引入多余的留白。如果你已经改变完成一行后就去掉行尾的空格,或进行单独的清理操作(推荐在没人使用此文件时进行)。
Loops and Conditionals 循环和条件语句
if (b) {&&&&&&&&& // Space after the keyword inconditions and loops.循环和条件句的关键字后有空格
} else {&&&&&&&&& // Spaces around else. else前后要有空格
while (test) {}&& // There is usually no space insideparentheses. while的括号里通常没空格
switch (i) {
for (int i = 0; i & 5; ++i) {
switch ( i ) {&&& // Loops and conditions may have spacesinside 循环和条件句内可以有空格,但很罕见
if ( test ) {&&&& // parentheses, but this is rare.& Be consistent.
for ( int i = 0; i & 5; ++i ) {
for ( ; i & 5 ; ++i) {& // For loops always have a space after thefor循环的分号后总有空格,分号前
&...&&&&&&&&&&&&&&&&&& //semicolon, and may have a space before the 可以有空格
&&&&&&&&&&&&&&&&&&&&&&& // semicolon.
for (auto x : counts) {& // Range-based for loops always have a 基于范围的for循环的冒号前后都有空格
&...&&&&&&&&&&&&&&&&&&& // spacebefore and after the colon.
switch (i) {
& case1:&&&&&&&& // No space before colon in aswitch case. case条件中的冒号前没空格
& case2:& // Use a space after a colonif there's code after it. 冒号后有代码时要有1个空格
Operators 操作符
x = 0;&&&&&&&&&&&&& // Assignment operators alwayshave spaces around 赋值符前后要有空格
&&&&&&&&&&&&&&&&&&& // them.
x = -5;&&&&&&&&&&&& // No spaces separating unaryoperators and their 单独的单目操作符和参数间没有空格
++x;&&&&&&&&&&&&&&& // arguments.
if (x && !y)
v = w * x + y /& // Binary operators usually have spacesaround them, 双目操作符前后通常有空格
v = w*x + y/z;&&&&& // but it's okay to remove spaces aroundfactors. 但在运算符前后可以去掉空格
v = w * (x + z);&&& // Parentheses should have no spaces insidethem. 括号内侧不应有空格
Templates and Casts 模板和转换
vector&string&&&&&&&&&&& // No spaces inside the angle 角括号内侧没有空格
y = static_cast&char*&(x);& // brackets (& and &), before 角括号和圆括号间没有空格
&&&&&&&&&&&&&&&&&&&&&&&&&&& // &, or between&( in a cast.
vector&char *&&&&&&&&& &&// Spaces between type and pointer are 类型和星号间可以有空格,但要保持一致
&&&&&&&&&&&&&&&&&&&&&&&&&&& // okay, but beconsistent.
set&list&string&&&&&&&&& // Permitted in C++11 code. C++11中允许此代码
set&list&string& &&&&&&& // C++03 required a space in & &.C++03中需要在&&中插入空格&
set& list&string& &&&&&& // You may optionally use 此时你也可以对称的在前面也加个空格
&&&&&&&&&&&&&&&&&&&&&&&&&&& // symmetricspacing in & &.
Minimize use ofvertical whitespace.
尽量少用垂直留白。
This is more aprinciple than a rule: don't use blank lines when you don't have to. Inparticular, don't put more than one or two blank lines between functions,resist starting functions with a blank line, don't end functions with a blankline, and be discriminating
with your use of blank lines inside functions.
这不仅仅是规则而是原则问题:不必要的时候不要用空行。尤其是不要在函数间留超过一到两个空格,函数首尾不要留空行,函数中也要谨慎使用空行。
The basicprinciple is: The more code that fits on one screen, the easier it is to followand understand the control flow of the program. Of course, readability cansuffer from code being too dense as well as too spread out, so use yourjudgement. But in general,
minimize use of vertical whitespace.
基本原则是:同一屏幕内能显示的代码越多,理解掌握程序的控制流越容易。当然,思路过分发散,导致代码过于密集,也会导致可读性下降,所以需要你自己把握。但通常来说要尽量少用垂直留白。
Some rules ofthumb to help when blank lines may be useful:
1.&&&&Blank lines at the beginning orend of a function very rarely help readability.
2.&&&&Blank lines inside a chain ofif-else blocks may well help readability.
使用空行的一些经验法则:
1.&&&&函数首尾的空行对提高可读性没有帮助。
2.&&&&if-else链中各块间的空行可能会对此有帮助。
4 Exceptionsto the Rules 规则的例外
The codingconventions described above are mandatory. However, like all good rules, thesesometimes have exceptions, which we discuss here.
上面叙述的代码习惯是强制性的。但是,像所有的良好规则一样,这些规则有时也有例外,此处就讨论这些例外。
现有不合规范的代码
You may divergefrom the rules when dealing with code that does not conform to this styleguide.
在处理不符合本风格规范的代码时,你可以不遵守上面的规则。
If you findyourself modifying code that was written to specifications other than thosepresented by this guide, you may have to diverge from these rules in order tostay consistent with the local conventions in that code. If you are in doubtabout how to do
this, ask the original author or the person currentlyresponsible for the code. Remember that consistency includes local consistency,too.
如果你在修改不符合本规范的代码时,你可能需要放弃这些规则,来和代码中原有的习惯保持一致。如果你困惑该怎么做,就去问原作者或当前负责此代码的人。记得要保持局部一致性。
Windows代码
Windowsprogrammers have developed their own set of coding conventions, mainly derivedfrom the conventions in Windows headers and other Microsoft code. We want tomake it easy for anyone to understand your code, so we have a single set ofguidelines for everyone
writing C++ on any platform.
Windows程序员已经发展出了他们自己的编程习惯,主要源于Windows头文件和其它微软代码中的习惯。我们想让任何人都能顺利理解你的代码,所以我们为在任何平台写C++代码的人都准备了一个单独的规范集合。
It is worth reiteratinga few of the guidelines that you might forget if you are used to the prevalentWindows style:
1.&&&&Do not use Hungarian notation(for example, naming an integer iNum). Use the Google naming conventions,including the .cc extension for source files.
2.&&&&Windows defines many of its ownsynonyms for primitive types, such as DWORD, HANDLE, etc. It is perfectlyacceptable, and encouraged, that you use these types when calling Windows APIfunctions. Even so, keep as close as you can to the underlying C++
types. Forexample, use const TCHAR * instead of LPCTSTR.
3.&&&&When compiling with MicrosoftVisual C++, set the compiler to warning level 3 or higher, and treat allwarnings as errors.
4.&&&&Do not use #instead use the standard Google include guards. The path in the include guardsshould be relative to the top of your project tree.
5.&&&&In fact, do not use anynonstandard extensions, like #pragma and __declspec, unless you absolutelymust. Using __declspec(dllimport) and __declspec(dllexport)however, you must use them through macros such as DLLIMPORT and DLLEXPORT, sothat
someone can easily disable the extensions if they share the code.
如果你习惯了通行的Windows风格,就很有必要重申一些你可能忘记的规范:
1.&&&&不要用匈牙利命名法(例如将整数命名为iNum)。用谷歌的命名规范,源代码文件扩展名用.cc。
2.&&&&Windows定义了许多它独有的内置类型的别名,如DWORD和HANDLE等等。在你调用Windows API时鼓励用这些类型。即使这样,你也应该尽量使用C++的类型名。例如,用constTCHAR*去替代LPCTSTR。
3.&&&&用VC++编译代码时,将警告等级调到3或更高,并将所有警告都视作错误。
4.&&&&不要用#pragma once;改用标准的头文件守卫规则。在#define中的路径要相对于项目根目录。
5.&&&&事实上不要用任何不标准的编译器扩展,如#pragma和__declspec,除非你必须得这么做。用__declspec(dllimport)和__declspec(dllexport)是允许的;但你必须通过宏来调用它们,如DLLIMPORT和DLLEXPORT, 这样别人就可以在分享使用此代码时方便的禁用这些扩展了。
However, thereare just a few rules that we occasionally need to break on Windows:
1.&&&&Normally we forbid the use ofmultiple implem however, it is required when using COM andsome ATL/WTL classes. You may use multiple implementation inheritance toimplement COM or ATL/WTL classes and interfaces.
2.&&&&Although you should not useexceptions in your own code, they are used extensively in the ATL and someSTLs, including the one that comes with Visual C++. When using the ATL, youshould define _ATL_NO_EXCEPTIONS to disable exceptions. You should investigatewhether
you can also disable exceptions in your STL, but if not, it is OK toturn on exceptions in the compiler. (Note that this is only to get the STL tocompile. You should still not write exception handling code yourself.)
3.&&&&The usual way of working withprecompiled headers is to include a header file at the top of each source file,typically with a name like StdAfx.h or precompile.h. To make your code easierto share with other projects, avoid including this file explicitly
(except inprecompile.cc), and use the /FI compiler option to include the fileautomatically.
4.&&&&Resource headers, which areusually named resource.h and contain only macros, do not need to conform tothese style guidelines.
但是,在Windows中还是有些场合我们必须打破某些规则:
1.&&&&通常我们禁止使用多个有实现的继承;但在用COM和一些ATL/WTL类时需要这么做。这时你就可以使用多个有实现的继承了。
2.&&&&尽管你不应该在自己的代码中用到异常,但异常被广泛用在ATL和一些VC++的STL实现中。用ATL时,你应该定义_ATL_NO_EXCEPTIONS来禁用异常。你应该研究一下是否也可以禁用STL中的异常,如果不能的话启用异常也没关系。(注意这只是为了编译STL,自己的代码中仍然不允许用异常。)
3.&&&&Windows代码文件中通常都包括预编译好的头文件StdAfx.h或precompile.h。为了让你的代码方便与其它项目分享,避免显式include此文件(除了precompile.cc),用/FI编译选项来自动包括此文件。
4.&&&&资源头文件通常被命名为resource.h且只包括宏,不需要遵守这些风格规范。
5 PartingWords 结束语
Use common senseand BE CONSISTENT.
运用常识,并保持一致。
If you areediting code, take a few minutes to look at the code around you and determineits style. If they use spaces around their if clauses, you should, too. Iftheir comments have little boxes of stars around them, make your comments havelittle boxes of
stars around them too.
如果你在编辑代码,花一些时间看看周围的代码再决定用哪种风格。如果它们在if分句前后留有空白,你也应该这么做。如果它们的注释用星号组成的矩形框包起来,你也这么做。
The point ofhaving style guidelines is to have a common vocabulary of coding so people canconcentrate on what you are saying, rather than on how you are saying it. Wepresent global style rules here so people know the vocabulary. But local styleis also important.
If code you add to a file looks drastically different fromthe existing code around it, the discontinuity throws readers out of theirrhythm when they go to read it. Try to avoid this.
风格指南的重点是提供一个通用的编程规范,这样人们就可以专注于实现内容,而不是表达方式。我们展示了全局的风格,但局部风格也很重要。如果你加进一个文件的代码与原有代码完全不同,这种不连续会破坏读者阅读的节奏。试着避免这种情况。
OK, enoughwritin the code itself is much more interesting. Have fun!
好吧,关于编程规范写的够多了;代码本身更有趣。尽情享受吧!
&但一行超过80个字符后还是要分行的
&vector.size()
&我的理解是会给程序引入一些数据或主动的改变程序状态的代码,不是验证性的
&注释要言简意赅, 不要拖沓冗余
&TODO 很不错, 有时候, 注释确实是为了标记一些未完成的或完成的不尽如人意的地方, 这样一搜索, 就知道还有哪些活要干, 日志都省了
&意思是用UTF-8就只会看到一个空格,就丢失了16进制能表现的含义了?
&这条其实不太能接受,看看别的规范怎么说
&指左括号,右括号可能因为放不下在别的行
&指左大括号
&为了避免读者漏掉&&,还能让后面各行看着没那么别扭
&用宏实现,应该是来自basic和pascal的习惯
&意思就是不要这么用
&推荐用()进行初始化,对于类对象,如果用=相当于是构造函数+赋值函数两项的开销
&避免被识别为&&操作符
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:29408次
排名:千里之外
原创:40篇
(3)(20)(1)(9)(3)(6)(5)(4)(2)

我要回帖

更多关于 do not leave me 的文章

 

随机推荐