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
-
devios157017y@bkwilliams It actually can't be; I omitted it for brevity but it's actually doing a !! on the parameter, meaning it's always going to be either true or false. :P
-
Then it might be a hardware thing. The true block of an “if” is put into the CPU pipeline to be worked before the result of the “if” condition is known. When the condition is false, whatever is in the pipeline is thrown away and the false block is loaded. “Switch/Case” does not follow that convention.
But that was a while ago and hardware may have changed. -
devios157017y@bkwilliams Doesn't that just apply to compiled code? The only branch prediction that would be happening would be in the js interpreter, wouldn't it?
Then again I don't know what kind of optimizations the interpreter does behind the scenes. It could actually JIT compile it for all I know.
Regardless, judging by the numerous other atrocities in this code I assure you the reasons for doing this are not optimization. :P -
devios157017y@valvy It's bad for a number of reasons. Logically it's fine, we all know it'll execute fine, but the problem is in communication of intent, readability, chance for mistakes, etc.
Logically this is a simple boolean condition. As I mentioned above, though I omitted it from the snippet, boolean is actually boolean: it's either true or false.
I also didn't mention that the cut out code is actually almost 200 lines of code. So this isn't even concisely viewable in a single page of code. It's two giant case statements inside what could have been a simple if/else. -
Just in case Boolean gets more values, always think about the future...always! 😛
Related Rants
I just encountered this:
switch (boolean) {
case true:
…
break;
case false:
…
break;
}
undefined
js
you forgot the default
i prefer more lines
smh
why??