7 ms·
How can it be ridiculously verbose? Converting an arbitrary expression into C’s notion of true or false just requires adding != 0 or != nil or similar.
by mikeash 9y ago
How can it be ridiculously verbose? Converting an arbitrary expression into C’s notion of true or false just requires adding != 0 or != nil or similar.
- userbinator 9y agoTry to write something more succinct and elegant than this: return ((a > 4) + (b > 4) + (c > 4)) > 1;
- mikeash 9y agoreturn [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.
- maxxxxx 9y agoIf you only do C you know this stuff. It gets confusing when you use a few other languages that all have slightly different semantics.
- indigochill 9y agoI'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).
- sitharus 9y agoThat example has a boolean operator in it, so in a strict language will already be a boolean. 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.
- always_good 9y agomax(a, b, c) > 4
- cheepin 9y agoThis isn’t correct for a=5, b=0, c=0. It would return true while OP’s example would return false
- always_good 9y agoOh, I missed the "more than 1 integer". I was wondering why the other solutions were so awkward, thus needed to share my immense wisdom. I feel shame.
- mikeash 9y agoIf it makes you feel any better, there are surely other people who thought that way, and they'll be enlightened upon reading this comment chain.
- bunderbunder 9y agoTo me, [a, b, c].count(x -> x > 4) > 1 makes the intent of the code more immediately apparent.