best practice with javascript switch statement
Asked Answered
B

3

14

I'm currently working on a jquery script using the switch statement and I was wondering what was the best solution when several 'cases' share some properties, let's say I have this pattern:

switch (settings.xxx) {
case 'case1':
    Execute some code
    break;
case 'case2':
    Execute some code
    break;
case 'case3':
    Execute some code
    break;
}

For each case, I have quite a lot of code that's partly repeated because some properties are common to the 3 cases. So my question is, can I do the same with :

switch (settings.xxx) {

case 'case1':
case 'case2':
case 'case3':
    Execute some code
    break;

}

switch (settings.xxx) {

case 'case1':
case 'case2':
    Execute some code
    break;
case 'case2':
case 'case3':
    Execute some code
    break;

}

Or is it a bad practice?

Bitterling answered 26/11, 2012 at 17:23 Comment(4)
Is the "some code" the same for all three cases?Worthy
The second one does not work as you seem to expect. In case 2, only the first code is executed, then you break out of the switch statement. The fallthrough does only work for case 1.Carousel
If you're forced to write like this, maybe a switch statement is not the way to go.Throe
Just to clarify, the "some code" means that those parts share a few lines. Let's say case1 and case2 share a portion of 2-3 lines and case2 with case3 share another different 2-3 lines. It's a bit theorical but I'd like to avoid posting full chunks of code that would probably be unnecessary...Bitterling
A
15

No, it's not bad practice. Actually, it is good practice. This way, you would not repeat yourself.

Also, take a look at the documentation: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Statements/switch

Angulate answered 26/11, 2012 at 17:25 Comment(0)
S
8

The best practice is to avoid repeating code. That way if you later need to modify the code, you don't need to remember to do it in multiple places.

In the situation where some of the code only applies to some cases, you can leave out a break, and the execution will fall through to the next section after executing the first section. This only applies when the special case code can be executed before the general case code. Be careful when you do this, and add comments clearly stating that you are doing it intentionally. Otherwise, if the code is later edited, someone else might not realise that it is intentional, and add the break in where it is missing. See below:

switch (settings.xxx) {
case 'case1':
case 'case2':
    Execute some code that applies to cases 1 & 2
    // break left out to allow execution to fall through
case 'case3':
    Execute some code that applies to cases 1 - 3
    break;
case 'case4':
    Execute different code
    break;
}
Skye answered 7/6, 2013 at 14:35 Comment(0)
C
0

you can do this also...

switch(settings.xxx){

case 'case1','case2':same work;
break;

case 'case3':different work;
break;

default:some task;
}
Canaan answered 17/6 at 6:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.