Efficient C + + planning

Source: Internet
Author: User


It is recommended to write C + + code style. Seemingly easy. Insist on not easy, and write and cherish!

--Chen Guolin

1. Version number and version number declaration

The version number and version number file declarations are located at the beginning of the header file and the definition file, and the main content

(1) Version number information

(2) file name, identifier, summary

(3) Current version, author, date of change

(4) Version number history information

2. Program Layout

(1) After each class declaration, after each function definition, add a blank line

(2) One line of code is just one thing.

(3) If, for, while, do each occupy a row. The run statement must not be followed, regardless of how many statements are run with {}

(4) Initialize the variable as much as possible when defining a variable

(5) The program delimiter {and} must be exclusive and left justified

(6) Code length should not exceed 70 to 80 characters. Break into a new line later. operator on the top of the new line

(7) the * and & modifiers should be close to the variable name, such as int *x = &a

(8) class is recommended to first write the public type in front, and then write the private type

3. Naming rules

(1) The class name and function name are made up of words in uppercase letters

(2) Variables and arguments should be made up of words with lowercase letters

(3) All the constants are in uppercase letters with an underscore to cut the words

(4) static variable plus prefix s_. such as the static int s_num

(5) Global variables prefixed with g_, such as int g_num

(6) The data member of the class is preceded by a m_, and can be separated from the member function and the static member

4. Expressions and Basic statements

(1) The BOOL value cannot be compared directly with true, False, 0, 1.

Assuming that flag is of type bool, direct if (flag) inference can

(2) Integer variables should be directly compared to 0, for example, num is an integer variable. if (num = = 0)

(3) The floating-point variable cannot be compared directly with = = and! =

(4) Pointer variable to be directly and NULL comparison, such as p is a pointer variable, if (p! = NULL)

5. Function design

(1) The writing of function parameters should be complete, assuming that there are no parameters, fill with void

(2) Assume that the parameter has a pointer, and that the pointer is only a read action should be added to the const, to prevent accidental changes in the pointer

(3) The validity of the parameters should be checked in the internal entrance of the function.

(4) function should have a single function. Do not design multi-purpose functions

(5) The size of the function body is small. Control in 50 lines of code

6. Memory Management

(1) After malloc and new have applied for memory, you should immediately check if the pointer is null and prevent the use of memory with a pointer value of NULL.

(2) To assign an initial value to the array and dynamic memory. Prevent uninitialized memory from being used as a right value

(3) After releasing memory with free or delete. Immediately point the pointer to null to prevent the wild pointer from being generated.



Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

Efficient C + + planning

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.