What’s the best practice when returning something from a routine? Should I return a status bit always or just on failure? For example:
Return(0, “Failed because….”) on failure,
Return(1, success_value, second_success_value) on success.
Or
Return(0, “Failed because….”) on failure,
Return( success_value, second_success_value) on success.
I usually program in Perl, but I guess the question stands for whatever language I might try to program in. Thanks!
The answer is very language dependent. You should follow the idiom for the language you are using. The idiom for Perl, which you mentioned, is to return zero(0) for success and other values for failure. If you need to return multiple values, as in your example, one of them could always be the success/failure code.
If you’re using a language that supports exceptions (eg. Java, C#, C++) then you should indicate exceptional conditions (eg. failures) using an exception. If the routine completes normally (ie. no exceptions) then it can be assumed to have succeeded and any returned values can be used safely.