Forum Moderators: open

Message Too Old, No Replies

switch/case, is default really necessary?

         

csdude55

6:02 pm on Aug 23, 2023 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



When using switch/case, I think of "default" the same as I'd think of an "else" in an if/else statement: I only use it if it's necessary.

But while optimizing my code, I see that ES Lint throws a warning on switch/case when I don't have a default.

What do you think, should I ignore the warning? Or is there an advantage to throw in an empty default?

phranque

8:24 pm on Aug 23, 2023 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



it's a warning.
if the warning is of your intention you may ignore it.
think of it as a reminder in case you forgot the default.

csdude55

11:40 pm on Aug 23, 2023 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I know that, historically, sometimes those warnings become errors :-O And other times they make things run slower than necessary. So I guess that I really just wanted to make sure that this was one I could ignore.

Either way, I think that I'm going to ignore it. The juice isn't worth the squeeze, ya know? LOL

phranque

7:17 am on Aug 24, 2023 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



The juice isn't worth the squeeze

it really depends on whether your ultimate objective is to minimize errors and warning messages, or to minimize code, or to optimize code, etc

csdude55

5:29 pm on Aug 24, 2023 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The ultimate objective is to increase pages per session, and the means to that end right now is to make the page faster for the end user. Everyone says that differences are negligible and not worth worrying about, but shaving microseconds here and there has added up!

My rule of thumb is to minimize code, which decreases the size of the page being loaded and results in a faster load time. But of course that doesn't always mean that the page is faster; eg, switch/case is faster to process than if/elseif/else, regardless of the size. So if throwing this warning adds more time to the process than adding the word "default:" would increase the load time then I'd go through and add it in.

phranque

7:34 pm on Aug 24, 2023 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



in that case the ultimate objective is code optimization and your answer lies in your test results.

csdude55

3:45 am on Aug 25, 2023 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I haven't found a good way to bench test Javascript, though. I can test server-side programming easily enough, but client-side has too many variables that I can't control. I've mostly just been trusting what the docs say.

phranque

6:19 am on Aug 25, 2023 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



oh yes, i forgot this is specifically a JS question.
you're probably balancing code bloat (download speed) against the cost of logging the warning.
i'm sure code execution is essentially equal otherwise.

lucy24

5:35 pm on Aug 25, 2023 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Then again, it can't possibly add many nanoseconds if you wind up every function with
 default:
break;

Some day you may get a visitor whose browser (the Ghost of MSIE Past, let's say) falls into a coma if it runs through the whole switch/case and never finds an applicable case.

csdude55

6:09 pm on Aug 31, 2023 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Sorry for the late reply, for some reason I haven't been getting emails from WebmasterWorld when someone replies :-/

Technically speaking, if default is last then I wouldn't even need the break, would I?

switch (foo) {
case 'bar':
// do something
break;

case 'whatever':
// do something else
break;

default:
}


That doesn't throw an error, anyway.