Google C ++ code specification-translation learning 1

Source: Internet
Author: User

Google CPP programming specifications-I can't remember how much I watch every day. I changed my skills in practice.CodeStyle!

The name of the CPP file is. CC rather than. cpp. This is not very clear, why not use CPP, but CC

Http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Scoping

1. header file # define protection

Each header file should have # define to prevent being contained multiple times. The definition should be the complete path in the project directory, such as file Foo/src/BAR/Baz. h The following methods should be used to protect project FOO:

# Ifndef foo_bar_baz_h _

# Define foo_bar_baz_h _

...

# Endif // foo_bar_baz_h

 

Header file dependency

Do not use # include

When a header file is referenced, a dependency is established. When the contained header file is changed, other files will be re-compiled. So try to avoid using # include

You can use the Forward Declaration to reduce the use of # include. For example, when your header file uses the file class but does not need to access its declaration, you can use the Forward Declaration instead of # include "file/base/file. h"

So when do I use a class Foo in the header file without accessing its definition?

1. Declare the data member Foo * or Foo &

2. We can declare function parameters or return values using type Foo.

3. A static member Foo can be declared, because the definition of a static member is outside the class.

However, if the class contains data members of the foo type, # include is required.

Sometimes it makes sense to use pointer members instead of object members.ProgramThe readability and performance are reduced. Therefore, it is not worthwhile to force pointer members only to use # include less.

Note: If the symbol foo is used in the source file, the definition of Foo should be imported or declared through # include or forward.

 

Inline functions

When the code is small and there are less than 10 lines, you can use inline functions.

Definition

You can declare a function as inline so that the compiler can expand them at the position where the function is called, rather than using the general function call mechanism.

Supported practices

Inline can improve the efficiency of the target code, so small functions should use inline as much as possible, such as the set get method or other code with high efficiency requirements.

Not Supported

If the Code contains more than 10 lines, do not use inline. Pay attention to destructor, because the call of implicit member functions and basic class destructor usually leads to more than 10 lines of code.

Do not use inline functions for loops and switches.

In some cases, functions may not be inline despite inline declarations. Such as virtual functions and recursion. Generally, a virtual function is used to define a class, rather than facilitating or implementing its behavior.

 

-InL. h file

When necessary, you can use the inL. h file to define complex inline functions.

Short inline functions can be directly defined in. H, such as the get set method. If the code is long, you do not want to put the inline function definition in. -inL. h file to define the inline function, which enables the inline implementation and class implementation to be divided, and allows the implementation to be included as needed.

-Another purpose of inL. H is to define a template, which makes it easy to understand the template definition. Like General. H, use # define guard

 

Sort function parameters

Parameter sorting: Input and Output

The C/C ++ parameter is either input or output, or input or output (for example, pointer or reference ). The input parameter is often a value or a reference, and the output is a non-constant pointer. When declaring a parameter, the input parameter is in front of the parameter. Do not add the parameter after the diagram to ensure that the input parameter is in front of the parameter.

 

Names and sorting of DES

Use standard sorting to ensure readability, while avoiding hidden dependencies: C library, C ++ library, other libraries, custom. h

For. CC files, such as foo2.cc

1. dir2/foo2.h

2. C System Library

3. cpp Library

4. other libraries

5. Other projects. h

For example


 

# Include "foo/public/foosever. H" // corresponding. h

# Include <sys/types. h> // C library

# Include <unistd. h>

# Include

# Include <vector>

 

# Include "Base/basictypes. h" // other projects. h

# Include "Base/commandlineflags. h"

# Include "foo/public/bar. H"

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.