Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^4: Mutator chaining considered harmful

by Anonymous Monk
on Dec 29, 2004 at 14:19 UTC ( #417986=note: print w/replies, xml ) Need Help??


in reply to Re^3: Mutator chaining considered harmful
in thread Mutator chaining considered harmful

Mostly it makes it difficult to catch errors if those methods fail.

But not any more difficult that catching an error in *any* chaining call. The OP had no problem with:
my $label_formatting = $button->child->font_style;
but if there's an error in the child method, it'll be as hard to catch as in:
$window->title('foo')->border(20);

And if your methods just throw exceptions on errors, you can just wrap it all in an eval:

eval {$window->title('foo')->border(20)}; if ($@) {...do something...}
If that isn't good enough for you, because you need fine grained control on catching errors, you can always write:
eval {$window->title('foo')}; if ($@) {....} else {eval {$window->border(20)} if ($@) {....}}
The point is that mutators returning $self allow for either style. Chaining for those who want it, and fine control for those who want that.

Returning the argument passed in is IMO not very useful. The OP mentioned "that's how = does it - it allows you to write $foo = $bar = $baz;. That's true, but the OO equivalent would be:

$obj->foo($obj->bar($baz))
which, IMO, gets unwieldy very quickly and isn't very readable either. I also have seldomly any use for that.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://417986]
help
Chatterbox?
[Corion]: hippo: Yeah, but at least two years ago, it still was close enough to 20% cut
[Corion]: But I have a very positive experience with a four day workweek and a three day weekend. I can't easily go back though to full money.
[Corion]: That is easy without having to pay for a house, a wife or children though. If I had any of these, or any two of these, the decision wouldn't be that easy.
[ambrus]: wait. I understand no wife and children, but how do you not have to pay for a house?
[hippo]: I had a low-paid job about 20 years ago and seriously considered going down to a 3-day week. Would have worked 60% of the time for about 80% of the cash.
[Corion]: ambrus: Well, I pay rent, but don't own a house with variable/ unforeseeable costs
[hippo]: The thesholds/benefits balance at the time was nuts.
[hippo]: But that doesn't last because, you know ... politicians. :(

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (11)
As of 2017-09-21 15:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    During the recent solar eclipse, I:









    Results (249 votes). Check out past polls.

    Notices?