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

Re^4: The Accessor Heresy

by Roy Johnson (Monsignor)
on Nov 28, 2005 at 19:02 UTC ( [id://512330]=note: print w/replies, xml ) Need Help??


in reply to Re^3: The Accessor Heresy
in thread The Accessor Heresy

The interface is the important thing. The interface is a big part of maintenance, because (presumably) the interface will be used in lots of code.

Your measurement of code complexity is deceiving. For each property, the only additional code is the new method, which is trivial. With more methods on an object, the overhead as a percent would shrink. The whole idea behind OO design is that it is intended for scalability, not that it can implement trivial things trivially.

All at the expense of performance and for what purpose again?
Expandability, for one. If you haven't anticipated all the functions that will be handy for your properties, you're stuck implementing them as additional operate_on_Doohickey type procedures unless you change all the Doohickey-related methods. Your Doohickey can never be a real boy object. I can just add the methods to the Doohickey package, and away they go. And if I have to debug it, my haystack is actually smaller, because it's modular.

I'm not saying it's always the way to go — I'm sure it's often not — but it is something to consider. You seem to be saying that it's never the way to go.


Caution: Contents may have been coded under pressure.

Replies are listed 'Best First'.
Re^5: The Accessor Heresy
by sauoq (Abbot) on Nov 28, 2005 at 19:59 UTC
    I'm not saying it's always the way to go — I'm sure it's often not — but it is something to consider. You seem to be saying that it's never the way to go.

    Certainly it is sometimes useful; which is why it has been around for ages and is used extensively. Think "has-a" relationships and delegation (original def'n, i.e. forwarding or consultation or wtf you want to call it.) It's the wholesale application of this design even where it makes no sense and costs a lot more than it buys that I disagree with. As in your example, for instance.

    An "Area" class? What in heaven's name for? (I haven't seen one compelling reason for one in this whole thread.)

    What I take issue with, really, is your motivation behind this which, from your OP, seems to be centered around these statements of yours:

    In OO, things that you interact with are supposed to be objects, and you interact via their methods. Accessible properties are things that you interact with, but they often aren't implemented as objects, and their methods are owned by their parent objects.
    In other words, it really seems you are saying something like, "In OO, things that you interact with are supposed to be objects and, since we interact with attributes let's make them all objects." As if that's reason enough in itself.

    I'm saying as loudly and clearly as I can that it's not reason enough. In the example you've given you've created a lot of bloat for no real gain and , worse, you've paid costs in terms of both performance and maintainability. The natural conclusion: over-engineering gone awry... It's the... uh... Death of KISS.

    But, if you want to retract most of your original node and instead say something like "sometimes it's useful to implement a has-a relationship" or "delegation (forwarding/consultation/whatever) is a nice technique to understand" then, by all means, I will agree with you.

    -sauoq
    "My two cents aren't worth a dime.";
    
      Think "has-a" relationships
      Every property is a "has-a" relationiship. A circle "has-an" area. An employee "has-a" salary.
      It's the wholesale application of this design even where it makes no sense and costs a lot more than it buys that I disagree with.
      Since I didn't endorse the wholesale application of that design, I have no idea why you're disagreeing with it to me.
      An "Area" class? What in heaven's name for?
      I never suggested that an Area class was, specifically, a good idea. I'm sure you've seen examples before that are not pulled from real world usage.
      In other words, it really seems you are saying something like
      Of course, I've said nothing of the kind. If you would concern yourself with what I say, rather than what it seems like I'm saying something like, we might both be significantly less annoyed.
      if you want to retract most of your original node [...] I will agree with you.
      It's not at all clear that you're familiar with the original node. I can't retract most of "what it seems like I'm saying something like." I could only retract something I said. I explained that the usual implementation of accessors is not OO design, and that's why they rankle. I demonstrated what an OO approach would be. You haven't disagreed with any of that.

      Caution: Contents may have been coded under pressure.
        Every property is a "has-a" relationiship. A circle "has-an" area. An employee "has-a" salary.

        So what, exactly? An employee has-a salary therefore you should implement a Salary class? It doesn't follow. In those cases where it makes sense to do this... well... it frequently already is. So, on one hand, you aren't offering anything that isn't already common practice.

        Since I didn't endorse the wholesale application of that design, I have no idea why you're disagreeing with it to me.

        You never discussed the conditions under which you think this design would be the right one to use. Instead, you made statements like "I believe I understand what it is that makes accessors rankle so much" and "An OO approach would be to have the properties be 'sub-objects' all of which implies the sweeping generalities that using this technique will make accessors rankle less and be a true "OO approach".

        Now, don't blame me for reading between the lines. If you want to communicate something different, than do so. Explicitly.

        Like I said above, on one hand you are offering an approach that is already common practice (though you haven't offered it as a tutorial but more like it was something original.) On the other hand, you are suggesting that this technique is an "OO approach" ostensibly exlcuding the OO-ness of other approaches. Or, at least, recommending more widespread use. Or something. Certainly you offer it as some sort of cure for rankling accessors, though you don't really cure anything by just adding another object in the chain to those accessors. Afterall, you still end up with ...->set(5).

        I never suggested that an Area class was, specifically, a good idea. I'm sure you've seen examples before that are not pulled from real world usage.

        Sure, but you never deigned to talk about the drawbacks of your example either. Nor what exactly you were attempting to illustrate. You just wrap an attribute up in a class and call it a good thing. That's why I called it useless abstraction.

        Of course, I've said nothing of the kind. If you would concern yourself with what I say, rather than what it seems like I'm saying something like, we might both be significantly less annoyed.

        I haven't really been all that annoyed up until now. So, you're the perfect communicator here, right? And anyone who has a perception of what it is you are saying is somehow at fault for not reading your mind? All right, Roy, here's what I think: the practice of wrapping accessors up as objects simply for the sake of making things more "OO" is about as ridiculous a notion I've heard in quite some time and I can't imagine why anyone would think it deserving of an meditation.

        I explained that the usual implementation of accessors is not OO design, and that's why they rankle. I demonstrated what an OO approach would be. You haven't disagreed with any of that.

        Actually, I've disagreed with all of that. Particular with the claim that you've demonstrated an OO approach. Perl isn't an OO language. You haven't escaped from that fact. It doesn't matter if you use $foo->set_baz(42) or $foo->bar->baz->set(42), you are still using "42" and, in Perl, that's not an object.

        -sauoq
        "My two cents aren't worth a dime.";
        

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-20 00:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found