return ((a > 4) + (b > 4) + (c > 4)) > 1; return [a, b, c].filter({ $0 > 4 }).count > 1
I disagree that your version is elegant. Succinct, yes, but I don’t think I’d like to leave that for the next guy to discover. 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 better example would be one that returned 0, so you’d have to add `== 0` to it.
I kind of understand your point with asm, but with higher level languages we’re optimising for maintenance rather than performance.
[a, b, c].count(x -> x > 4) > 1
makes the intent of the code more immediately apparent. max(a, b, c) > 4