You have a temp that is assigned once with a simple expression, and the temp is getting in the way of other refactorings.
Replace all references to that temp with the expression.
(Fowler, p. 119)
Inline Temp is probably the most trivial refactoring pattern:
my $base_price = $self->an_order->base_price(); return ( $base_price > 1000 );
becomes:
return ( $self->an_order->base_price() > 1000 );
This causes occasional consternation in code reviews I attend, as some like to create temp variables to clarify their code and other find it to be unnecessary clutter. I find that programmers most often employ this technique when a verbose method name or deep data structure would otherwise exist multiple times in code:
my $thing = $that_object->that_method->{thing}; $obj->do_this_with( $thing ); $obj->do_that_with( $thing );
I generally avoid getting into scuff-ups about such matters. Fowler suggests this refactoring for situations where the temp gets in the way of other refactoring, generally Replace Temp With Query (up next).
Fowler also suggests an interesting technique to aid in the refactoring:
Declare the temp as final if it isn't already and compile. This checks that the tem is really only assigned to once. (Fowler, p. 119)
While Perl lacks the means to turn a once dynamic variable into a read-only (does anyone know of one?) there are a number of ways to create read only variables in perl. My favorite is Readonly. This is a debugging technique that I hadn't considered before.
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 3 - Inline Temp
by BrowserUk (Patriarch) on Jul 17, 2007 at 20:34 UTC | |
Re: Refactoring Perl 3 - Inline Temp
by ikegami (Patriarch) on Jul 17, 2007 at 20:18 UTC | |
Re: Refactoring Perl 3 - Inline Temp
by hossman (Prior) on Jul 17, 2007 at 20:15 UTC | |
by agianni (Hermit) on Jul 18, 2007 at 15:47 UTC | |
Re: Refactoring Perl 3 - Inline Temp
by rvosa (Curate) on Jul 21, 2007 at 16:23 UTC | |
by agianni (Hermit) on Jul 23, 2007 at 12:45 UTC |