Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re: Re: Re: chaining method calls

by demerphq (Chancellor)
on Jun 15, 2003 at 01:06 UTC ( #265979=note: print w/replies, xml ) Need Help??

in reply to Re: Re: chaining method calls
in thread chaining method calls

You just put your finger on what I don't like about this: it is not a common idiom.

I keep seeing people say this idiom isn't common, which to me is a bit of a suprise as Data::Dumper employs this idiom, and even has


in its synopsis, and

The method forms return the object itself when called with arguments, so that they can be chained together nicely.

in its method documentation. As Data::Dumper is one of my earliest memories of Perl (the wonder of watching that data structure stream down the screen!) I have always assumed this idiom was common place. (And shouldnt every Perl hacker past their first script know about Data::Dumper? The thought of hacking perl without it make me cringe.)

Having said that, Ive employed the idiom myself on occasion, and on occasion found myself removing it afterwards. I think its an optimise-for-the-common-usage judgement call when designing a classes interface. If its likely that you would want to set many attributes of an object at once, without being concerned with the new or old value then it can be convenient to provide chaining. OTOH, sometimes I find that having set accessors return the new value (or the old value sometimes) can be a big code simplification, so sometimes I go that way. I do whatever "feels" appropriate for how I envisage myself and others using the routines in question. So long as the behaviour is documented I dont see any problem with any of the common approaches, nor with using any mixture of them in a single module, so long as the end result is reasonably intuitive.


<Elian> And I do take a kind of perverse pleasure in having an OO assembly language...

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://265979]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2017-08-18 10:51 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (299 votes). Check out past polls.