Google C ++ programming style guide Study Notes

Source: Internet
Author: User

Header file

1. Define Protection

The header file name should be based on the projectSource codeThe full path of the tree.

2. header file dependency

Use the forward statement to minimize the number of # include in. H files.

Declarations that can be depended on do not depend on definitions.

3. inline functions

The Inline type is used only when the number of functions is less than 10 or less.

4.-inL. h

The definition of complex inline functions should be placed in the-inL. h file.

5. Function Definition order

The input parameter is before and the output parameter is later.

6. File Inclusion Sequence

C library \ c ++ library \ other libraries in. h \ project. h

 

Namespace

1. namespace

We recommend that you use an unnamednamespaces namespace in the. CC file ).

You cannot use an anonymous namespace in A. H file.

2. nested class)

When exposing a nested class as part of an interface, it is best to place the declaration of the nested class in the namespace.

3. Non-member functions (nonmember), static member functions (staticmember), and Global Functions)

Use non-member or static member functions in a namespace. Do not use global functions whenever possible.

4. Local Variables

Place function variables in the smallest possible scope and initialize them when declaring variables.

5. Global Variables

Class-type Zone Security Supervision Bureau variables are forbidden, built-in types of global variables are allowed, of course, multithreadingCodeNon-Changshu global variables are also forbidden. Never use function return values to initialize global variables.

C ++ class

1. Constructor's Responsibilities

The constructor only initializes meaningless data. If possible, use the init () method to initialize the non-trivial data in a centralized manner.

2. default constructor

If a member variable is defined in the class and no other constructor is provided, You need to define a default constructor. The default constructor is more suitable for initializing objects, so that the internal state (internal state) of objects is consistent and valid.

3. Clear Constructor

The C ++ keyword is used for parameter constructors.

4. copy constructors

Use the copy constructor when you need to copy a class object in the Code. Use disallow_copy_and_assign when you do not need to copy the class object.

5. struct and class

Use struct only when there is only data, and use class for others.

6. Inheritance

It is generally better to use a combination than to use inheritance. If inheritance is used, only public inheritance is used.

7. Multi-Inheritance

There are very few Inheritance needs to be implemented with multiple implementations. Only when at most one base class contains implementations and other classes are pure interface classes suffixed with interfaces will multiple inheritance be used.

8. Operator Overloading

Do not overload operators except for a few specific environments.

9. Access Control

Privatize data members and provide related access functions.

10. Declaration Order

Public before private, the member function is before the data member.

11. Compile Short Functions

Smart pointers and other C ++ features

1. smart pointer

Scoped_ptr is fully qualified if you really need to use smart pointers. In special cases, you should only use shared_ptr, and do not use auto_ptr in any case.

2. Reference parameters

Const must be added to parameters passed by reference.

3. Function Overloading

Use overload functions (called constructor) when different input types and functions are used at the same time. Do not use function overload to mimic default function parameters.

4. Default Parameters

Do not use default function parameters.

5. variable-length array and alloca

Do not use a variable-length array or alloca. Use a safe Allocator allocator.

6. youyuan

Reasonable Use of friend functions and friends functions is allowed.

7. Exceptions

Do not use C ++ exceptions.

8. Run-Time type information (rtti)

Disable rtti.

9. type conversion

Use static_cast <> () and other C ++ types for conversion. Do not use int y = (INT) X or Int y = int (X );

10. Stream)

Use a stream only when logging.

11. preincrement and predecrement)

Use the auto-increment and auto-increment operators in the prefix format (++ I) for the iterator and other template objects.

12. Use of const

We strongly recommend that you use const whenever possible.

13. Integer type

14. 64-bit portability

15. Preprocessing macros

Exercise caution when using macros, and replace them with unassociated functions, enumerations, and constants as much as possible.

16, 0, and null

The integer is 0, the real number is 0.0, the pointer is null, and the character (string) is '\ 0 '.

17. sizeof (sizeof)

Use sizeof (varname) instead of sizeof (type ).

18. Boost Library

Only authorized libraries in boost are used.

Naming Conventions

1. General naming rules

Function naming, variable naming, and file naming should be descriptive and should not be over-abbreviated. Types and variables should be nouns. function names can use the "imperative" verb.

2. File Name:

The file name must be in lowercase. It can contain underscores (_) or hyphens (-).

3. type naming

Type name: each word starts with an uppercase letter.

4. variable naming

All variable names are in lower case, and the words are underlined and connected. The member variables of the class end with an underscore.

5. Constant naming

Add K before the name.

6. Function naming

Common functions are case-insensitive. The access function must match the variable name.

7. namespace

The name of the namespace blank is in all lower case. The name is based on the project name and directory structure.

8. Enumeration naming

The enumerated values should all be in upper case, and the words should be underlined and connected.

9. Macro naming

10. Naming rule exceptions

Code comment

1. Comment Style)

The style should be unified.

2. File comment)

Copyright, license version, author, content

3. Class Annotation

The description of each class must be attached with a comment describing the functions and usage of the class.

4. Todo comments

Use todo annotations for temporary, short-term solutions or code that is good but not perfect.

Format

1. Line Length

The number of characters in each line of code cannot exceed 80.

2. Space or tab

It only uses spaces. Two spaces are indented at a time.

 

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.