Документация
HTML CSS PHP PERL другое

6.3 Escaping the Global Variable Trap

 
Previous Table of Contents Next

6.3 Escaping the Global Variable Trap

Far too many programs that I read start with dozens of variable declarations:


my @e1badges;

my @e2badges;

my $optemp;

my $found_match = 0;

my $total1;

my $total2;

my %linecount;

my $lines_per_page;

# etc ad nauseam for at least a dozen more lines

And that's just the ones that are savvy enough to use my. This type of code is hard to maintain because of how much there is to keep track of. Where is $optemp used? What parts of the code are allowed to use it? Who can modify it? You must go on a fishing expedition to find these things out, wearing out the "find" function of your editor. Sometimes a variable is no longer used elsewhere at all.

Some students of formal life-cycle methodologies decide that comments are the cure for this problem, and the more the better. Their version of the preceding starts with:


##########################################################

# Global variable: @e1badges                             #

# Type: Array                                            #

# Purpose: First set of badge numbers read from file     #

# Index range: 0 through last badge number               #

# Sentinel values: none                                  #

# Exception indications: none                            #

##########################################################

my @e1badges;

This is as much of a pain to read as it is to type. Only someone getting paid by the line could love this code. In the process of adding only minimal information, its obesity squeezes more valuable code out of view. Remember, the real estate on the maintenance programmer's screen is precious; every part of it should count as much as possible, and that means comments have to carry their weight.

The first way we can improve this situation is to note that Perl is unlike many procedural languages that require all the variables that are going to be used within a block to be declared before any executable code in that block. You can put declarations anywhere you want in Perl, as long as they precede all other references to their variables. So declare a variable as close as you can get to its first use; if you ever remove the use of the variable you're more likely to see the superfluous declaration and remove it, too. An exception to this guideline is a constant assignment intended to be configurable. For the sake of convenience, these are usually clustered near the top of the code so a casual maintenance programmer isn't tempted to go further into code you'd rather they didn't try to modify.

Next, see if you can reduce the scope of the variables by putting them inside blocks. Look for all the uses of each variable in turn; if they're bunched in a relatively small area, that suggests that you may be able to turn that area into a subroutine. Then, identify purely temporary variables. If a variable is used only over a few lines of code and isn't an input or output then it is probably a synthetic variable. Either eliminate it using the guidelines in Section 4.8.2, or isolate it with a naked block around the area of its use.

    Previous Table of Contents Next
    © 2000- NIV