3 ms·
My philosophy has always been: Clearly separate the input from the result from the error, using different features of the language for each. Pass input variable
by ryandrake 4y ago
My philosophy has always been: Clearly separate the input from the result from the error, using different features of the language for each. Pass input variables only as arguments, return only the "result" of the function, and throw errors. Take the classic (and simplistic) divide function, in pseudocode. There are a number of ways you can do it, and I'm not listing them all:
divide(numerator, denominator) returns quotient, throws error
divide(in numerator, in denominator, out quotient) throws error
divide(in numerator, in denominator, out quotient) returns error
divide(in numerator, in denominator, out quotient, out error)
divide(numerator, denominator) returns tuple(quotient, error)
I've always considered the first one more expressive, more usable by the caller (e.g. you can pass the result of the function directly to the next function), and the least confusing. The second one is -meh-.. I'm just not a big fan of input and output arguments. You have a return value, why not use it? The last three are particularly bad, because the caller must remember to check the error value manually (unless your compiler can remind you).