Good coding style habits collation

Source: Internet
Author: User

1. Add empty lines after each class declaration fire, after each function definition ends

2, if, for,while,do and other statements from the line, the execution statement must not be followed. No matter how many statements are executed with {}, it is convenient to read the code and prevent mistakes in writing.

3, as far as possible to define the variable at the same time to initialize the variable (nearest principle), the pointer variable for the header file is best to assign null in the constructor function

4, ' (' Back immediately, ', ', ') ', '; ' Straight ahead, with no spaces to follow

5, ', ' after the empty lattice, such as function (x, y, z), if '; ' is not a line of the end symbol, followed by a blank lattice, such as for (int i = 0; i<n; ++i)

6, for "=", "+ =", ">=", "%", "| |", "<<" and other two-dollar operators should be added space before and after

7, for "!", "~", "+ +", "--", "&" (take address) and other unary operators before and after the space

8, for the expression of long for statements and if statements, in order to compact can be appropriate to remove some spaces, such as for (i=0; i<10;++i) and if ((a<=b) && (C<=d))

9. Long expressions should be split into new lines at the lower priority operator, and the operator will be highlighted at the beginning of the line. New lines to be split properly indented, neatly formatted

Such as:

if ((Very_longer_varible1 >= very_longer_varible12)

&& (very_logner_varible2 >= very_longer_varible22)

&& (Very_longer_varible3 >= very_longer_varible32))

10. Variable naming rules preferred by Microsoft Company's "Hungarian" method: variable name = attribute + Type + object, if pointer variable plus prefix p, constant full capital const int MAX = 100, other variables in a similar way

11. Do not compare floating-point variables with "= =" or "! =" with any data, the correct way: if ((X>=-epsinon) && (X<=epsinon)), Epsinon is the permissible error, i.e. precision

12, if judgment if (bool) or if (int ==0) or if (P==null), p is the pointer

13, in the multi-cycle, if allowed, the longest cycle should be placed in the most inner layer, the shortest loop on the outermost, to reduce the number of CPU cross-cutting cycle

14, generally put the purpose parameter in front, the source parameter is placed in the back: void Stringcopy (char *strdestination, char *strsource)

15, if the input parameters passed the object by value, it should be passed with "Const &", saving the construction and destruction process of temporary object, and improving the efficiency.

16, the function body size to be small, as far as possible to control within 50 lines, the function of low coupling convenient call

17. Using assertions, assert only works in the debug version of VS, helping us to find out why the error occurred, using assertions to make explicit comments

18, after malloc or new application memory, should immediately check whether the pointer value is NULL, using the pointer to empty memory, free or delete after releasing memory, immediately set the pointer to NULL, prevent the "wild pointer"

19.

Good coding style custom collation

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.