High quality c ++ programming guide 3-4 chapter-thanks to Lin Rui

Source: Internet
Author: User
Tags case statement

Chapter 3 Naming rules

Naming rules should be naming rules for variables, functions, and classes. I have heard of the "Hungary" Naming rules. The main idea is to add a prefix to the variable and function name to improve people's understanding of the program. For example, all character variables are prefixed with 'ch. If a variable starts with PPCH, it indicates that it is a pointer to a character pointer. The biggest disadvantage of the "Hungary" method is that it is cumbersome, such as int
I, J, K; if you use the "Hungary" Naming rule, it should be int II, IJ, ik ;.

3. 1. Common rules

[Rule 3-1-1] identifiers should be intuitive and expected to be informed.

[Rules 3-1-2] naming Rules should be consistent with the operating system or development tools used. The identifiers of Windows applications are usually mixed in upper and lower cases, such as addchild. The identifier of a Unix application is usually "lowercase and underlined", for example, add_child.

Rules 3-1-3 do not contain local variables and global variables with identical identifiers.

[Rule 3-1-4] a variable name is used as a "noun" or "Adjective + noun ". The name of a global function must be a "verb" or "Verb + noun ". When only "verb" is used for member functions of the class, the omitted term is the object itself.

[3-1-1] avoid numbers in the name.

3. 2 simple windows application naming rules

Rule 3-2-1: The class name and function name are combined with words starting with uppercase characters.

Rule 3-2-2: a combination of variables and parameters starting with a lowercase letter.

[Rule 3-2-3] constants are separated by underscores (_) with uppercase letters.

[Rule 3-2-4] Static variables are prefixed with S _ (indicating static ).

[Rule 3-2-5] If you have to use a global variable, add the global variable with the prefix G _ (indicating global ).

Rules 3-2-6: Class Members are prefixed with m_( indicating member ).

[Rules 3-2-7] to prevent conflicts between some identifiers in a software library and other software libraries, you can add prefixes that reflect the nature of software for various identifiers. For example, all library functions of three-digit graphics standard library OpenGL start with GL, and all constants (or macro definitions) start with GL.

Chapter 4 expressions and basic statements

The second chapter of the operator format has already been discussed. Do you have rules in this chapter?

4. Priority of the 1 operator

The +,-, and * operators with a higher priority than the corresponding binary operators.

[Rule 4-1-1] If there are many operators in the code line, use parentheses to determine the Operation Sequence of the expression, so as to avoid using the default priority.

4, 2 compound expressions

Expressions such as a = B = C = 0 are called conformity expressions.

[Rule 4-2-1] Do not write too complex conformity expressions.

[Rule 4-2-2] do not have a multi-purpose conformity expression.

[Rule 4-2-1] distinguishes regular expressions from mathematical expressions.

4. 3if statement: This section uses "compare with zero value" as an example.

[Rule 4-3-1] indicates the flag of a Boolean value, which is a standard if statement compared with a zero value: If (FLAG)/If (! Flag)

[Rule 4-3-2] compare Integer Variables with zero values. If (value = 0)/If (value! = 0), if (value)/If (! Value). The value is a Boolean value.

[Rule 4-3] Do not use "=" or "for floating point variables "! = "Is compared with any number. Because float and double types have precision restrictions. The format should be converted to "> =" or "<=. Assume that the floating point variable is named X.
(X = 0.0) is converted to If (x> =-epsion) & (x <= epsion), where epsion is the allowable error (that is, accuracy ).

[Rule 4-4-4] the pointer variable is compared with the zero value. If (P = NULL)/If (P! = NULL ).

Additional instructions:

4. Efficiency of loop statements


The basic way to improve the efficiency of a circular body is to reduce the complexity of the circular body.

[4-4-1] in multiple cycles, try to put the longest cycle on the innermost layer and the shortest on the outermost layer to reduce the number of times the CPU switches to the cyclic layer.

[4-4-2] If logical judgment exists in the loop body and the number of cycles is large, it is recommended to move the logical judgment outside the loop body.

4. cyclic control variables of the 5for statement

[Rule 4-5-1] The loop variable cannot be modified in the for loop to prevent the for loop from being out of control.

[4-5-1] It is recommended that the value of the for statement's cyclic control variable be written in the form of "half-open and half-closed interval.

4. 6 switch statements

[Rule 4-6-1] The end of each case statement + break. Otherwise, multiple branches overlap.

[Rule 4-6-2] Do not forget the last default branch. Even if you do not need the default processing, you must retain the default statement.
: Break; in case someone mistakenly thinks that you forgot to handle the default statement.

4. 7goto statement: Use with caution.

 

Summary: This chapter mainly describes some rules for using expressions and basic statements and some layout suggestions.

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.