General Provisions of software programming specifications-4 readability

Source: Internet
Author: User

4. Readability

Bytes4-1: Pay attention to the operator priority, and use parentheses to clarify the operation order of the expression to avoid using the default priority.

Note: This prevents misunderstandings during reading the program and program errors caused by inconsistent default priorities with the design philosophy.

Example: expression in the following statement

WORD = (high <8) | low (1)

If (A | B) & (A & C) (2)

If (A | B) <(C & D) (3)

If

High <8 | low

A | B & A & C

A | B <C & D

Because

High <8 | low = (high <8) | low,

A | B & A & C = (a | B) & (A & C ),

(1) (2) no errors, but the statements are not easy to understand;

A | B <C & D = A | (B <c) & D, (3) results in an error in the judgment condition.

Limit 4-2: Avoid using numbers that are hard to understand and use meaningful identifiers instead. It involves physical states or constants that contain physical meanings. numbers should not be used directly and must be replaced by meaningful enumerations or macros.

Example: The following program has poor readability.

If (trunk [Index]. trunk_state = 0)

{

Trunk [Index]. trunk_state = 1;

... // Program code

}

 

The format should be changed to the following.

# Define trunk_idle 0

# Define trunk_busy 1

 

If (trunk [Index]. trunk_state = trunk_idle)

{

Trunk [Index]. trunk_state = trunk_busy;

... // Program code

}

Generation 4-1: Codes with close relationships in the source program should be as adjacent as possible.

Note: It is easy to read and find programs.

Example: The following code layout is not reasonable.

Rect. Length = 10;

Char_poi = STR;

Rect. width = 5;

 

It may be clearer if it is written as follows.

Rect. Length = 10;

Rect. width = 5; // the length and width of the rectangle are closely related and put together.

Char_poi = STR;

Limit 4-2: Do not use difficult and skillful statements unless necessary.

Note: high-skill statements are not equivalent to efficient programs. In fact, the key to program efficiency lies in algorithms.

Example: The following expression is difficult to understand.

* Stat_poi ++ = 1;

 

* ++ Stat_poi + = 1;

 

It should be changed as follows.

* Stat_poi + = 1;

Stat_poi ++; // the function of these two statements is equivalent to "* stat_poi ++ = 1 ;"

 

++ Stat_poi;

* Stat_poi + = 1; // The functions of these two statements are equivalent to "* ++ stat_poi + = 1 ;"


Related Article

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.