I don't like overloaded functions that allow you to both get and set a property. For example:

my $thing = Thing->new($id); print $thing->name; $thing->name($new_name); print $thing->id; $thing->id($new_id);

If that's an object that represents a row in the database, there's a good chance that you can't set the id to a new value. However, the interface doesn't make that clear. If you have multiple "read only" methods, overloading the methods names leads to overloading the brain. Bad Stuff.

Another reason for not overloading the methods lies in bugs: if you overload the methods, you have to explicitly write code -- potentially buggy -- to prevent someone from changing read-only values. If you keep accessors and mutators separate, you simply omit mutators for read only attributes.

After a much experimentation, I find that I prefer the following:

print $thing->name; $thing->set_name($new_name);

Aside from the fact that it's less typing, I use Template Toolkit quite heavily and it's easier for our designers to see what's going on.

[% FOREACH customer = customers; customer.last_name; ", "; customer.first_name, "\n"; END; %]

To me, that just seems much cleaner.

Cheers,
Ovid

New address of my CGI Course.
Silence is Evil (feel free to copy and distribute widely - note copyright text)


In reply to Re: Getter and Setter Function Names by Ovid
in thread Getter and Setter Function Names by Wally Hartshorn

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.