High-quality C ++/C Programming Guide-Chapter 1 program Layout

Source: Internet
Author: User

although 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 empty 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.

[Rule 2-1-1] Empty rows must be added after each class declaration and after each function definition. See example 2-1 (a)

[Rule 2-1-2]. In a function, no blank lines are added between statements that are closely related to the limit operator, separate empty rows in other places.

See example 2-1 (B) </P> <p> Example 2-1 (B) Empty rows inside the Function
/// Empty rows
Void function1 (...)
{
...
}
// Empty rows
Void function2 (...)
{
...
}
// Empty rows
Void function3 (...)
{
...
}

Example 2-1 (B) Empty rows inside the Function
/Empty rows
While (condition)
{
Statement1;
// Empty rows </P> <p> If (condition)
{
Statement2;
}
Else
{
Statement3;
}
// Empty rows
Statement4;
} </P> <p> 2.2CodeLine
L

[Rule 2-2-1] a line of code only does one thing, for example, defining only one variable or writing only one statement. Such code is easy to read and easy to write comments. </P> <p> L

[Rule 2-2-2] If, for, while, do and other statements occupy a row, the execution statement cannot 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. </P> <p> Example 2-2 (a) code lines with good style
Int width; // width
Int height; // height
Int depth; // depth
X = A + B;
Y = C + D;
Z = e + F;
If (width {
Dosomething ();
}
For (initialization; condition; Update)
{
Dosomething ();
}
// Empty rows
Other (); </P> <p> Example 2-2 (B) bad code lines
Int width, height, depth; // width, height, and depth
X = a + B; y = C + D; Z = e + F;
If (width For (initialization; condition; Update)
Dosomething ();
Other ();

[2-2-1] It is recommended to 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 normalize the height
Int depth = 10; // define and initialize depth </P> <p> 2.3 spaces in the code line
[Rule 2-3-1] enter 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. </P> <p>

[Rule 2-3-2] Do not leave a space after the function name, followed by the left brace '(', which is different from the keyword. </P> <p>

[Rules 2-3-3] '('backward followed,') ','; 'follow forward, followed by no spaces. </P> <p>

[Rule 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 ). </P> <p>

[Rules: 2-3-5] value assignment operators, comparison operators, Arithmetic Operators, logical operators, bit-field operators, such as "=", "+ =" "> =", "<=", "+", "*", "%", "&", "│", "<", "^" and other binary operators should be preceded by spaces. </P> <p>

[Rule 2-3-6] The unary operator is like "!" , "~" , "+ +", "--", "&" (Address operator) and so on without spaces. </P> <p>

Rules 2-3-7 do not contain spaces before and after operators such as "[]", ".", and "->. </P> <p>

[2-3-1] For the for statements with long expressions and if statements, we recommend that you remove some spaces for compact purposes, as shown in figure
For (I = 0; I <10; I ++) And if (A <= B) & (c <= D ))
Bytes ------------------------------------------------------------------------------------
Void func1 (int x, int y, int Z); // good style
Void func1 (int x, int y, int Z); // bad style
Bytes ------------------------------------------------------------------------------------
If (Year> = 2000) // good style
If (Year> = 2000) // bad style
Bytes ------------------------------------------------------------------------------------
If (A> = B) & (c <= D) // good style
If (A> = B & C <= d) // bad style
Bytes ------------------------------------------------------------------------------------
For (I = 0; I <10; I ++) // good style
For (I = 0; I <10; I ++) // bad style
For (I = 0; I <10; I ++) // too many spaces
Bytes ------------------------------------------------------------------------------------
X = A <B? A: B; // good style
X = A <B? A: B; // bad style
Bytes ------------------------------------------------------------------------------------
Int * x = & Y; // good style
Int * x = & Y; // bad style
Bytes ------------------------------------------------------------------------------------
Array [5] = 0; // do not write it as array [5] = 0;
A. function (); // do not write a. function ();
B-> function (); // do not write B-> function (); </P> <p> Example 2-3 spaces in the code line </P> <p> 2.4 alignment
[Rule 2-4-1] the program's delimiters '{' and '}' Should exclusive a row in the same column and be left aligned with the statements that reference them. </P> <p>

The code block in [Rule 2-4-2] {} 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. </P> <p> Example 2-4 (a) well-aligned Style
Void function (int x)
{
... // Program code
}
If (condition)
{
... // Program code
}
Else
{
... // Program code
}
For (initialization; condition; Update)
{
... // Program code
}
While (condition)
{
... // Program code
}
If nested {} appears, use indent alignment, for example:
{
...
{
...
}
...
} </P> <p> Example 2-4 (B) Incorrect style alignment
Void function (int x ){
... // Program code
}

If (condition ){
... // Program code
}
Else {
... // Program code
}
For (initialization; condition; update ){
... // Program code
}
While (condition ){
... // Program code
}
</P> <p> 2.5 split long rows
[Rule 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. </P> <p> L

[Rule 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 (very_longer_variable1> = very_longer_variable12)
& (Response <= very_longer_variable14)
& (Response <= very_longer_variable16 ))
{< br> dosomething ();
}< br> virtual cmatrix cmultiplymatrix (cmatrix leftmatrix,
cmatrix rightmatrix);
for (very_longer_initialization;
very_longer_condition;
very_longer_update)
{< br> dosomething ();
}

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.