Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: Use of "die" in OO modules

by imp (Priest)
on Jan 18, 2007 at 19:17 UTC ( #595322=note: print w/ replies, xml ) Need Help??

in reply to Use of "die" in OO modules

Code should fail as early as possible to avoid collateral damage, and to help identify the source of a problem. Using special return values will clutter the code with assertions, which decreases readability and increases the chance that you will miss an exceptional condition.

It is also difficult to come up with a consistent failure convention. Using undef is a bad idea as it is true in list context (any value is), and some functions will return undef as a valid response. -1 is less appropriate. You could bless an error object and return that, but that strikes me as an ugly solution.

I prefer to use Exception::Class, or if I am feeling lazy then die will do. Perl Best Practices has an excellent chapter on Error Handling (13) - I highly recommend it.

Comment on Re: Use of "die" in OO modules
Re^2: Use of "die" in OO modules
by theguvnor (Chaplain) on Jan 18, 2007 at 21:27 UTC

    Thanks for your input, though I should have specified one thing more clearly: when I said I return (undef), I meant that I just return;, I don't return undef;. I'm aware of the problem with returning undef explicitly.

    (For the unaware: a bare return gives the caller an undef, regardless of whether the call was in scalar or list context. Returning an explicit undef gives the caller undef in scalar context and a list containing one element (undef) in list context.)


Log In?

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2014-08-29 08:57 GMT
Find Nodes?
    Voting Booth?

    The best computer themed movie is:

    Results (277 votes), past polls