On Tuesday, September 16, 2003, at 02:53 pm, Andy Wardley wrote:

Steve Purkis wrote:
        # chaining:
        $obj->foo( $a_value )
            ->bar( $another_value );

I recognise how useful this can be but I'm not a fan of it. IMHO, the foo() method of an object should return the foo of the object. It shouldn't magically switch to returning the object itself just because you passed a parameter.

Of course you can argue that it shouldn't magically switch
from getting to setting based on there being a parameter or
not, but that doesn't bother me so much.  They are both operations
on the foo of the object and the method name doesn't make any
implication about what kind of operation.  The only implication
is that you'll get the foo returned back when called.

On the other hand, I find it quite acceptable to have get_foo()
that returns the foo, and set_foo($new_value) which sets the
new value for foo and returns the object.  In this case, the
action is explicit in the method name.

  # this is when chaining is good, IMHO
  $obj->set_foo(10)->set_bar(20)->set_baz(30);

Question is: what style should be the default?  I'm not looking for a
debate here,

Ooops, sorry.


If you just want numbers, then I'm for classic.  I like chainability,
but only if the accessor is prefixed with 'set_' or something similar.
The foo() should always return the foo(), IMHO.  If it doesn't then
chainability is broken for accessing items.

  # why chaining is bad, IMHO
  $book->author()->name();       # author.name
  $book->author($a)->name();     # book.name

One returns the author name, the other returns the book name.  And what
if $a is undef?  Does that return the book name or author name?

Oops, sorry. No debate. I forgot.

;-)


I see your point, but like I've said before, methinks this is a style issue. I'm trying to find out which style most people prefer so the majority of people can benefit from the module.

But you bring up some points I've gotta answer -- first off, 'set_' & 'get_' don't seem to be too popular (laziness?) so unless people prove me wrong here, they're out for accessors.pm. [1]

Secondly, with the current implementation an 'undef' argument will trigger a set for both 'classic' and 'chained' accessors:

$book->author($a); # still sets when $a = undef

If you pass an empty list, it will act as a get and your original code would prolly break:

$book->author(@a)->name; # buggy code if @a = ()!


If you have a preference here, let me know.

Dark chocolate and raspberry wheat ale. :-)

Mmm.. evil tempter..
Uppercanada used to do this maple beer that was really nice... <digs around> try:


http://www.uppercanada.com/template.asp?CName=ATheBeer2

-Steve

[1] As a complete aside, I don't think 'get_' and 'set_' are bad ideas. There's no reason reason someone couldn't write 'accessors::setget', or 'getters' and 'setters' pragmas. Michael Schwern recently pointed me to Ruby's 'attr_reader' and 'attr_writer' shortcuts - see http://www.rubycentral.com/book/tut_classes.html




Reply via email to