Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

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

by scrottie (Scribe)
on Nov 27, 2003 at 07:25 UTC ( #310487=note: print w/replies, xml ) Need Help??


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

Hi,

I wrote-up something along these lines a while ago for perldesignpatterns.com. Other things have been written in the past too. Off the top of my head, the major gripes people have with put-get accessors (not lvalue or tied interface) are:
  • -> becomes the only useful operator. + becomes foo->add() or foo.add().
  • Side effect of above: incrementing or any += style operator becomes particularly painful
  • The rich interfaces of hashes and arrays is lost when implementing datastructures as objects, an extremely common case
Like functional programming, object oriented programming is more a state of mind than a set of tools. There are powerful motivators for people to drop the features of Perl for a far more simplistic language that offers a clean, consistent object library and strict checking. These kinds of projects are seldom done in Perl - projects with numerous programmers. Attempting to do these large projects without the natural inter-programmer boundaries that interfaces afford is every bit as painful as losing hashes, automatic stringification, and so on. Well, it need not be such a choice: operator overloading, tied interfaces, and lvalue methods collectively allow you to present a portion of an OO interface as an array, hash, or so on. An early on accepted RFC for Perl 6 (perhaps this has changed since then) asks that hashes iteractors be reset explicitly with a sort of %hash.reset() type thing. Things like exists() would be made into methods as well rather than polluting the core namespace. Hashes have a rich interface, but as rich it is, it will never be rich enough. Your object will present some of its interface masquerading as core Perl features, but after a point, you must commit to an API, and you should do this carefully, thoughtfully, and knowledgeably. This is a topic unto itself. Designing anything that pases the test of time boils down to becoming a history major, studying many falls of many civilizations. In other words, just because you can avoid OO interfaces for a while doesn't mean you should forever, or that you need be any less skillful with them just because you're writing Perl instead of Java.

-scott
  • Comment on Re: Why get() and set() accessor methods are evil

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2021-01-23 09:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?