McConnell_2004_CH10_General_Issues_in_Using_Variables Flashcards
What is ‘implicit variable declaration’ and why is it a ‘hazardous’ feature?
Implicit Delclaration: Some langauges have implicit variable declaratoins. For example, if you use a variable in Microsoft Visual Basic without declaring it, the complier declares it for you automatically (depending on your compiler settings).
Implicit declaration is one of the most hazardous features available in any language. If you program in Visual Basic, you know how frustrating it is to try to figure out why acctNo doesn’t have the right value and then notice that accNum is the right variable that’s reinitialized to 0. This kind of mistake is an easy one to make if your language doesn’t require you to declare variables.
Where should a varaible be initalized..?
Intialize each variables as they’re delcared
What is ‘variable scope’?
“Scope” is a way of thikning about a variable’s celebrity status: how famous is it? Scope, or visability, refers to the extent to which your variables are known and can be referened throughout a program.
What is the ‘variable span’?
One method of measuring how close together the references to a variable are to compute the “span” of a variable. Here’s an example:
Java Example of Variable Span
a = 0;
b = 0;
c = 0;
a = b + c;
Java Example of Spans of One and Zero
a = 0;
b = 0;
c = 0;
b = a + 1;
b = b / c;
In this case, there is one line between the first reference to be and the second, for a span of one. There are no lines between the second reference to b and the third, for a span of zero.
The avarage span is computed by averaging the individual spans. In the second example, for b, (1+0)/2 equals an average span of 0.5.
What is “variable live time”?
A concept that’s related to variable to variable span is variable “live time,” the total number of statments over which a variable is live. A variable’s life begins at the first statement in which it’s referenced, its life ends at the last statement in which it’s referenced.
What are two advantages of reducing variable span and live time?
See pages 245 to 247.
What are four guidelines for minimizing variable scope?
Initialize variables used in a loop immediately before the loop rather than back at the beginning of the routine containing the loop...
Don’t assign a value to a variable until just before the value is used.
C++ Example of Good Varaible Declarations and Initializatoins.
Group related statements
C++ Example of Using Two Sets of Variables in a Confusing Way.
What is the key difference between the “convenience” and “intellectual management “ philosophies of programming?
The difference between the “convenice” philosophy and the “intellectual manageability” philosophy boils down to a difference in emphasis between writing programs and reading them. Maximizing scope might indeed make programs easy to write, but a program in which any routine can use any variable at any time is harder to understand than a program that uses well
What is the main problem that aries with variable presistence?
“Persistence” is another word for the life span of a peice of data. Persistence takes several forms. Some variables persis.
The main problem with persistence arises when you assume that a variable has a longer presistence than it really does.
How does the use of functions reduce variable bind time…?
252 to 254
How can a variable have a hidden meaning and why ios this bad….?
- The value in the variable * pageCount* might represent the number of pags printed, unless it equals -1, in which case it indicats that an error has occurred.
- The variable customerId might represent a customer number, unless its value is greater than 500,000 in which case you subtract 500,000 to get the number of a delinquent account.
- The variable bytesWritten might be the number of bytes written to an output file, unless its value is negative, in which case it indicates the number of the disk drive used for the output.