Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

A method's body is just as clear as its name.

Put the method's body into the body of its callers and remove the method.

(Fowler, p. 117)

Fowler's second refactoring pattern is remarkably simple, but it is a classic example of a refactoring that breaks tests for me. It's not generally a big problem but sometimes it makes me wonder if I'm doing something wrong.

Fowler's example in Perl looks like this:

sub get_rating{ my $self = shift; return $self->more_than_five_late_deliveries() ? 2 : 1; } sub more_than_five_late_deliveries{ my $self = shift; return $self->{_number_of_late_deliveries} > 5; }

becomes:

sub get_rating{ my $self = shift; return ( $self->{_number_of_late_deliveries} > 5 ) ? 2 : 1; }

Get the code

The concept is simple, and Fowler puts it well in the summary. But there are two issues that are introduced with what appears to be a simple refactoring pattern. The first and most obvious issue is that this refactoring breaks my tests (see inline_method_refactored.t in the example code for the fixed test). This isn't a big deal exactly, although with larger refactorings of this type it takes some effort to rewrite the tests. The upshot is that, ideally, you really need to rewrite your tests before you perform the refactoring so that you aren't testing the methods that are going away. At the same time, you need to make sure that anything you may have overridden for the sake of isolating tests to specific methods.

The second issue is more of a philosophical question about when and how to refactor. For example, while the get_rating method is small, it is useful to have it extracted because it includes some specific logic. Were I to need this in more than one place, I wouldn't have to worry about whether I repeated the code correctly in more than one place. Even in the case where I am only going to call this method once, it is useful to have it extracted.

I might actually consider refactoring this differently given the right context. From a business logic perspective, it appears that $self->{_number_of_late_deliveries} has some business significance, so I might simply change change the name of the original more_than_five_late_deliveries method to too_many_late_deliveries. Alternately, I might refactor it as Fowler does, but extract the value of the number 5 to a config-ish method called something like late_delivery_threshold.

All of this is simply to point out that refactoring isn't generally as simple as having a technical understanding of a programming language and having a developers understanding of how to make code more maintainable. In a lot of cases, having an understanding of the business domain can be helpful in organizing code effectively.

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 #2 - Inline Method 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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chanting in the Monastery: (7)
    As of 2014-08-27 09:37 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (235 votes), past polls