Google Open source Project style Guide read notes (c + +)

Source: Internet
Author: User

Although the programming style guide, but a lot of dry, very many C + + useful technology in it.

Header file

Usually each. cpp file corresponds to a. h file;
#define保护
All header files should use # define to prevent header files from being included in multiple, and for uniqueness, header files should be named according to the full path of the source code tree in which they are located.

Where you can use the front-facing declaration, try not to include
When a header file is included in the same time also introduced a new dependency, once the header file is changed, the code will be compiled again, assuming that the header file also includes other header files, any changes in these files will cause all the code including the header file is compiled again.

Assuming that the header file uses the class file, but does not need to access the file class declaration, the header file only needs to be pre-declared class file, without # include.

Sometimes it's wise to use pointer members instead of object members, which can then reduce code readability and efficiency.

inline functions
Only when the function is only 10 rows or less is defined as an inline function, performance-critical functions, recommended inline, and cannot include a switch statement.

The definition of a complex inline function should be placed in a header file with the suffix named Inl.h.

Define the function, the order of the parameters are: input parameters, output parameters, input parameters of the general value or a const reference, the output parameters are non-const pointers.

#include的路径和顺序
Using standard header files including the order can enhance readability and avoid hiding dependencies. Order: C library, C + + library, other libraries. h, this project. H.
including file order In addition to aesthetics, the most important thing is to reduce the hidden dependencies, so that each header file in the "most need to compile the place to compile."

Scope
encouraged in. cpp files using the anonymous name control, the name of the appliance name can be based on the project name or relative path, and do not use Usingkeyword.
Namespaces subdivide global scopes into independent, named scopes that effectively prevent naming conflicts for global scopes.
Use namespaces to encapsulate files, gflags declarations and definitions, and entire files other than the predecessor declarations of a class, in order to discriminate among other named controls.
It is best not to use Usingkeyword to ensure that all names under the namespace are working properly.
Usingkeyword can be used in functions, methods, and classes in. cpp and. H.

Nested classes
While it is common to have nested classes as part of an interface, while being able to keep them in the global scope directly, it is better to place the declaration of the absconded class in the namespace.
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.

Non-member functions, static member functions, and global functions
Using static member functions or non-member functions within namespaces, try not to use full-naked global functions, assuming that you must define a non-member function, and simply use it in a. cc file to qualify its scope with an anonymous namespace or static link keyword.

Local variables
The function variable is placed as small as possible within the scope and initialized when the variable is declared. To place an object in a loop, each entry is constructed and left to be deconstructed.

Static and global variables
It is forbidden to use static or global variables of class type, which can cause very difficult to find bugs and indeterminate construction and destructor invocation order.
Never initialize static variables with function return values; Do not use non-const static variables in multithreaded code.
The scope is used in addition to the name pollution, readability, mainly to reduce the coupling, improve the compile/run efficiency. Generally do not overload operators, the disadvantage is confusing, difficult to debug. If necessary, you can define functions like equals (), CopyFrom ().

Declare all data members as private, and provide the appropriate access function as required, usually in the header file access function is defined as inline.

The Access control section of the class is declared in the order of Public->protected->private. Declaration order for each section: member functions, constructor, destructors, constants, typedefs and enumerations Data members.

functions as short, compact, and functional as possible.
Assuming that the function exceeds 40 lines, it is possible to consider whether it can be cut without affecting the structure of the program.

Try to avoid the use of smart pointers, when used as far as possible localized, safety first.
Assume that you must use the smart pointer, scoped-ptr fully competent, only in very specific circumstances to use the shared-ptr, no matter when not with auto_ptr;

Other C + + features
All references by reference must have a const value.
Input parameters are either a value or a const reference, and the output parameter is a pointer.
Only if the input parameter type is different, the function also uses overloaded functions, do not use function overloading to simulate the default function parameters.
Specify that all the parameters must be clearly specified, forcing the ape to understand the meaning of the API and the respective values, and avoid being used without deep understanding of the function.
Instead of using variable-length arrays and alloc (), it is recommended that you use a secure allocator.
Friends extend the wrapper boundaries of a class, and in some cases, the relative domain declares the class member as public, making it a better choice, especially if you only agree that there is one class to access the private members of the class.

Hide, overwrite, and reload
Hide-some functions in the base class, assuming that there is no virtualkeywordkeyword, and the function name in its base class, then in the subclass, all functions with the same name of the base class will be hidden.
Override-The virtual function of the derived class overrides the same function with the same name as the base class, which is exactly the same as the function name, the number of parameters, and the return type.
Functions of the same function name, the relationship between the base class and the derived class can only be overridden or hidden.

Overloaded-must be in a domain, the function name is the same, but the function parameters are different.

Unless the unit tests, do not use rtti, assuming that the origins of their own to write some behavior logic depends on the object type of code, consider a different way to infer the object type.

Type conversions do not use C-style type conversions, but should use C + + styles.
1.static_cast C-style value conversion, or a class pointer to understand upward conversion to a parent pointer;
2.const_cast remove the const attribute;
3.reinterpret_cast A pointer type and an integer type or other pointers for unsafe mutual conversions;
4. Do not use dynamic_cast outside the test code.

Instead of using streams, there are a lot of pros and cons to using streams, but code consistency is better than anything else, except that the log interface is required, using a substitution like printf.

For iterators and other template objects, use the prefix form (++i) for the self-increment/decrement operator.

A constant is named before the name plus K.

Using const in whatever possible case, the class function plus const indicates that the class does not alter the state of the member variable.

It is assumed that the data member will not change after the object is constructed, defining it as Const.

Use assertions to indicate that a variable is non-negative, rather than using an unsigned type.

It is assumed that the data member will not change after the object is constructed, defining it as Const.

Be very cautious when working with macros, instead of inline functions/enumerations/constants as much as possible.

Macros mean that you and the compiler see the code differently, which can cause unexpected behavior, especially when it has global scope.

Do not define macros in. h files
Do not attempt to use a macro that will cause C + + instability after expansion, at least with the documentation description behavior.

File gaze can show off your accomplishments, and others can find you in order to poke a bucket.

Each line of code has no more than 80 characters.

Just use a space as the indent character.

function declaration and implementation of all the formal participation, the name must be consistent.

The return expression is not enclosed in parentheses.







Google Open source Project style Guide read notes (c + +)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.