3rd Chapter naming rules
1. Common rules:
Rule 1. Identifiers should be intuitive and can be spelt out, and it is expected that the text should be understood without "decoding".
Rule 2. The length of the identifier should conform to the "min-length&& max-information" principle.
Rule 3. The naming rules should be consistent with the style of the operating system or development tool being used.
Rule 4. Do not appear in the program similar identifiers that are only case sensitive.
For example:
int x, x; Variable x is easily confused with X
void foo (int x); function Foo is easily confused with Foo
void FOO (float x);
Rule 5: Do not have local variables and global variables with identical identifiers in the program, although the
The scope is different without a syntax error, but it can be misleading.
Rule 6. The name of the variable should use "noun" or "adjective + noun".
For example:
float value;
float OldValue;
float NewValue;
Rule 7: The name of the global function should use "verb" or "verb + noun" (moving the phrase).
The member function of a class should use only "verbs", and the omitted noun is the object itself.
For example:
Drawbox (); Global functions
Box->draw (); member functions of the class
Rule 8. Use the correct antonyms group to name variables with mutually exclusive meanings or functions that have opposite actions.
For example:
int minValue;
int maxValue;
int SetValue (...);
int GetValue (...);
Suggestion: Try to avoid the number of numbers appearing in the name, such as value1,value2, unless logically
Number is required.
2. Simple Windows Application Naming conventions
Rule 1: Class names and function names are combined with words that begin with uppercase letters.
Rule 2: Variables and arguments are combined with words that begin with a lowercase letter.
Rule 3. Constants are all capitalized letters, and the words are divided by underscores.
Rule 4: Static variable prefixed with S_ (for static).
Rule 5. If a global variable is required, prefix the global variable with g_ (representing global).
Rule 6: The data member of the class is prefixed with m_ (representing member), which avoids the data member and
The parameter of the member function has the same name.
Rule 7. To prevent conflicts in some of the identifiers and other repositories in a software library, you can
Various identifiers plus prefixes that reflect the nature of the software.
4th Chapter Expressions and BASIC statements
1. Precedence of Operators
Rule: If there are more operators in the line of code, use parentheses to determine the order of operations of the expression, and avoid
Use the default priority level.
2. Composite expressions
Rule 1. Do not write complex expressions that are too complicated.
Rule 2. Do not have multi-purpose composite expressions.
Rule 3. Do not confuse the compound expression in the program with the "true mathematical expression".
3. Loop control variables for the for statement
Rule 1: You cannot modify the loop variable inside the for loop to prevent the for loop from losing control.
Rule 2. It is recommended that the value of the loop control variable for the For statement be used as the "half open half closed interval" notation.
4. Efficiency of circular statements
Recommendation 1. In multiple loops, if possible, the longest cycle should be placed in the most inner layer, the shortest
The loop is placed at the outermost level to reduce the number of times the CPU crosses the loop layer.
Recommendation 2. If there is a logical judgment in the loop and the number of cycles is large, it is advisable to move the logical judgment out of the loop body.
5. Switch statement
Rule 1. Do not forget to add break at the end of each case statement, or it will cause multiple branches to overlap
(unless you intentionally overlap multiple branches).
Rule 2. Don't forget the last default branch. Even if the program really does not need default processing,
The statement default:break should also be preserved; This is not superfluous, but to prevent others from mistakenly
Forgot the default deal for you.
5th Chapter Constants
1. Why constants are required
Rule: Try to use the meaning of the intuitive represented to represent those numbers that will appear multiple times in the program or
String.
2 Comparison of Const and #define
Rule: Only const constants are used in C + + programs without using macro constants, that is, const constants End
Replace all macro constants.
3. Constant definition Rules
Rule 1: Constants that need to be exposed are placed in the header file, and the constants that do not need to be exposed are defined
The head of the file. For ease of management, the constants of different modules can be centrally stored in a common header file.
Rule 2. If a constant is closely related to other constants, the relationship should be included in the definition without
Some orphaned values should be given.
6th Chapter Function Design
1. Rules for parameters
Rule 1: Complete the writing of the parameters and omit the parameter names by simply writing the type of the parameter.
If the function has no arguments, it is filled with void.
Rule 2. Parameter naming should be appropriate, order should be reasonable
Rule 3. If the parameter is a pointer and is only used for input, you should add a const before the type to prevent the
The pointer was accidentally modified in the function body.
Rule 4: If the input parameter passes the object as a value, use the "Const &" method instead
To eliminate the construction and destruction of temporary objects, thus improving efficiency.
Recommendation 1: Avoid the function has too many parameters, the number of parameters as far as possible to control within 5. If the parameter is too
, it is easy to mistake the parameter type or order when using.
Recommendation 2. Try not to use parameters of type and number uncertainties.
2. Rules for returning values
Rule 1. Do not omit the type of the return value.
Rule 2. The function name and return value type are semantically non-conflicting.
Rule 3. Do not mix the normal values and the error flags back together. The normal value is obtained by the output parameter, and
The error flag is returned with a return statement.
Recommendation 1. Sometimes the function does not originally need to return a value, but in order to increase flexibility such as support chain expression,
The return value can be appended.
Recommendation 2. If the return value of a function is an object, some occasions use the "reference pass" substitution value to pass
"can improve efficiency. Some situations can only be passed with "value" and not "referenced", otherwise an error occurs.
3. Using assertions
Recommendation 1. Use assertions to catch illegal situations that should not occur. Do not confuse illegal situations with error conditions
The difference between the latter is inevitable and must be dealt with.
Recommendation 2. At the entrance of the function, use assertions to check the validity of the parameters (legality).
7th Chapter Memory Management
1. Overloading with overrides, the member function is overloaded with features:
(1) The same range (in the same class);
(2) The function has the same name;
(3) different parameters;
(4) The virtual keyword is optional.
2. Overrides refer to a derived class function overriding a base class function, characterized by:
(1) different ranges (in the derived and base classes, respectively);
(2) The function has the same name;
(3) the same parameters;
(4) The base class function must have the virtual keyword.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"High-quality Programming Guide C + +" Lin Rui Han Yongquan compiled summary 2