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

Re^5: Mutator chaining considered harmful

by eric256 (Parson)
on Dec 30, 2004 at 19:44 UTC ( #418364=note: print w/ replies, xml ) Need Help??


in reply to Re^4: 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.

$foo->bar(2) ->baz(3) ->qux;

Is no more difficult to read than:

my $bar = $foo->bar(2); $bar->baz(3); $bar->qux;

My point all along has been that it is only hard to read if you aren't used to the style. Map, grep and sort used to be nearly impossible for me to read because that wasn't a style I had encountered before. Now I understand and recognize those patterns. If I know that the mutators return the object in order to allow chaining then that code is easy to follow. If I don't know that then a quick check to the POD makes it clear in an instant.

The first requires a lot more effort to decipher than the 2nd.

That is only true for those who arn't used to it, or don't use it. It is not true for everyone, it is not an absolute that one is harder or easier to read. Like I said originaly, method chaining (mutators or otherwise) has always made perfect sense to me, maybe it's the way I think or experiences I've had. Either way, arguments that it's bad because it's hard to decipher, read, trace, etc, are all false because it is just a matter of the readers perception/experience.

Frankly, I' amused you think that's condescending because I think it's condescending to assume that because some people can't read it that it should be considered harmfull by everyone.


___________
Eric Hodges


Comment on Re^5: Mutator chaining considered harmful
Select or Download Code
Re^6: Mutator chaining considered harmful
by revdiablo (Prior) on Dec 30, 2004 at 20:28 UTC
    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.

      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

      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.

      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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2014-11-23 06:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (128 votes), past polls