First of all, Dr. Lin Rui is strongly recommended to read this high-quality C ++/C Programming Guide carefully in an hour or two. You will benefit a lot. My personal harvest records are as follows.
The role of header files is described as follows:
(1) Use the header file to call the library function. In many cases, the source code is inconvenient (or inaccurate) to publish to users, as long as the header file and binary library are provided to users. You only need to follow the interface declaration in the header file to call the library function, without worrying about how the interface is implemented. The compiler extracts the corresponding code from the library.
(2) the header file can enhance the type security check. If an interface is implemented or used in a different way than the declaration in the header file, the compiler will point out an error. This simple rule can greatly reduce the burden on programmers to debug and correct errors.
In addition, extern can be placed before a variable or function to indicate that the definition of a variable or function is in another file, prompting the compiler to find its definition in other modules when it encounters this variable or function. If the function declaration contains the keyword extern, it only implies that the function may be defined in other source files and has no other function.
Empty row
A. Empty rows must be added after each class declaration and after each function definition.
B. In a function, there are no empty rows between closely related statements on the zookeeper. Separate empty rows in other places. (This article is worth noting)
Code line
A. Only one row of code is required, such as defining only one variable or writing only one statement. Such code is easy to read and easy to write comments.
B. if, for, while, do and other statements occupy one row, the execution statement cannot be followed by the statement. {} Must be added no matter how many statements are executed {}. This prevents writing errors.
other();
C. initialize the variable whenever possible while defining the variable (proximity principle)
Spaces in the code line
A. leave a space after the keyword, such as const, virtual, inline, case, if, for, and while. Do not leave a space after the function name, followed by the left brace '(', to be different from the keyword.
B. leave a space after the comma and semicolon; "=", "+ =" "> =", "<=", "+", "*", "%", "&", "| ", "<", space should be added before and after the binary operators such as "^"; no space is added before and after the unary operators.
C. There is no space before and after operators such as "[]", ".", and "->.
D. for long for and if statements with expressions, some spaces can be removed for the sake of conciseness.
(i=; i<; i++)
Long row splitting
The maximum length of a code line should be 70 to 80 characters. A long expression is split into new rows at the lower-priority operator, which is placed at the beginning of the new line (to highlight the operator ). The new lines to be split should be properly indented to make the layout neat and the statement readable.
((very_longer_variable1 >=&& (very_longer_variable3 <=&& (very_longer_variable5 <=
Note
A. Note is a "prompt" for the code, not a document. Comments in the program cannot be overwhelming, and too many comments will be dazzled. There are few annotations.
B. Write and annotate the code, modify the code, and modify the corresponding comments to ensure the consistency between the comments and the code.Annotations that are no longer useful must be deleted.
C. Avoid using abbreviations in comments, especially those that are not commonly used.
D. The location of the comment should be adjacent to the described code. It can be placed above or right of the code, not below.
F. When the code is relatively long, especially when multiple nesting occurs, you should add comments at the end of some paragraphs to facilitate reading.
Class Layout
There are two main types of layout:
(1) write private-type data in front and public-type functions in the back. Programmers who use this layout claim the design of the class "data-centric", focusing on the internal structure of the category.
(2) Write Functions of the public type in front and data of the private type in the back. The programmer who uses this layout claims that the design of the class is "behavior-centric", focusing on the interface (or service) that the class should provide ).
We recommend that you use the "behavior-centered" writing method, that is, first consider the functions that the class should provide.
Func1( Func2(
Common rules
A. identifiers should be intuitive and readable. The length of the identifiers should comply with the "min-length & max-information" principle.
B. The naming Rules should be consistent with the operating system or development tools used.
For example, the identifiers of Windows applications are usually in a case-insensitive manner, such as AddChild. The identifier of a Unix application is usually "lowercase and underlined", for example, add_child. Do not mix these two types of styles.
C. The name of a variable should be "noun" or "Adjective + noun"; the name of a global function should be "verb" or "Verb + noun" (verb phrase ).
D. Avoid numerical numbers in the name, such as Value1 and Value2, unless the number is required logically. This is to prevent programmers from being lazy and refuse to name their brains, resulting in meaningless names (because numbers are the most convenient ).
Simple naming rules for Windows Applications
A. The class name and function name are combined with words starting with an upper-case letter.
B. variables and parameters are combined with words starting with lowercase letters.
C. constants are all separated by uppercase letters and underscores.
D. Add the static variable prefix s _ (indicating static ).
E. If you have to require a global variable, add the global variable prefix g _ (indicating global ).
F. The data member of the class is prefixed with m_( indicating member). This prevents the data member from having the same name as the parameter of the member function.
G. To prevent conflicts between some identifiers in a software library and other software libraries, you can add prefixes that reflect the nature of software to various identifiers.
If statement
Do not use "=" or "! = "Is compared with any number.
((x >= -EPSINON) && (x <=
Sometimes we may see such an odd format as if (NULL = p. The program is not wrong. It is intended to put p and NULL upside down to prevent if (p = NULL) from being mistakenly written as if (p = NULL. The compiler considers if (p = NULL) as legal, but it indicates that if (NULL = p) is incorrect because NULL cannot be assigned a value.
Sometimes, if/else/return combinations are encountered in the program.
y;
Rewrite
Or rewrite it to a more concise
(condition ? x : y);
Efficiency of loop statements
In C ++/C loop statements, the for statement is frequently used, while statements are rarely used. The basic way to improve the efficiency of a circular body is to reduce the complexity of the circular body.
In multiple loopsThe longest loop is placed in the innermost layer, and the shortest loop is placed in the outermost layer.To reduce the number of times the CPU switches across the cycle layer.
Switch statement
Do not forget the last default branch. Even if the program really does not need to be processed by default, the statement default: break should be retained. This is not an extra step, but to prevent others from mistakenly thinking that you have forgotten the default processing.
Goto statement
We advocate less use and careful use of goto statements, rather than disabling them.
Comparison between const and # define
The C ++ language can use const to define constants, or use # define to define constants. However, the former has more advantages than the latter:
(1) const constants have data types, while macro constants do not. The compiler can perform type security checks on the former. However, only character replacement is performed for the latter, and there is no type security check. In addition, replacement may produce unexpected errors (marginal effect ).
(2) Some integrated debugging tools can debug const constants, but cannot debug macro constants.
Suggestion: Use Only const constants in the c ++ program instead of macro constants.
Constants in the class
Sometimes we want some constants to be valid only in the class. Since the macro constants defined by # define are global and cannot be achieved, it is assumed that we should use const to modify data members. The const data member does exist, but its meaning is not what we expected.The const data member is a constant only within the lifetime of an object, but it is variable for the entire class, because the class can create multiple objects, the values of the const data members of different objects can be different.The const data member cannot be initialized in the class declaration. The following usage is incorrect because the compiler does not know what the SIZE value is when the class object is not created.
SIZE = ; array[SIZE]; };
The const data member initialization can only be performed in the initialization table of the class constructor, for example
size); size) : SIZE(size) ); A b();
How can we create constants that are constant throughout the class? Don't count on the const data member. We should use the enumerated constants in the class. For example
{ SIZE1 = , SIZE2 = };
Enumerated constants do not occupy the storage space of objects. They are fully evaluated during compilation.. The disadvantage of an enumerated constant is that its implicit data type is an integer, its maximum value is limited, and it cannot represent a floating point number (such as PI = 3.14159 ).