Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: The Accessor Heresy

by lhoward (Vicar)
on Nov 28, 2005 at 21:01 UTC ( #512371=note: print w/ replies, xml ) Need Help??


in reply to The Accessor Heresy

My problem with this approach is the disregard for the Law of Demeter. In short: Only talk to your immediate friends.

The Law of Demeter is intended to encourage shy, decoupled code (Pragmatic Programmer p.138) to promote maintainability & adaptability. Without the Law of Demeter, objects depend more on the internal structure of other objects. The trade off is that you end up writing more accessor methods.

In this example, if Circle::Radius or Circle::Area were changed (or replaced by other objects) interface compatibility must be maintained, not only for Circle but for all the other code that works with Circle objects. If the Circle::Radius & Circle::Area objects are accessed through accessor methods of Circle, then their interface details are hidden from any users of the Circle class.

Of coruse, any tenet of programming if taken too far leads to horrible code.

L


Comment on Re: The Accessor Heresy
Re^2: The Accessor Heresy
by Roy Johnson (Monsignor) on Nov 28, 2005 at 21:32 UTC
    The reason the Circle::Radius object exists is that it is part of the Circle object model. It is not an implementation detail, any more than Circle itself is. It is the portion of the Circle public interface that deals with radius-related property fiddling. Instead of embedding the name of a property in the method name, you would have an object by that name that has a simple method name. For example, you prefer:
    $myCircle->operate_on_Radius;
    Inherent in that name is the fact that there is a Radius property, a sort of implicit object. All else being equal, I would prefer to see
    $myCircle->Radius->operate;
    If either of us got rid of the concept of Radius, we would have to change our API. There is no benefit of either API over the other as far as that goes. If you change the object model, you have to change the API. Because the API is supposed to reflect the object model.

    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://512371]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (11)
As of 2014-04-17 07:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (441 votes), past polls