I’m working in PHP (but in this case I think the programming language doesn’t matter), and in my class methods I usually meet the following situations:
- Method has to return true or false
- Method has to return true or error message
- Method has to return true + success message or false + error message
- Method has to return true + success results (object, array, whatever) or false
- Method has to return true + success results (object, array, whatever) or false + error message
- etc.
My problem is, that when I use this class methods in my code somewhere I always have to come back to the class, and check what is the method actually returning: simply true or false, true or error message, etc.
Is a good idea to standardize the returning values? If yes, how?
My idea is:
- if function has to return true or false then simply return true or false
-
if function has to return true or error message then:
if (success) { return array( TRUE, null ); } else { return array( FALSE, $error_message ); } -
if function has to return true + success message or error message then:
if (success) { return array( TRUE, $success_message, ); } else { return array( FALSE, $error_message ); } -
etc.
I hope you understand guys my problem, even thought my explanation wasn’t so good 🙂
What are your suggestions, or best practices? How should I handle this?
UPDATE:
Let’s take a simple example:
function login($username, $password)
{
// Login logic here ..
if ($logged_in)
{
return TRUE;
}
{
return $error_message;
}
}
So the correct way to do this will be: return true, or throw exception, and when calling the login method do in withing a try catch. So, when somethong goes wrong (validation fails, etc) I should use exceptions.
I’d say the premise of returning a boolean and something else is misguided.
A function should have a clear purpose with a clear result. If this result can be achieved, the result is returned. If the result cannot be achieved, the function either returns
falseor throws an exception. Which is better depends on the situation and your general error-handling philosophy. Either way, it’s not typically useful to have a function return an error message. That message is not useful to the code that called the function.PHP has its own mechanism to output error messages in addition to returning
falseresults:trigger_error. It’s purely a tool to aid debugging though, it doesn’t replace the standard return value. It can be a good fit for cases where you want to display error messages purely to aid the developer though.If a function is complex enough to possibly result in several different types of errors that need to be handled differently, you should use exceptions to do so.
For example, a very simple function with a clear purpose that only needs to return
trueorfalse:A function with a purpose that may fail to fulfill that purpose:
The same function that also triggers a message, useful for debugging:
A function that may legitimately run into several different kinds of errors that the calling code needs to know about:
And I’ll paste this comment here as well: