C Programming Language Specification (b)

5. identifier naming rules

5-1 regular named identifiers to clear, clear, clear meaning, and use complete words or abbreviations we basically can be understood, to avoid misleading.

Description: shorter word may be formed by removing the abbreviation "vowel"; preferably longer words forming the first few letters of the word.
Abbreviations; there are some words we recognized abbreviations.
Example: The following abbreviations of words that can be recognized by everyone basically.
temp abbreviated as tmp;
In Flag abbreviated as FLG;
statistic may be abbreviated as STAT;
INCREMENT abbreviated as inc is;
Message may be abbreviated MSG;

Rules 5-2 custom data type aptly named, making it self-describing, to improve readability. Note that in the same product naming unity, such as: int32_t, int64_t, int8_t, uint64_t , uint32_t, uint8_t, char.
Description: With a custom type, you can make small type programming language provides insufficient information shortcomings, and make the procedures clear and concise.
Example: Reference manner custom data type declaration.
unsigned char uint8_t typedef;
typedef unsigned Short uint16_t;
typedef unsigned int uint32_t;

Rule 5-3 using typedef defined structure, and needs _t after its name; using typedef enum defined, and needs _e after its name.

Rule 5-4 naming conventions must be maintained with the style used by the system is consistent and unified in the same project, such as the use of all lowercase underlined UNIX style or capitalization shuffling way, do not use capitalization and underscores mixed up manner, such as special identification or identifier of the member variables and global variables m_ G_, thereafter adding the case is to allow the shuffling.
Example: Add_User not allowed, add_user, AddUser, m_AddUser allowed.

Alone do not appear similar identifier case sensitivity rules 5-5 program.

Rules 5-6 do not appear in the program identifier identical local and global variables, although the difference between the two scopes without grammatical errors, but misleading. Global variables required to add g_ prefix.

Rule 5-7 variable name should be used "noun" or "adjective + noun."
For example: a float value;
a float oldValue;

Rule 5-8 global function name should be used "verb" or "verb + noun" (movable object phrases). Class member function should be used only "verb", the noun is omitted is the object itself.
For example: DrawBox (); // global function

6. Basic statements and expressions

Do not place undue reliance on Rule 6-1 C expressions of operator precedence rules.
In addition to the use of parentheses operator can override the default priority it may also be used to emphasize the operators used. C using a rather complex operator precedence rules can easily lead to error, this method can help avoid such errors, and can make the code legible. However, too much parentheses distract it reduces the readability of the code.
The following approach is recommended when given in parentheses:
1, the right hand operand assignment operator does not require the use of parentheses, unless the right-hand end of the assignment expression itself comprising:

x = a + b; /* acceptable */
x = (a + b); /* ( ) not required */

2, the operands of unary operators without using brackets:

x = a * -1; /* acceptable */
x = a * (-1); /* ( ) not required */

3, otherwise, binary and ternary operator operands should be cast-expressions, unless expression is the same for all operators.

x = a + b + c; /* acceptable, but care needed */
x = f ( a + b, c ); /* no ( ) required for a + b */
if (a && b && c) /* acceptable */
x = (a + b) - (c + d);/* acceptable */
x = (a * 3) + c + d; /* acceptable */
x = (uint16_t) a + b; /* no need for ( ( uint16_t ) a ) */

4, even if all the operators are the same, may be used to control operation sequence in parentheses. Some operators (e.g., addition and multiplication) in the associative algebra, but not necessarily so in C. Similarly, a hybrid type involving integer operations (number of rules allowed) since the presence of integral promotion may produce different results. The following example is written in 16-bit implementation, which describes the structure and a clear expression of the importance of the addition is not binding:

uint16_t a = 10;
uint16_t b = 65535;
uint32_t c = 0;
uint32_t d;
d = (a + b) + c; /* d is 9; a + b wraps modulo 65536 */
d = a + (b + c); /* d is 65545 */

Rule 6-2 variable is incremented and decrement operators need a separate line.

The following terms tell our dependence on the order of operations is how it happened, and thus help us to adopt the rule.
1, increment or decrement operators
as can produce erroneous example, consider
x = b [i] + i ++;
The b [i] is the first operation in the operation or after i ++ expression generated different results. The value-added operations as a separate statement, to avoid this problem. Then:
X = B [I] + I;
I ++;
2, function parameters
calculation order function parameter is not specified.
x = func (i ++, i );
The order of the two different operational parameters of the function, the expression will give different results.

Rule 6-3 or logical operators && | | not contain the number of right-handed side.
Among C there is a case, some part of the expression is not to be calculated. If these sub-expressions have side effects, the side effects may occur or may not occur, depending on the value of the other sub-expressions.
E.g:

if ( ishigh && ( x == i++ ) ) /* Not compliant */

Rule 6-4 is not available in an if statement the Boolean variables were compared directly with TRUE, FALSE, or 1,0.
Assume Boolean variable named flag, comparing it with the standard zero value if statement is as follows:
if (flag) // express flag is true
if (flag!) // express flag is false

Rule 6-5 in an if statement should use the integer variable "==" or "! =" Directly compared with 0.
Boolean variables can not be imitated style and write:
IF (value) // is misleading value is a Boolean variable
if (value!)

Rule 6-6 if the statement is not floating-point variables with "double =" or "! =" In comparison with any number. Be sure to pay attention, whether it is a variable of type float or double, have accuracy limitations. So be sure to avoid floating-point variables "==" or "! =" Is compared with the digital, should try to be converted into "> =", or "<=" form.

Rule 6-7 in an if statement should be a pointer variable with "==" or "! =" Compared to NULL.

Rule 6-8 if statement to try to add else branch of statement has no else branch should be treated with care.

Rule 6-9 comparative statement recommended constant value for the left! ! !
Example: For example, if (5 == i), instead of i == 5, written == = error prevention.

Rule 6-10 in a multiplex cycle, if possible, should be placed in the innermost loop longest, shortest cycles on the outermost layer, to reduce the number of CPU cycles cross-cut layer.
Recommended Style:

for (col=0; col<5; col++ )
{
for (row=0; row<100; row++)
{
sum = sum + a[row][col];
}
}
不推荐风格:
for (row=0; row<100; row++)
{
for (col=0; col<5; col++ )
{
sum = sum + a[row][col];
}
}

6-11 rule should be limited to nesting depth of multiple cycles, it is recommended within 5 layers.

Rule 6-12 each end of the case statement do not forget to add break, otherwise it will lead to multiple branches overlap. If a plurality of branch overlap intentionally, may also need to increase the explanatory notes.

Do not forget that the final rule 6-13 default branch. Even if the program really does not need default processing, it should retain the statement default: break ;.

Rule 6-14 goto statement frequent errors or problems. It is possible to skip the construction of some objects, initialization, an important calculation statement variable, and will destroy the structural design. These rules advocatedLess, caution goto statement

6-15 branch code rule using braces {} are enclosed, including the case of only one line of code! !

Published 56 original articles · won praise 75 · Views 300,000 +

Guess you like

Origin blog.csdn.net/qq_44710568/article/details/105264289