This is a refactoring of a common anti-pattern: the inappropriately re-used temporary variable. In Perl, the refactoring looks like this:You have a temporary variable assigned to more than once, but is not a loop variable nor a collecting temporary variable.
Make a separate temporary variable for each assignment.
(Fowler, p. 128)
sub print_perimeter_and_area{ my $self = shift; my $temp = 2 * ( $self->{_height} + $self->{_width} ); print "$temp\n"; $temp = $self->{_height} * $self->{_width}; print "$temp\n"; }
becomes:
sub print_perimeter_and_area{ my $self = shift; my $perimeter = 2 * ( $self->{_height} + $self->{_width} ); print "$perimeter\n"; my $area = $self->{_height} * $self->{_width}; print "$area\n"; }
my $i; for $i ( 1..$some_max ){ #do something } # and later for $i ( 1..$some_other_max ){ #do something else }
but I don't generally find myself doing that anyway. True, loop variables are rarely going to get you in trouble, as they are usually initialized on each iteration, but it certainly doesn't make the code particularly readable. As such, I'd rather name my loop variables based on what they're actually doing. But I also like $verbose_variable_names, so YMMV.
The upshot is that this refactoring pattern is as much about making your code understandable as it is about avoiding errors that can occur when you reuse a temp variable. If you forget to reinitialize it before the second time you use it (or, say, accidentally delete the line that does), you're in trouble.
perl -e 'split//,q{john hurl, pest caretaker}and(map{print @_[$_]}(joi +n(q{},map{sprintf(qq{%010u},$_)}(2**2*307*4993,5*101*641*5261,7*59*79 +*36997,13*17*71*45131,3**2*67*89*167*181))=~/\d{2}/g));'
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Refactoring Perl #6 - Split Temporary Variable
by pKai (Priest) on Aug 16, 2007 at 18:52 UTC | |
by agianni (Hermit) on Aug 16, 2007 at 20:19 UTC |