Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( [id://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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-04-25 09:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found