Objective
Today quickly turned to Google C + + coding specifications http://zh-google-styleguide.readthedocs.org/en/latest/google-cpp-styleguide/contents/, learned something.
For this specification, I will not receive 100%, such as the variable naming specification inside the work item code conflicts with, and Google suggested that the switch in each block to add {} and so on.
While I was reading the document, I recorded some of the new things I had learned, and some of the coding habits I used to be accustomed to, I stopped recording, for example, the destructor was to use the virtual keyword, and sizeof (varname) instead of sizeof (type). The excerpt here is a recommendation that I think is universal.
Body
- All header files should be used #define prevent header files from being multi-contained, naming the format when: Project_path_file_h_
- The #include can be used as far as possible in the place of the predecessor declaration.
- The definition of a complex inline function should be placed in a header file with the suffix named-inl.h.
- Using the standard header file contains the order to enhance readability and avoid hiding dependencies: C libraries, C + + libraries, and. H of other libraries,. h in this project.
- Do not use anonymous namespaces in. h files.
- Do not define nested classes as public unless they are part of an interface, for example, a nested class contains a set of options for some methods.
- Prohibit the use of static or global variables of class type: they can cause difficult to find bugs and indeterminate construction and destructor invocation order.
- The constructor only makes meaningless initialization, and, if possible, uses the Init () method to centrally initialize meaningful (non-trivial) data.
- declarations in each section of a class are typically in the following order:
- Typedefs and enumerations
- Constant
- constructor function
- Destructors
- member functions, with static member functions
- Data members, with static data members
- We are not allowed to use the default function parameters.
- We prohibit the use of RTTI.
- For iterators and other template objects, use the prefix form (++i) for the self-increment, decrement operator.
- Integers with 0, real numbers with 0.0, pointers with NULL, characters (strings) with ' + '.
- For the naming of functions, never use abbreviations that omit letters, such as the inability to abbreviate count to CNT.
- function declaration Note describes function function; The definition describes the function implementation.
- When passing a NULL, Boolean, or integer to a function, note the meaning of the description, or use constants to make the code look at the text.
- If a function is declared as const, the keyword const should be on the same line as the last parameter.
- If some of the parameters are not used, annotate the parameter names at the function definition:
- Do not surround the return expression with parentheses.
- Preprocessing directives do not indent, starting at the beginning of the line.
Google C + + code specification reading notes