<#6 - Split Temporary Variable Series Index #8 - Replace Method with Method Object>

The code assigns to a parameter.

Use a temporary variable instead.

(Fowler, p. 131)

Initially this may strike you as a simple refactoring pattern and the mechanics of it are. Here's how it looks in Perl:

sub discount{ my $arg_ref = shift; $arg_ref->{input_val} -= $arg_ref->{input_val} > 50 ? 2 : 0; $arg_ref->{input_val} -= $arg_ref->{quantity} > 100 ? 1 : 0; $arg_ref->{input_val} -= $arg_ref->{year_to_date} > 10000 ? 4 : 0; return $arg_ref->{input_val}; }

becomes:

sub discount{ my $arg_ref = shift; my $input_val = $arg_ref->{input_val}; $input_val -= $arg_ref->{input_val} > 50 ? 2 : 0; $input_val -= $arg_ref->{quantity} > 100 ? 1 : 0; $input_val -= $arg_ref->{year_to_date} > 10000 ? 4 : 0; return $input_val; }

Get the code

Fowler's primary reason for promoting this pattern is to address the "lack of clarity and to [sic] confusion between pass by value and pass by reference". (p. 131)

Note: my original writeup contained a number of factual errors, which I believe I have corrected.

This is important in Java for reasons that Fowler explains, but I don't care to pretend I know or understand Java enough to elaborate. In Perl everything is passed by reference. Updating a parameter passed as a reference -- i.e. modifying $_[$x] or a copy of a reference contained therein -- could have unexpected consequences and is best avoided. Thanks to ikegami for pointing out that Perl is always pass by reference, contrary to my original suggestion to the contrary.

The exception would be in the situation where a subroutine would otherwise modify and return a very large data structure or object or where you will call a subroutine many, many times to modify and return any data passed to it. In these situations, it may be appropriate to pass in a reference, modify it within the subroutine and not return it. This is not a pre-optimization that I would recommend, as I think it makes code less maintainable, but it is an optimization to be considered if you find your code to be slow. I should point out that this is much less likely to be a problem in Perl than in other programming languages, but there are situations where it is an appropriate technique.

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 #7 - Remove Assignments to Parameters 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.