in reply to Re^8: Perl OO and accessors
in thread Perl OO and accessors
lots of needless complexity for little or no real gain.
First off, this is only needlessly difficult in Perl. Python, Ruby and many others support this kind of smart attribute out of the box.
As for the gain, it's in programmer time, readability, simplicity, conciseness and uniformity.
An attribute is essentially a variable with possibly some runtime enforced restrictions so it should be accessible just like any other variable. I can do
so why should I have to do$radius *= 2; $h{radius} *= 2; $radii[5] *= 2;
why not$c->setRadius($c->getRadius * 2);
?$c->Radius *= 2;
The existence of something that fundamentally is a variable but that must be accessed in an unnatural way is actually a source of extra complexity. Accessors and mutators are something that are only necessary when the language designer didn't bother include smart attributes.
A variable hidden behind methods causes more code to be written by anyone trying to use that variable and it actually prevents code reuse. For example any function which takes a reference to a variable and alters it can not be resued if the interface for the variable is via an accessor method. Whereas an lvalue attribute will work just fine anywhere a normal variable would have worked.
Occam's razor says we should reduce the number of concepts we have to deal with, lvalue accessors mean we can throw away the awkward not-quite-a-variable attributes. Lispers realised this long ago and introduced generalised variables which give a uniform interface to "cars and cdrs of lists, elements of arrays, properties of symbols, and many other locations are also places where Lisp values are stored".
Why would I want to leave bits of a redundant API around with the caveat that, if performance is an issue, they shouldn't be used?
I think this is backwards. The Area method is the primary and recommended interface. setArea and getArea are the redundant parts of the interface (although they are an essential part of the implementation) and are available in case performance is an issue (they might also be useful if other code is expecting a setter method rather than an lvalue).
So essentially, while the implementation may be complex, it is once-off complexity, hidden even from the author of the class and results in an overall reduction of complexity and increase in productivity. If it leads to unacceptable performance issues you can choose to use to the use the more complex interface in performance sensitive places in which case you still better off than when you were using set/get everywhere.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^10: Perl OO and accessors
by sauoq (Abbot) on Nov 29, 2005 at 22:13 UTC | |
by dragonchild (Archbishop) on Nov 29, 2005 at 22:34 UTC | |
by sauoq (Abbot) on Nov 30, 2005 at 02:55 UTC | |
|
Re^10: Perl OO and accessors
by herveus (Prior) on Nov 29, 2005 at 22:33 UTC | |
by fergal (Chaplain) on Nov 30, 2005 at 00:17 UTC | |
by herveus (Prior) on Nov 30, 2005 at 15:30 UTC | |
by fergal (Chaplain) on Nov 30, 2005 at 16:42 UTC | |
by Perl Mouse (Chaplain) on Nov 30, 2005 at 21:13 UTC | |
|