I work in PHP (but in this case I think the programming language does not matter), and in my class methods I usually come across the following situations:
- The method should return true or false
- The method should return true or an error message
- The method should return a True + error message or a ++ error message
- The method should return true + success results (object, array, whatever) or false
- The method should return true + success results (object, array, whatever) or false message + message
- and etc.
My problem is that when I use the methods of this class in my code somewhere, I always need to go back to the class and check what the method returns: just true or false, true or an error message, etc.
Is it good to standardize return values? If so, how?
My idea:
- If the function should return true or false, then just return true or false
if the function should return true or an error message, then:
if (success)
{
return array(
TRUE,
null
);
}
else
{
return array(
FALSE,
$error_message
);
}
if the function should return an error message + successful message or error message:
if (success)
{
return array(
TRUE,
$success_message,
);
}
else
{
return array(
FALSE,
$error_message
);
}
and etc.
I hope you guys understand my problem, even thought that my explanation is not so good :) What are your suggestions or recommendations? How should I do it?
UPDATE: Take a simple example:
function login($username, $password)
{
if ($logged_in)
{
return TRUE;
}
{
return $error_message;
}
}
, : return true throw exception, try catch. , somethong ( ..), .