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 ;"