Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
Problems? Is your data what you think it is?
 
PerlMonks  

Comment on

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

Answer quickly! Does the following set the name or not?

$object->name(@name);

Well, its not clear to me that this behaviour is a bad thing at all. As written its conceptually similar to

$object->name(@name) if @name;

You could keep the dual purpose behaviour but make it more sensitive to this type of error quite simply:

use Carp; sub name { my $self = shift; if (@_) { $self->{name} = shift; return $self; } elsif (!defined wantarray) { croak "Error:", (caller(0))[3], " called in void context with no arguments"; } else { return $self->{name}; } }

Your point about using id() as a setter when it isnt meant to be used that way doesnt do much for me either. If the id() sub isnt meant to set the value then it shouldn't and it should throw an error when its asked to. Which puts the dual-use solution in the same position as set_id() would be. (IE it would be a run time error, one caught by the code, one caught by perl).

Also the two method approach has a problem that the dual use approach doesnt that you havent mentioned at all: in the two method approach you have two code stubs whose responsibility it is to operate on the same data. This means that if you change the field name or the objects construction you have to change two subroutines. With the dual use approach all of the logic to manipulate a given atttribute of an object is encapsulated in one sub. That means that things like validation rules are right next to the fetch logic, which means that when you are reviewing code you can see all of the "rules" that pertain to a given attribute in one place. You don't have to remember to review both. You don't have to independently review the setters validation logic in order to determine what the fetcher will return.

Personally i find the encapsulation offered by the dual-use approach combined with careful construction to be better practice than separate-subs.

Updated: to add the missing defined, as noted by radiantmatrix.

---
$world=~s/war/peace/g


In reply to Re: Things I Don't Use in Perl by demerphq
in thread Things I Don't Use in Perl by Ovid

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 perusing the Monastery: (5)
    As of 2014-04-18 07:07 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      April first is:







      Results (462 votes), past polls