<#2 - Inline Method Series Index #4 - Replace Temp with Query>

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 );

Get the code

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));'

In reply to Refactoring Perl #3 - Inline Temp by agianni

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.