Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Your second example is interesting, but I wouldn't call it method chaining at all: Method chaining is a mechanism to change related attributes, which have very little effect on each other. Consider:
$person->make_head('large') ->make_nose('small') ->make_belly('surgerized');
This is method chaining. What you are doing (externally), however, is calling a method on the seventh column of a table. It is best achieved simply (IMHO) by returning a reference to an object of package column, whose methods you can call (your probably knew this, but I'd rather emphasize the point for other readers). Code would be something like:
package Table; use Column; sub new { bless [],__PACKAGE__ } sub col { return $_[0]->[$_[1] + 1] if exists $_[0]->[$_[1] + 1]; $_[0]->make_column([$_[1] + 1]) and return $_[0]->[$_[1] + 1]; } sub make_column { $_[0]->[$_[1]] = Column->new; }
Like you said, you're even doing that in your first example. You're just enapsulating a series of objects in a greater object. The method I use, though, would make it so that you don't have to even hold the differant objects. ( sub aggregate { $_->duckwalk($_[1]) for @{$_[0]}) } )

There are no similarities between that and object chaining- you are not returning self; you are returning a different object...
Aggregators are perfectly intuitive, because most methods return something. For aggregators that something returned is a different object, but that's no different from 5.squared returning 25, if you consider 5 and 25 to be objects of NUM or something like it (like they are in Ruby and other languages).

However, method chaining is completely counterintuitive, because it's not natural to have $object->make_head('larger') return the object with the head made larger. One would expect it, like most methods, to return true on success and false on failure. This is the programming idiom that most programmers would subscribe too. If something is not returning true or false, then OO standards would dictate that it's returning another object, which will be modified. If you were to give me the same code I previously mentioned without telling me that you're employing method chaining, I'd ask you why you're modifying the belly of a nose. What is the belly of the nose?

This point is hammered in by that notion. However, intuitively, my rejection of method chaining is that it doesn't make sense without the requisite white space. Programmers are almost forced to indent when they chain methods to show that all the methods are basically working off of the single parent object. When indentation is forced for a process so simple as changing an object you know there's a problem.
Gyan Kapur
gkapur@myrealbox.com

In reply to Re: object aggregators by Revelation
in thread chaining method calls by perrin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (6)
    As of 2014-09-16 23:36 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

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











      Results (53 votes), past polls