I have a (common) situation where I am currently returning a result that is a mixed type, sometimes a boolean, sometimes an error message. For example:
function checked_thing_is_legal(){
// Do stuff and check for errors in here.
} // Returns true if there are no errors, otherwise returns an error message string.
This feels dirty, and someone once said “it’s good to distill code down to single, reliable return values”, which I find to be good advice. So what is a better paradigm to check for errors?
i see two options here
use atomic (boolean) validators and decouple error messages from the validator itself
use validator objects with boolean test() and string error() functions:
in a quick and dirty application you can also consider returning an empty value if everything is ok