Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: chaining method calls

by chromatic (Archbishop)
on Jun 11, 2003 at 23:32 UTC ( [id://265223]=note: print w/replies, xml ) Need Help??


in reply to chaining method calls

I like it. It feels very Smalltalky. You're sending a bunch of messages to the object, so why repeat the invocant? Expressions have results, so why not use them?

I co-opted this in Test::MockObject quite some time ago. It works well for accessors there. I don't know if I'd use it elsewhere.

Replies are listed 'Best First'.
Re: Re: chaining method calls
by dws (Chancellor) on Jun 12, 2003 at 03:55 UTC
    I like it. It feels very Smalltalky. You're sending a bunch of messages to the object, so why repeat the invocant? Expressions have results, so why not use them?

    In Smalltalk this is called a "cascade", and there's special syntax to support it. In Smalltalk, if you send two messages to the object anObject (i.e., if you invoke two of anObjects's methods), you can use cascading to rewrite

    anObject foo. anObject bar.
    as
    anObject foo; bar.
    Both messages get sent to anObject, regardless of what the first method returns. That is, the return value from foo is ignored. The value of this expression is whatever is returned from bar.

    Perl has no notion of method call cascading, so to simulate cascading, all member functions in a simulated cascade chain (except that final one) need to return self. This works great if you're making a chain of calls for side-effects, and less great if you're composing an expression from a set of cascaded calls, and are interested in the final value, because of the requirement that all cascadable methods return self.

    My opinion is that cascading is an idiom that doesn't translate well into Perl. Without a special syntax to designate a cascade, a simulated cascade is indistinguishable from a gross violation of the Law of Demeter.

      My opinion is that cascading is an idiom that doesn't translate well into Perl. Without a special syntax to designate a cascade, a simulated cascade is indistinguishable from a gross violation of the Law of Demeter.

      That's an interesting point, and the most convincing one I've come across against the idiom. I can see how a maintainence coder might see a cascade and have it look like a ghastly exposure of internals ripe for refactoring.

      That said, it's not a problem I've ever come across. Probably because that by the time you're looking at the code you already have an idea of what methods are associated with what objects, so you can mentally parse out cascades from other code.

      The fact that I try not to write objects that flagrently violate the LoD probably helps too :-)

      (I wonder if Perl6 will have an explicit syntax? If not we could always make one up.)

      I still find:

      $my_object_variable->foo($x)->bar($y)->ni($z);

      clear, and preferable to:

      $my_object_variable->foo($x) $my_object_variable->bar($y) $my_object_variable->ni($z) # or for ($my_object_variable) { $_->foo($x); $_->bar($y); $_->ni($z); };

      To me the former is succinct and obscures the mainline less. Probably too much Smalltalk at an early age :-)

        $my_object_variable->foo($x)->bar($y)->ni($z);
        The trouble is, that sort of construct already has a meaning and this isn't it. Chaining methods on different objects is common. Like this, for example:
        $car->wheels()->turn('right');
        That kind of thing is common on most OO languages, and it's what I expect when I see chained method calls. This "return yourself" business foils my expectations.
        >> wonder if Perl6 will have an explicit syntax? It's my understanding that perl6 will change the method call operator to operate on the current blocks default variable ($_ is always the default var in perl5). Which allows you write things like:
        sub foo { my $bar=new Obj, is default; #ok, I don't remember the exact syntax b +ut I'm sure you get what I mean .foo(); .baz(); .qux(); }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-03-28 16:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found