Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^2: Class::Accessor and Damian's PBP

by grantm (Parson)
on Feb 23, 2006 at 00:49 UTC ( #532126=note: print w/replies, xml ) Need Help??


in reply to Re: Class::Accessor and Damian's PBP
in thread Class::Accessor and Damian's PBP

If you have so many accessible properties that you need to generate accessors for them, you may need to re-think your design. Accessors are a code smell.

I'm not sure that I entirely agree with that assertion.

Most OO classes will have a number of properties, but unfortunately Perl's object model does not provide a standard way to define those properties. The simple and common approach is to store properties as keys in a hash but this leads to hard-to-find bugs resulting from typos in hash keys. Generating accessors is an effective way to eliminate that class of error.

I do agree that lots of accessors in the public API of a class is often a problem with the design.

The examples on the page you linked to seem to be written in a language that, unlike Perl, supports both properties and private methods.

Replies are listed 'Best First'.
Re^3: Class::Accessor and Damian's PBP
by acid06 (Friar) on Feb 23, 2006 at 15:15 UTC
    The simple and common approach is to store properties as keys in a hash but this leads to hard-to-find bugs resulting from typos in hash keys. Generating accessors is an effective way to eliminate that class of error.
    Have you already tried to use fields;?
    Not that I dislike accessors, but I think this is not the exact reason why they're good.


    acid06
    perl -e "print pack('h*', 16369646), scalar reverse $="
Re^3: Class::Accessor and Damian's PBP
by Roy Johnson (Monsignor) on Feb 23, 2006 at 17:57 UTC
    The reason accessors are a code smell is that OO programming is supposed to be about objects performing tasks that they know how to do, not programmers fiddling with the objects' internal states. The internal states should primarily change as a result of interacting with their universe. If that's not the case, then maybe OO is the wrong approach.

    That is not to say that there should be no public accessors in good OO code. Code smell is not an absolute condemnation, it's just a marker that should make you think twice.


    Caution: Contents may have been coded under pressure.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (11)
As of 2019-10-14 13:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?