Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re: Why get() and set() accessor methods are evil

by bronto (Priest)
on Nov 28, 2003 at 19:59 UTC ( #310780=note: print w/replies, xml ) Need Help??

in reply to Why get() and set() accessor methods are evil

Hello synistar

Months ago I wrote a meditation on the involuntary encapsulation violation; even if I had three nice answers, I still think that that kind of implicit ability of modifying the internals of an object without using accessors is a bad thing. What I think is wrong in an accessor like get_list is that you are passing back to the caller a reference to an internal structure of the object. Do something like this:

my $x = $obj->get_list ; $x->[3] = 'surprise!' ; voilà!: you changed the object without using set_list. I agree that the example above is really-bad-code, but there is a lot of it around...

Strictly speaking about your meditation, I like best accessors that have the ability to get/set their values, or read-only ones, or write-only ones. After reading Advanced Perl Programming I started using get/set methods, but after a short time I found them really annoying...


The very nature of Perl to be like natural language--inconsistant and full of dwim and special cases--makes it impossible to know it all without simply memorizing the documentation (which is not complete or totally correct anyway).
--John M. Dlugosz

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (3)
As of 2022-08-18 07:04 GMT
Find Nodes?
    Voting Booth?

    No recent polls found