Chapter 1 program LayoutAlthough the layout does not affect the functions of the program, it affects readability. The pursuit of clear and beautiful program layout is an important component of the Program style. The program layout can be compared to "Calligraphy ". Good "Calligraphy" makes people have a clear view of the program, and they are very interested. The poor program "Calligraphy" is like a crab crawling, which makes people look tasteless and makes defenders even more worried. It is necessary to ask programmers to learn the "Calligraphy" of the program to make up for the loopholes in university computer education.2.1 blank linesEmpty rows separate program paragraphs. The program layout will be clearer if there are few blank lines. Empty rows do not waste memory. Although printing programs containing empty rows will consume more paper, it is worth it. So do not be reluctant to use empty lines. LRule 2-1-1]Empty rows must be added after each class declaration and after each function definition. See example 2-1 (a) lRule 2-1-2]In a function, there are no empty rows between closely related statements on the zookeeper, and empty rows should be separated elsewhere. See example 2-1 (B)
// Void function1 (...) {...} // Void function2 (...) {...} // Void function3 (...) {...} |
// Empty row while (condition) {statement1; // empty row if (condition) {statement2;} else {statement3;} // empty row statement4 ;} |
Example 2-1 (a) Empty rows between functions Example 2-1 (B) Empty rows inside the Function2.2 code linesLRule 2-2-1]One line of code only does one thing, such as defining only one variable or writing only one statement. Such code is easy to read and easy to write comments. LRule 2-2-2]If, for, while, do, and other statements occupy one row, the execution statement must not be followed by it. {} Must be added no matter how many statements are executed {}. This prevents writing errors. Example 2-2 (a) is a good-style code line, and Example 2-2 (B) is a bad-style code line.
Int width; // width int height; // height int depth; // depth |
Int width, height, depth; // width, height, and depth |
X = a + B; y = C + D; Z = e + F; |
X = a + B; y = C + D; Z = e + F; |
If (width |
If (width |
For (initialization; condition; update) {dosomething () ;}// empty row other (); |
For (initialization; condition; update) dosomething (); other (); |
Example 2-2 ()Good styleCode line Example 2-2 (B)Poor StyleLine 2[2-2-1 is recommended.]Whenever possible, initialize the variable while defining the variable (proximity principle). If the reference of the variable is far away from its definition, the initialization of the variable is easy to forget. If an uninitialized variable is referenced, a program error may occur. This suggestion can reduce risks. For example, int width = 10; // define and normalize the width int Height = 10; // define and optimize the height int depth = 10; // define and optimize the depth2.3 spaces in the code lineLRule 2-3-1]Leave a space after the keyword. Leave at least one space after keywords such as const, virtual, inline, and case. Otherwise, keywords cannot be analyzed. For example, if, for, while, and other keywords, leave a space with the left brace '(' to highlight the keyword. LRule 2-3-2]Do not leave a space after the function name, followed by the left brace '(', to be different from the keyword. LRule 2-3-3]'('Backward followed,') ','; 'follow forward without spaces. LRule 2-3-4]',' And then leave a space, such as function (x, y, z ). If ';' is not the end symbol of a row, leave a space after it, such as for (initialization; condition; update ). LRule 2-3-5]Value assignment operator, comparison operator, arithmetic operator, logical operator, bitfield operator, such as "=", "+ =" "> =", "<=", "+", "*", "%", "&", and "|", "<", "^" and other binary operators should be preceded by spaces. LRule 2-3-6]The unary operator is like "!". , "~" , "+ +", "--", "&" (Address operator) and so on without spaces. LRule 2-3-7]No spaces are added before and after operators such as "[]", ".", and "->. 2.[2-3-1 is recommended.]For long for statements and if statements, some spaces can be removed for compact purposes, such as for (I = 0; I <10; I ++) and if (A <= B) & (c <= D ))
Void func1 (int x, int y, int Z); // good style void func1 (int x, int y, int Z); // bad style |
If (Year> = 2000) // good style if (Year> = 2000) // bad style if (A> = B) & (c <= D) // good style if (a> = B & C <= d) // bad style |
For (I = 0; I <10; I ++) // good style for (I = 0; I <10; I ++) // bad style for (I = 0; I <10; I ++) // excessive Spaces |
X = A <B? A: B; // good style x = A <B? A: B; // bad style |
Int * x = & Y; // good style int * x = & Y; // bad style |
Array [5] = 0; // do not write it as array [5] = 0;. function (); // do not write it as. function (); B-> function (); // do not write B-> function (); |
Example 2-3 space in the code line2.4 alignmentLRule 2-4-1]The program's delimiters '{' and '}' should have an exclusive row in the same column and be left aligned with the statements that reference them. LRule 2-4-2]The code block in {} is left aligned at the right digit. Example 2-4 (a) indicates that the style is well aligned, and Example 2-4 (B) indicates that the style is poorly aligned.
Void function (int x ){... // Program code} |
Void function (int x ){... // Program code} |
If (condition ){... // Program code} else {... // Program code} |
If (condition ){... // Program code} else {... // Program code} |
For (initialization; condition; update ){... // Program code} |
For (initialization; condition; update ){... // Program code} |
While (condition ){... // Program code} |
While (condition ){... // Program code} |
If nested {} appears, use indent alignment, for example :{... {... }...} |
|
Example 2-4 ()Good styleAlignment Example 2-4 (B)Poor StyleAlignment2.5 split long linesLRule 2-5-1]The maximum length of a code line should be 70 to 80 characters. The code line should not be too long; otherwise, it will not be easy to print. LRule 2-5-2]A long expression is split into new rows at the lower-priority operator, which is placed at the beginning of the new line (to highlight the operator ). The new lines to be split should be properly indented to make the layout neat and the statement readable.
If (Response >=very_longer_variable12) & (very_longer_variable3 <= very_longer_variable14) & (Response <= very_longer_variable16) {dosomething ();} |
Virtual cmatrix cmultiplymatrix (cmatrix leftmatrix, cmatrix rightmatrix ); |
For (very_longer_initialization; very_longer_condition; very_longer_update) {dosomething ();} |
|