Coincidentally, a friend of mine told me this week his switch(true) did not pass his code review, despite the arguments of it looking much clearer. Some people will not accept this, so beware 😛
I've been a professional C, Perl, PHP and Python developer.
I'm an ex-sysadmin from the late 20th century.
These days I do more Javascript and CSS and whatnot, and promote UX and accessibility.
I've been a professional C, Perl, PHP and Python developer.
I'm an ex-sysadmin from the late 20th century.
These days I do more Javascript and CSS and whatnot, and promote UX and accessibility.
Would like to understand the reason for not allowing switch (if you can share). Because. even I have started using switch over if-else in many cases. It just make more sense to me.
I think it is because it takes an extra thinking step.
if/else-if is more easily read as a sentence. But please let me know if there are other reasons.
Just a bit of a guess here, but I think the switch statement is considered a last resort when you have a ton of conditionals that can't be done any other way.
The switch statement may be like a logic shortcut when there may be another solution out there. Sometimes if/else is sufficient and reads easier when you really don't have that many statements. Sometimes a quick if like if (!someExpressionA) { return console.log('yes') } may be a less intensive solution. And some people just don't like using conditionals at all and prefer to use objects whenever they can.
I kinda use all four solutions depending on the situation, so maybe @dantederuwe
's friend didn't pass because there was a better solution. Just a total guess of course!
Coincidentally, a friend of mine told me this week his
switch(true)
did not pass his code review, despite the arguments of it looking much clearer. Some people will not accept this, so beware 😛Because anyone who isn't used to seeing it will think it's a typo and then have to rethink what the function is doing.
But what if it is more common. At some point you would expect a turning point if there are no other downsides ofcourse.
Generally I think people are trying to move away from functions that have a lot of conditionals, so it's probably not going to become more common.
good point, not a fan of them myself 😉
Would like to understand the reason for not allowing
switch
(if you can share). Because. even I have started using switch over if-else in many cases. It just make more sense to me.I think it is because it takes an extra thinking step.
if/else-if is more easily read as a sentence. But please let me know if there are other reasons.
Just a bit of a guess here, but I think the switch statement is considered a last resort when you have a ton of conditionals that can't be done any other way.
The switch statement may be like a logic shortcut when there may be another solution out there. Sometimes if/else is sufficient and reads easier when you really don't have that many statements. Sometimes a quick if like
if (!someExpressionA) { return console.log('yes') }
may be a less intensive solution. And some people just don't like using conditionals at all and prefer to use objects whenever they can.I kinda use all four solutions depending on the situation, so maybe @dantederuwe 's friend didn't pass because there was a better solution. Just a total guess of course!
hence my question, should I use it 😁