Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: chaining method calls

by Ovid (Cardinal)
on Jun 11, 2003 at 23:36 UTC ( #265226=note: print w/ replies, xml ) Need Help??


in reply to chaining method calls

When chaining method calls, an mutator only returns the object if the method call succeeds. As a result, it doesn't inhibit debugging because, in your example, if the proxy() method fails, you'll get a fatal error stating "Can't call method f2c on an undefined value". This does two things. First, it tells you that the proxy() method failed. Second, you no longer have to check whether or not each and every mutator was successful. By failing to return a copy of the object, you immediately get a fatal error.

It also has a nifty "dual use" feature: if you don't like chaining method calls, you don't have to. You can simply see if the method returns true or false upon success. Of course, checking each and every method can be a pain, so not everyone does it. However, if you try to set the day of the week to "9" or the color to "cheese", you have to check the method calls. Chaining gets this check for free, often even if you forget to add nice error checking in the method :)

Cheers,
Ovid

New address of my CGI Course.
Silence is Evil (feel free to copy and distribute widely - note copyright text)


Comment on Re: chaining method calls
Re: Re: chaining method calls
by perrin (Chancellor) on Jun 12, 2003 at 01:22 UTC
    Hmm, wouldn't this normally be handled by throwing an exception rather than returning undef? I always set 'RaiseError' on DBI for this reason.

      Yes, that's a better way of going about it. I'll confess that when I'm coding things quickly, I often do the simplest thing and just return the undef rather than raise an exception. In my tests, I get a chance to evaluate this how this actually works and when I have problems, I go back in and croak, carp, or silently ignore the issue, depending upon what it is and how it affects the actual problem.

      That being said, I really like tye's error object idea. If I can create a class that has an Exception object, no methods, and an AUTOLOAD, I can get robust error handling with plenty of flexibility. And again, if someone doesn't like chained calls, they still don't have to use them.

      Cheers,
      Ovid

      New address of my CGI Course.
      Silence is Evil (feel free to copy and distribute widely - note copyright text)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://265226]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (3)
As of 2014-11-28 03:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (192 votes), past polls