Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2024-04-24 23:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found