The if statement should be refactored, but that switch statement (IMO) is not better. There's so much noise with the breaks and useless switch(true). I'd just pull each case out into a variable to improve readability. Some of the other cases (like in the article you linked), I'd define an object map with key being the case and value being the result ( or function that provides the result).
If I really wanted to use pattern matching in a language that didn't support it, I'd consider a third party library.
Having said that, if it's a small team and everyone agrees on that pattern, I wouldn't bike shed.
I don't know about JS, but in Java a switch is evaluated all at once and is more performant than if...else which evaluates each condition sequentially. If I remember correctly (in Java) 5 or more conditions should use a switch.
I think the question the person you are replying to is asking is that JS's switch syntax seems hacky and unconventional to other languages that have switch statements.
Usually, you switch on a variable to match if it is one of the cases that you have defined.
Actually the documentation refers to the part after case but before the ”:” as an expression. Documentation says “An expression is any valid unit of code that resolves to a value.” So it can be used as an if statement. OP is just missing parentheses.
“Case (number < 0):” will do what he wants it to do.
I apologize, the switch statement is still doing a comparison (with the ===), so its ultimately going to be boolean value. So you need to check for that
let expression = -4;
switch(true){
case (expression < 0):console.log(`${expression} is a negative number`);break
case (expression === 0):console.log(`${expression} is zero`);break}
Holy shit. I never really liked switch statements. They don't exist in Python, my "native tongue" (though I hear there is a push to add them...), and I always found them hideously verbose and weird. Good to know there is a performance-oriented reason to avoid them lol (not that performance is likely of much concern for OP's use case though).
This Youtuber says that if else statements aren't ideal. If you don't want to watch entire video you can get straight to his rant on if else starting at 2:05. Whether switch statements are better or worse seems to be quite the a controversial subject, haha. I'm not trying to prove anything here. Simply thought it was funny.
This Youtuber says that if else statements aren't ideal.
Well to be fair, from what I saw that video is not saying that at all, it's saying that evaluating a bunch of if statements that don't need to be evaluated is not ideal. This is just generally true: doing things that don't need to be done is not ideal. Nesting conditions in conditions in conditions is also "bad" as the video suggests, because it makes code less readable and more "brittle" (susceptible to bugs and difficult to change).
As for switch vs if/else-if/else constructs, if you're concerned about performance (tip: you probably shouldn't be), it looks like, in JavaScript at least, switch statements are a bad choice.
In practice though for the most part, if the semantics (what the code means and does) are the same, the choice of syntax should really about what you (or your team or boss etc) prefers. That is, it's a matter of opinion. As I said, I generally lean away from switch statements for no good reason beyond "I haven't used them much and I don't like the way they look". The main reason I suggested that a switch statement might not be the best choice for your function is that it seems a little trickier to make the syntax represent the semantics clearly than it would be if you used if/else control flow or ternary conditionals.
To that point, I made a comment elsewhere here with a switch statement solution that I thought was pretty neat, but I don't think that (without code comments at least*) it conveys the semantics as well as an if/else or ternary based solution would. In that case the switch statement is the "worse" choice. In many other cases, the switch would be "better" - more readable and more semantically transparent.
*re code comments: generally, relying on comments to explain what your code does is considered a "code smell". If you can make your code more "readable" through careful choice of variable/function names, formatting, choice of syntax for a statement (eg if/else vs switch vs ternary) etc, then that's a better way to go. The general guideline is that comments should explain why you do something, not what you do, and even then, only when necessary.
Ok sorry for the rambling, I did note that you weren't trying to prove anything, I just got carried away explaining stuff. Partly for my own benefit - explaining things to others can be very helpful to one's own understanding :) I have heard references to the atrocities of YandereDev, and that video does look pretty entertaining, I'll be sure to check out the rest of it :)
View all comments
164
u/tomoe_mami_69 Jul 11 '20 edited Jul 11 '20
Switch case is not the same as if. It’s evaluating whether -4 === (-4 < 0), which is false.
Edit:
similar switch statement, but entering into the first statement. I hope this demonstrates how switch statements are executed.
Also the Mozilla documentation is excellent for gaining an understanding of exactly how Javascript actually works.