High-quality C++/C Programming Guide-2nd Chapter-Program Layout

Source: Internet
Author: User
Tags arithmetic operators execution function definition logical operators split

The 2nd chapter of the program layout layout does not affect the function of the program, but it will affect the readability. The layout of the program pursues clear and beautiful, which is an important component of the procedural style.

You can compare the layout of the program to "calligraphy". Good "calligraphy" can make people have a clear view of the program, look at the cheerful. Poor program "calligraphy" such as the crab crawling, let a person see uninteresting, more so that defenders worry. It is necessary for programmers to learn the "calligraphy" of the program and make up for the loophole of computer education in university.

2.1 Blank lines play the role of separating the program passages. An empty line (not too much, but less) will make the program more clearly laid out. A blank line does not waste memory, although printing a program with a blank line consumes some more paper, but it is worth it. So don't be reluctant to use empty lines.

L "Rule 2-1-1" adds blank lines after each class declaration and after the end of each function definition. See Example 2-1 (a)

L "Rule 2-1-2" in a function body, the Logos Yi the closely related statement does not add the blank line, other place should add the blank line to delimit. See Example 2-1 (b)

Blank Line

void Function1 (...)

{

...

}

Blank Line

void Function2 (...)

{

...

}

Blank Line

void Function3 (...)

{

...

}

Blank Line

while (condition)

{

Statement1;

Blank Line

if (condition)

{

Statement2;

}

Else

{

Statement3;

}

Blank Line

STATEMENT4;

}

Example 2-1 (a) a blank row between functions 2-1 a blank line inside a function (b)

2.2 Code line L "rule 2-2-1" one line of code to do only one thing, such as defining only a variable, or writing only one statement. Such code is easy to read and easy to write annotations.

L "Rule 2-2-2" if, for, while and do statements from one line, the execution statement must not be followed. Add {} Regardless of how many execution statements are executed. This will prevent writing errors.

Example 2-2 (a) is a style-good line of code, and example 2-2 (b) is a poorly styled line of code.

int width;//width

int height;//height

int depth;/deep Degree

int width, height, depth;//width height

X = a + B;

Y = c + D;

Z = e + F;

X = a + b; y = c + D; z = e + F;

if (width < height)

{

dosomething ();

}

if (width < height) dosomething ();

for (initialization; condition update)

{

Dosomethin g ();

}

//blank line

Other ();

for (initialization; condition; update)

DoSomething ();

Other ();

Example 2-2 (a) example of a style-good line of code 2-2 (b) line of code with bad style

2 "recommendation 2-2-1" Initialize the variable as much as possible while defining the variable (proximity principle)

If the reference of a variable is far away from its definition, initialization of the variable can easily be forgotten. If you reference a variable that is not initialized, you may cause a program error. This recommendation can reduce the risk. For example

int width = 10; Define and start the width of the first

int height = 10; Define and first-turn the height

int depth = 10; Define and initialize depth

2.3 Space in the code line L "rule 2-3-1" after the keyword to leave blank. Like the const, virtual, inline, case and other keywords after at least a space, or can not discriminate keywords. You should leave a space followed by a left parenthesis ' (') like if, for, while, to highlight the keyword.

L "Rule 2-3-2" after the function name do not leave blank, followed by the opening parenthesis ' (', with the keyword distinction.

L "Rule 2-3-3" ' (' Backward, ') ', ', ', ', '; ' Go ahead and keep the space blank.

L "Rule 2-3-4" ', ' then leave blank, such as Function (x, Y, z). If '; ' is not a line ending symbol, followed by a blank, such as for (initialization; condition; update).

L "Rule 2-3-5" assignment operator, comparison operators, arithmetic operators, logical operators, bit field operators, such as "=", "+ +" ">=", "<=", "+", "*", "%", "&&", "| |", "&L t;< "," ^ "and other two-yuan operators should be preceded and preceded by a space.

L "rule 2-3-6" unary operators such as "!" "," ~ "," + + ","--"," & "(address operator), and so on without spaces.

L "rule 2-3-7" like "[]", ".", "->" operators are not preceded by spaces.

2 "recommendation 2-3-1" for expressions that are longer for statements and if statements, you can appropriately remove some spaces for the sake of compactness, such as for (I=0 i<10; i++) and if ((a<=b) && (C<=d)

void Func1 (int x, int y, int z); A 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 space

x = a < b? A:B; A good style

x=a<b?a:b; Bad style.

int *x = &y; A good style

int * x = & y; Bad style.

ARRAY[5] = 0; Do not write array [5] = 0;

A.function (); Don't write A. Function ();

B->function (); Do not write B-> Function ();

Example 2-3 a space within a line of code

2.4 Align L "rule 2-4-1" program's delimiters ' {' and '} ' should be exclusive and in the same column, and left aligned to the statement that references them.

The code blocks within the L "rule 2-4-2" {} are aligned to the left of the ' {' Right grid.

Example 2-4 (a) is a good alignment and example 2-4 (b) is a poorly styled alignment.

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 a nested {} appears, the indent alignment is used, such as:

{

...

{

...

}

...

}

Example 2-4 (a) good-style alignment to alignment case 2-4 (b) style

2.5 Long line split l "rule 2-5-1" code line maximum length should be controlled within 70 to 80 characters. The line of code should not be too long, or the eyes can not see, nor easy to print.

L "rule 2-5-2" long expressions are split into new rows at the lower-priority operator, and the operator is placed at the top of the new line (to highlight the operator). The new line to be split is properly indented to make the layout neat and the statement readable.

if ((Very_longer_variable1 >= very_longer_variable12)

&& (very_longer_variable3 <= very_longer_variable14)

&& (very_longer_variable5 <= very_longer_variable16))

{

DoSomething ();

}

Virtual Cmatrix Cmultiplymatrix (Cmatrix Leftmatrix,

Cmatrix Rightmatrix);

for (very_longer_initialization;

Very_longer_condition;

Very_longer_update)

{

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.