Chapter 10 general issues in using variables
General variables
10.1 data Literacy data cognition
10.2 making variable declarations easy
easy to understand variable definitions
· implicit declarations implicit declaration
1. some languages Support implicit declaration. For example, when VB uses an undeclared variable, the compiler automatically declares the change volume for you.
2. Implicit variable Declaration is one of the most dangerous features for any language.
3. Disable implicit Declaration, for example, option explicit in VB.
4. Declare all variables
5. follow certain naming rules
6. Check the variable name
10.3 guidelines for initializing Variables
Variable initialization principles
A series of problems caused by improper variable initialization are due to the default initial value of the variable, which is different from your expectation. There are the following problems:
1. No value is assigned to the variable. The value isProgramThe value of the memory region of the variable at startup.
2. The variable value has expired. A variable has been assigned a value somewhere, but the value is no longer valid.
3. Some of the variables are assigned values, while others are not.
The following are some suggestions to avoid initialization errors:
1. Initialization during declaration.
2. initialize the variable near the position used for the first time. Some languages such as VB do not support initialization while declaring.
3. Ideally, the location near the first time variables are used and the number of defined changes.
4. Use final or const whenever possible
5. Pay special attention to counters and Accumulators
6. initialize the data member of the class in the class constructor.
7. Check whether reinitialization is required
8. One-time initialization of the named variable; executableCodeTo initialize the variable.
9. Use compiler settings to automatically Initialize all variables
10. Use the warning information of the Compiler
11. Check the validity of the input parameters.
12. Use the memory access check tool to check the incorrect pointer
13. initialize the working memory at the beginning of the program
10.4 scope Scope
"Scope" can be seen as a way to measure the visibility of variables.
Scope or visibility refers to the visible and recommendable range of variables in the program.
· Localized references to variables
Make variable reference Localization
Code between multiple reference points of the same variable can be called "window of vulnerability )". There may be new code added to this window, improper
Or the person reading the code may forget to change the value of the variable. In general, the reference of a variable is localized, that is, the reference points are as concentrated as possible in
It is always a good way to work together.
One way to measure the closeness of different reference points of a variable is to calculate the "span" of the change volume ".
· Keep variables "live" for as short a time as possible
It is possible to shorten the variable "survival" Time
Survival time live time, that is, the total number of statements that are crossed during the existence of a variable.
If you use the concepts of span and survival time to check global variables, you will find that the span and survival time of global variables are long-this is also to avoid the use of global variables.
By one.
· General guildelines for minimizing Scope
General principle of scope reduction
1. initialize the variables used in the loop at the beginning of the loop, instead of initializing these variables at the beginning of the subroutine to which the loop belongs.
2. assign a value to the variable until it is about to be used.
3. Put related statements together
4. Extract related statements into separate subprograms
5. Use the strictest visibility at the beginning, and then expand the scope of the variable as needed.
· Comments on minimizing Scope
Description on narrowing down the variable scope
10.5 persistence continuity
10.6 binding time
10.7 relationship between data types and control structures
Relationship between data types and control structures
Relationship between three types of data and corresponding control structures
1. Translation of sequence data into sequential statements in a program
2. Select-type data is translated into if and case statements in the program.
3. Iterative data is translated into loop structures such as for, while, and repeat in the program.
10.8 using each variable for exactly one purpose
Specify a single purpose for a variable
1. Each variable is only used for a single purpose
2. Avoid giving the Code an implicit meaning.
3. Make sure that all declared variables are used.
Key Points
1. Data Initialization is prone to errors. Use the initialization method described in this chapter to avoid errors caused by unexpected initial values.
2. Minimize the scope of each variable. Combine the reference points of the same variable. Limits variables to the scope of subprograms or classes. Avoid using global data.
3. Combine statements with the same variables as much as possible.
4. Early binding reduces flexibility, but helps reduce complexity. Late binding can increase flexibility and complexity.
5. Use each variable for a unique purpose.