4 ms·
I am starting to hate languages that are not super picky and precise. We are working with C#, PHP and JavaScript and between them it's almost Impossible to reme
by maxxxxx 9y ago
I am starting to hate languages that are not super picky and precise. We are working with C#, PHP and JavaScript and between them it's almost Impossible to remember what will evaluate as what. I would be perfectly fine if the only input to an if was true or false, no number, no strings.
- Waterluvian 9y agoA common JS pitfall: never ever use == in JS. Always use ===
- mikeash 9y agoSame here. I’ve used C for ages and now I’ve been doing a lot of Swift, where if statements only operate on Bool values. I find it much better to be explicit about any condition that isn’t literally true or false. The common style of accepting all types and defining a set of “falsey” values just encourages pointless shortcuts.
- userbinator 9y agowhere if statements only operate on Bool values I mostly use Asm and C, and I have the exact opposite opinion: I hate languages like that. It makes simple problems like "determine if more than 1 of 3 integers are greater than 4" require ridiculously verbose solutions.
- mikeash 9y agoHow 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.
- lgas 9y agoYou might like Haskell. The only possible conditional is a boolean and in general it's quite picky and precise about everything (with a few notable exceptions like partial functions).
- maxxxxx 9y agoI also think i would like Haskell. However, it seems there is not much demand for it in the market.