Huawei software programming specification Learning (IV)-readability
4-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:Avoid misunderstanding when reading the program, and prevent program errors because the default priority is inconsistent 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 it is written as follows:
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.
4-2: Avoid using numbers that are hard to understand and use meaningful identifiers instead. A number must be used instead of a meaningful enumeration or macro, which involves a physical state or a constant with a physical meaning.
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 1if(Trunk[index].trunk_state == TRUNK_IDLE){ Trunk[index].trunk_state = TRUNK_BUSY; ... // program code}
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 of the rectangle is closely related to the width. Char_poi = STR;
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 to the following:
* Stat_poi + = 1; stat_poi ++; // This statement is equivalent to "* stat_poi ++ = 1;" ++ stat_poi; * stat_poi + = 1; // The function of these two statements is equivalent to "* ++ stat_poi + = 1 ;"