Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
Coming from a working environment with plenty of on-the-run modifications and requests if there is even a tiny chance it will recieve more cases then switch it is. Easier to add/remove.
-
@andrasadam93 totally agree. Like for example, for an enum I would almost always use a switch in case the enum grows. Switch has also (in some languages) the benefit of warning you if you forgot some of the enum values! So when you add more values, warnings will pop up, which is great.
-
Pauly-Rey408yI'll tell you what's worse, I found in some live code
Switch (var)
{
Case default:
<code>
Break;
} -
@Bramskyyy It's multiple if statements broken down into cases where the selected case is determined by the value of a switch variable.
So it's multiple ifs without all the hassle. -
@Bramskyyy
switch(e){
case 5:
console.log ("e is 5");
break;
default:
console.log ("e is not 5");
break;
} -
@wbhob I though he was joking, but I provided an explanation anyway because I wasn't sure.
No I see that all of us tought so. -
Btw, a switch IS NOT equivalent to multiple IF statements. The compiled binary is much more efficient most of the times, like in the case of an enum. If you have N values, with ifs you will have to make N comparisons and N conditional jumps. With a switch you do one jump!
-
@Joserc87 Yeah, but I didn't over analize, nor did I say that they were equivalent.
When someone uses a *switch* statement with only 2 cases
undefined