7 ms·
I think you can just do: if (cellA > cellB) return 1; if (cellA < cellB) return -1; if (cellA === cellB) return 0; Why is the switch better? Or ev
by XCSme 7y ago
I think you can just do:
if (cellA > cellB) return 1;
if (cellA < cellB) return -1;
if (cellA === cellB) return 0;
Why is the switch better?
Or even (less explicit, but shorter code):
if (cellA === cellB) return 0;
return cellA > cellB ? 1 : -1;
- cfv 7y agoA switch is literally a series of chained ifs. In most languages ir takes the form of a series of If(Variable == $CaseValue){ Yield $CaseBody() } But in some languages, like js, nothing is preventing you from hacking it to do something different. EDIT: Just how petty, sheltered and dumb does a person have to be to downvote a statement of fact and then not leave a response to justify that? Grow up already.
- pythonaut_16 7y agoIn that case you're not really hacking it. If you think about what a switch does and how it works, it makes perfect sense. It's just a bit unusual to do it that way.
- cfv 7y agoIs it not though? By exploiting the conditions to evaluate to true == $CaseValue you're subverting the expectation that the choices all rely on a single variable, all the while breaking fallthrough and default.
- marcus_holmes 7y agopresumably downvoting JS not your post hehe
- deleted 7y ago[deleted]
- K0SM0S 7y ago'Hacking' doesn't even begin to cover it. How about we don't `break` like we should (!) and instead have fallthrough¹ cases that execute several cases— or more —on purpose²? Oh, switches. Always the horror. In networking they somehow replaced bridges, but it's the same thing (that was never confusing to anyone, obviously). In (my) programming, a switch so often turns out to be a nice dressing hiding some horror pattern. And that Nintendo thing... nice games but 720p 30fps? Really?? When I re-read / refactor, I've got this nagging phrase in my mind, whenever I see one: “Kill the switch! Death to switches!” To conclude this perfectly balanced and nuanced opinion (!): some switches find their place; but most don't, and never should. ____ 1: SO: Should fallthrough be “allowed”? I move that whatever congress runs your country should outlaw it! But exceptions can be made... https://stackoverflow.com/a/188506/5376417 https://stackoverflow.com/a/188506/5376417 2: Book: (NSFW // Programming horror // mutilated code in this example) https://eloquentjavascript.net/02_program_structure.html#h_jMKsa0SXdL https://eloquentjavascript.net/02_program_structure.html#h_j... (upvoted you, if only to balance the weird ones) (note: this comment is meant absurd. do not downvote yourself into a reading mistake, the tone is funny ;-) Vanilla Javascript is funny.
- lmm 7y agoIt makes it clear that you're going to execute only one of the possibilities. Maybe that's clearer than a page full of ifs. But yeah, not particularly fond of the idea.
- oefrha 7y agoExcept JavaScript switch has fallthrough, so it doesn’t make that clear at all. (Sure, no fallthrough happens with return in each case, but neither would a bunch of plain if ... return.)
- simonsarris 7y agohah, well then you can of course continue... return (cellA === cellB) ? 0 : (cellA > cellB ? 1 : -1);
- XCSme 7y agoKeep going: return (cellA > cellB) * 1 + (cellA < cellB) * -1; More: return Math.sign(cellA - cellB);
- XCSme 7y agoYou don't need the * 1 multiplication: return (cellA > cellB) - (cellA < cellB);
- xxs 7y agodoes work for NaN... rookie mistake, eh
- amelius 7y agoMath.sign trick doesn't work for strings.
- XCSme 7y agoThat's true, but the default string comparison sometimes isn't that useful, right? I somehow assumed the data is numbers only, rookie mistake :) 'ana' > 'Bob' '2' > '123' You usually want to clearly define the order or pre-process the strings in some way (trim them, same casing, etc).
- amelius 7y agoWell, you can pass the strings through .toUpperCase() before comparing them. Or indeed drop the <, > based approach and use: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Collator https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... Or: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- marcus_holmes 7y agoSwitch statements indicate "it's one of these things", whereas in the series of ifs any of them could be true. Using a switch statement tells the reader that you expect one of these things to happen [0]. Using a series of ifs tells the reader that these things might or might not happen depending, and they're going to have to read each statement to work out which. Your last version doesn't tell the reader what your intention is at all, and they need to work out what you're trying to do here. It's a lot less readable. Unless you're desperate for those bytes, it'd be better to use the switch statement for this case. [0]: This gets a little weird in languages like JS where switch statements fall through to the next one if you don't break or return, but generally it still holds true.
- foldr 7y agoI don't think this line of reasoning makes sense, since as you acknowledge, JS switch has fallthrough. I would not immediately assume that the coder's intent was for only one branch to execute if I came across a 'switch' in some JS code.
- TheRealPomax 7y agoThat's not how switches work: a switch can (and will) trigger lots of cases if you don't either `break` (highly unintuitive to new programmers, who like you believe switch is "do one of these") or `return`, either of which cuts cuts the switch short.
- jannes 7y agoWith if you can also indicate "it's one of these things" by throwing an error at the end: if (cellA > cellB) return 1; if (cellA < cellB) return -1; if (cellA === cellB) return 0; throw new Error();
- bluedino 7y agoLet me tell you about if..else!
- deleted 7y ago[deleted]
- xxs 7y agoboth fragments are incorrect for NaN... The correct is return a<b?-1: (a>b? 1 :0)
- crznp 7y agovar a=NaN, b=1; a<b ? -1 : (a>b ? 1 : 0); // 0 var a="a", b=1; a<b ? -1 : (a>b ? 1 : 0); // 0 The switch and if statements would return undefined in these cases. Or others threw an error if none of the conditions matched, that would work too.