Google C + + code specification reading notes

Source: Internet
Author: User

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:
    1. Typedefs and enumerations
    2. Constant
    3. constructor function
    4. Destructors
    5. member functions, with static member functions
    6. 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

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.