3 ms·
I'll make a suggestion that's more verbose but maybe(?) more elegant (in the sense I'd argue it's more quickly evident what's being done and might conditionally
by indigochill 9y ago
I'll make a suggestion that's more verbose but maybe(?) more elegant (in the sense I'd argue it's more quickly evident what's being done and might conditionally improve performance):
if (a > 4) {
return b > 4 || c > 4
} else {
return b > 4 && c > 4
}
My argument is that the boolean operators make it more evident immediately that the return value is a boolean value (well, a 1 or 0 in C). Also this doesn't explicitly check the case of all the checks evaluating to true, but it doesn't need to because in that event then the if condition evaluates to true and the return value is true, which is the correct return value in that situation.
You could use the ternary operator in place of the if syntax to reduce the verbosity, but I find the smaller statements easier to read quickly.
Additionally, I think(?) this code would have an ever-so-slight performance advantage in some situations since in the event a > 4 and b > 4 evaluate to true, then c > 4 doesn't get evaluated. Same if a > 4 but b < 4.
Not with a computer I can easily run C on at the moment to check these claims, but I welcome input on the elegance/performance/readability comparisons.
- a_t48 9y agoBranch prediction might make this slower. (And if you have to add a fourth variable it will be harder to extend).