http://www.perlmonks.org?node_id=418384


in reply to Re^5: Mutator chaining considered harmful
in thread Mutator chaining considered harmful

You missed one giant and important point. If you are familar with the style then its not more difficult to decipher.

It is more difficult to decipher, because it's ambiguous. The ambiguity must be resolved somehow. In the first example, you have to follow the chain of methods and know what type of thing they return. In the 2nd example, the variable name and usage is a big clue that helps resolve the ambiguity.

My point all along has been that it is only hard to read if you aren't used to the style.

It's not more difficult to read, it's more difficult to understand.

Frankly, I' amused you think that's condescending

I say your view is condescending, because rather than accept the fact that someone understands a concept that they don't like, you instead decided they must not understand it fully.

Understanding how method chaining works is easy. Reading code that uses method chaining is not any more difficult than other types of operator or function chaining. The difficulty comes in when you don't know whether a method is returning the object it's working on, or a sub object.

Replies are listed 'Best First'.
Re^7: Mutator chaining considered harmful
by eric256 (Parson) on Dec 30, 2004 at 20:32 UTC

    The difficulty comes in when you don't know whether a method is returning the object it's working on, or a sub object.

    Finaly an objection that I can understand and agree on. Thank you for at least pointing that out to me. If it was implied in the other parts of this discussion then I somehow missed that.


    ___________
    Eric Hodges
Re^7: Mutator chaining considered harmful
by DrHyde (Prior) on Jan 04, 2005 at 10:03 UTC
    If you don't know what a method is returning, it doesn't help to store all those intermediate values in variables or throw them away or whatever. Whatever style you use, you need to read the documentation.

    The only valid argument I can see against method chaining (and this applies to all method chaining, not just mutator chaining) is that intermediate failures are hard to catch.

Re^7: Mutator chaining considered harmful
by Anonymous Monk on Dec 31, 2004 at 01:33 UTC

    The difficulty comes in when you don't know whether a method is returning the object it's working on, or a sub object.

    So, basically you are saying you don't think the method names are picked appropriately? Perhaps you'd prefer Hungarian notation for methods?

      So, basically you are saying you don't think the method names are picked appropriately?

      No, that's not what I'm saying.

      Perhaps you'd prefer Hungarian notation for methods?

      I would prefer that people stop using [and writing, and chaining] methods that return the object they operate on.

        I would prefer that people stop using {and writing, and chaining} methods that return the object they operate on.
        I would prefer having Natalie Portman as a girlfriend (having just seen Closer, the subject is fresh on my mind, sorry).

        But seriously...

        I find mutators-that-return-$self to be very handy. I've tried to understand your complaint. In the Alpaca book, I talk about mutators that return:

        • the new value (usually accidentally the default),
        • the previous value (ala umask),
        • $self (as in this discussion),
        • a success/fail code (for things that might fail being set),
        • or an unspecified result.
        I've seen it all. All have been useful over time. The real thing is to be as consistent as you can, and don't change it once you've committed.

        My File::Finder::Steps methods actually return a cloned-and-modified-$self, permitting a chaining of sorts. I find the notation very natural there. If I had to do it some other way, I'd probably scream. {grin}

        So, perhaps you are not comfortable with mutators returning $self, but they're probably here to stay. My guess is that you got burned once, and now you're out for blood. Chill. {grin}

        -- Randal L. Schwartz, Perl hacker
        Be sure to read my standard disclaimer if this is a reply.

        So, basically you are saying you don't think the method names are picked appropriately?
        No, that's not what I'm saying.
        Let's go through your argument. You said:
        It is more difficult to decipher, because it's ambiguous. The ambiguity must be resolved somehow. In the first example, you have to follow the chain of methods and know what type of thing they return. In the 2nd example, the variable name and usage is a big clue that helps resolve the ambiguity.
        Which to me means that you are purely relying on the name of the temporary variable to document what the (first) method returns. And this is because you otherwise don't know what's being returned. However, variable names follow the same rules as method names. Hence, if a variable name can document what value it contains, so can a method name. But you couldn't determine from the method name what it returned.

        Hence the conclusion that you find the method name used inappropriately.