Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Getter and Setter Function Names

by Ovid (Cardinal)
on Apr 09, 2003 at 21:36 UTC ( [id://249438]=note: print w/replies, xml ) Need Help??


in reply to Getter and Setter Function Names

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)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://249438]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-26 03:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found