2 ms·
For my mind it only works if a meaning of a return value is “compatible” with an expression. E.g. if a function returns a status, but an expression is an intege
by wruza 4y ago
For my mind it only works if a meaning of a return value is “compatible” with an expression. E.g. if a function returns a status, but an expression is an integer comparison, the flow of logic skips an important conversion step.
bool is_db_open() {
return (db != NULL);
}
bool is_time_to_report() {
if (n_errors >= limit) {
return true;
}
return false;
// a little confusing way
return n_errors >= limit;
}
Also, if there’s a chance of an additional condition, they can be easily be added and documented.
if (n_errors >= limit) {
return true; // too many errors
}
if (now() >= deadline) {
return true; // periodic
}
return false;
For the same reason I often do this:
if (x == 0) {
return 0; // not `x`