McConnell_2004_CH10_General_Issues_in_Using_Variables Flashcards

1
Q

What is ‘implicit variable declaration’ and why is it a ‘hazardous’ feature?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Where should a varaible be initalized..?

A

Intialize each variables as they’re delcared

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is ‘variable scope’?

A

“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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the ‘variable span’?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is “variable live time”?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are two advantages of reducing variable span and live time?

A

See pages 245 to 247.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are four guidelines for minimizing variable scope?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the key difference between the “convenience” and “intellectual management “ philosophies of programming?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the main problem that aries with variable presistence?

A

“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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How does the use of functions reduce variable bind time…?

A

252 to 254

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How can a variable have a hidden meaning and why ios this bad….?

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly