C ++ programming skills (2)

xiaoxiao2021-03-05  27

Second, the format of the program

Interval

[Rule 2-1-1] After each class declaration, the rows must be added after each function definition. [Rule 2-1-2] In a function body, the lack of interval between the lack of relevant statements, and other places should be separated. Single line of format

Single line of format

[Rule 2-2-1] One-line code only does one thing, if only one variable is defined, or write only a statement. Such a code is easy to read, and it is convenient to write a comment. [Rule 2-2-2] If, for, while, do, other words, the execution statement must not follow. No matter how much the execution statement is, {}. This prevents writing errors. [Recommendation 2-2-1] To initialize this variable while defining variables (in close proximity) If the number of variables is far apart and its definition is relatively far, the initialization of variables is easily forgotten. If the variable that is not initialized is referenced, it may cause a program error. This recommendation can reduce hidden dangers. The use of spaces [Rule 2-3-1] will leave spaces after keywords. At least one space is left after const, virtual, inline, case, etc., otherwise the keyword cannot be analyzed. Like if, for, while, I should leave a space and the left bracket '(' to highlight keywords. [Rule 2-3-2] Do not leave the space after the function name, keep track of the left bracket '(', In line with keywords. [Rule 2-3-3] '(' Tie, ')', ',', ';'; ',', close, close to space. [Rule 2-3 -4] ',' After you have space, such as Function (x, y, z). If ';' is not the end symbol, then leave space, such as for (Initialization; Condition; Update). [Rule 2 -3-5】 Assign value, compare operator, arithmetic operator, logical operator, bit domain operator, such as "=", " ="> = "," <= "," "," * ","% "," && "," │ │ "," << "," ^ "should be added before and after the binary operator. [Rule 2-3-6] One yuan operator is as"! " , "~", " ", "-", "&", no vacancy before and after. <【Rule 2-3-7】 "[]", ".", " > "This type of operator does not add space before and after. [Recommendation 2-3-1] For a longer FOR statement and if statement, in order to properly remove some spaces, such as for (i = 0; i < 10; i ) and IF ((a <= b) && (c <= d)) [Rule 2-4-1] Division '{' and '}' should be exclusive and in the same column, at the same time References their statements left align. The code blocks within {} are aligned at the left of the '{' right side. If nest {}, indent alignment [rule 2- 5-1】 The maximum length of the code line should be controlled within 70 to 80 characters. Don't be too long, otherwise you can't see it, it's not convenient to print.

L [rule 2-5-2 The long expression is to be split into a new line at a low priority operator, and the operator is placed in the first of the new row (in order to highlight the operator).

转载请注明原文地址:https://www.9cbs.com/read-37719.html

New Post(0)