Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^7: Mutator chaining considered harmful

by Anonymous Monk
on Dec 31, 2004 at 01:33 UTC ( #418453=note: print w/ replies, xml ) Need Help??


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

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?


Comment on Re^7: Mutator chaining considered harmful
Re^8: Mutator chaining considered harmful
by revdiablo (Prior) on Dec 31, 2004 at 02:12 UTC
    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.

        He said "the variable name and usage".

        His point is that

        $foo->bar($x)->baz($y)
        can be either
        $foo->bar($x); $foo->baz($y); # same object
        or
        my $bar = $foo->bar($x); $bar->baz($y); # different object
        which is the ambiguity he's talking about.

        ihb

        See perltoc if you don't know which perldoc to read!
        Read argumentation in its context!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2014-09-19 04:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (129 votes), past polls