in reply to Functions that return nothing, nada, failure...
So if return; is just as right as return ();, but twice as fast, when would you use return();?
I vaguely remember the meme "use return with no arguments to return false to any context" as received wisdom, but I can't find any such citation.
I would argue that it is sufficiently common usage that it should probably be the default way to signal "false" unless you have good reason otherwise.
If you find yourself in the situation where there are no valid sentinel values to indicate "false" (perhaps you are returning a list where the empty list might be a perfectly valid response) then you need to signal failure some other way. Three come immediately to mind:
In my own code, I can mostly get away with dealing with empty lists as a normal occurance; if I don't get any answers back, then I just don't do anything. Relying on foreach, map, etc, and avoiding keeping any meta-data about these collections in other variables makes this style straightforward and easy to read.
Exceptions let me write most of my code without worrying about the corner cases; I catch those exceptions towards the top of my code, where I have large chunks of work that I can either accept or reject ("commit" or "rollback", if you like.)
(One thing I would love is if the Fatals module would create detailed exception objects for every call mentioned; currently it is a bit cumbersome to customize the details for each case, so I still have ... or die "..." chunks throughout my code.)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Functions that return nothing, nada, failure...
by adrianh (Chancellor) on Jun 01, 2004 at 10:54 UTC | |
by tkil (Monk) on Jun 01, 2004 at 20:03 UTC |