Chapter 7 portability Defects
Although I have read chapter 7, I really don't know how to describe it. The headquarters may display the content in the book
Repeat it ~ And portability is a common thing for us, but for me
You have not considered this
So I decided not to write it. Reading this chapter is better. For example
Mark Horton's How to Write portable software in C.
Chapter 8 Suggestions
1.Do not persuade yourself to believe in "The Emperor's New Clothes ".
Some errors are extremely deceptive and pseudo
2.Straightforward indication of intent.
When the meaning you want to express may be ambiguous, use parentheses or other methods to make your
The intention is clear.
3.Evaluate the knowledge of the simplest case.
This is applicable in almost any situation and can be inspired by the simplest example.
4.Use asymmetric boundaries.
This is 3.6 of the content and is worth reading again.
5.Pay attention to potential bugs.
Adhere to the well-known part of the C language to avoid the "Uncommon" language features. Especially when Porting Programs.
Remember, the life cycle of a program is often longer than that of the machine on which the program runs.
6.Defensive Programming.
Do not make too many assumptions about program users and compiler implementations.
This is Chapter 8.
Below are some examples of CSome language suggestions:
I. Program Layout
Blank line: determines whether to add blank lines based on the degree of relevance between codes.
Code line: the code line only does one thing
Spaces in the code line: it is easy to distinguish between independent variables or constants.
Alignment: Easy code hierarchy
Long line splitting: easy to understand the meaning of the Code
Note: Necessary
Naming rules: Hungarian naming rules, etc. It is best to keep the naming rules uniform in a program.
Ii. Efficiency of loop statements
In multiple cycles, the longest number of cycles should be placed in Zeng
Try to move the judgment out of the loop
3. the pointer can only be compared with null and cannot be compared with 0.
4. If the floating point number is not 0, it cannot be compared with 0 or be compared directly.
5. Switch statement
Add break after case
Add defult to the end
6. Const constants have data types. It is best not to use the define macro to define them.
VII. Function Design
Function interface element parameters and return values. In C language, function parameters and return values are transmitted in two ways: value transfer and pointer
Parameter rules
Parameters must be completely written, named properly, and known (type and meaning)
If the parameter is a pointer and only used for input, you should add const before the type to prevent the pointer from being modified outside the function body.
Avoid function having too many parameters and limit the number to 5
Do not use parameters with undefined types or quantities, that is, check the validity of parameters at the function entry (important)
Return Value rules
Do not omit the type of the returned value
The function name and return value type cannot conflict with each other in the statement.
Differentiate between normal and error responses
Return cannot return a pointer to the stack memory because the stack has been released.
Check whether the returned value is a variable.
Function functions should be single and small, preferably within 50 lines
Avoid static variables in the function whenever possible
Check not only the legitimacy of input variables, but also global variables, file pointers, and other variables entering the function body through other channels.
Clear the meaning of error return code
8. Remember to release free () after applying for memory in malloc ()
PS: article 6 does not work in C language. Only in C ++ can const define constants. I personally think that in C Language
When you need to define numeric constants, you may use enumeration instead (do not forget the semicolon following the parentheses of enumeration ).