最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Dealing with combining cases & duplicate cases in switch statements - Stack Overflow

programmeradmin3浏览0评论

Is it okay to bine cases that share assignments and repeat the case for assignments that are not shared, or is it preferred to just keep each separated?


To illustrate with a simple example.. case 0 and 180 both include w = 330 so they have been bined; the value assigned to x is different for each so they are repeated to do the x assignment.

switch(window.orientation) {
    case   0:
    case 180:
        w = 330;
        //break
    case   0:
        x = '-180px';
        //break
    case -90:
    case  90:
        w = 480;
        x = '0';
        break;
    case 180:
        x = '-80px';
        break;
}

Is it okay to bine cases that share assignments and repeat the case for assignments that are not shared, or is it preferred to just keep each separated?


To illustrate with a simple example.. case 0 and 180 both include w = 330 so they have been bined; the value assigned to x is different for each so they are repeated to do the x assignment.

switch(window.orientation) {
    case   0:
    case 180:
        w = 330;
        //break
    case   0:
        x = '-180px';
        //break
    case -90:
    case  90:
        w = 480;
        x = '0';
        break;
    case 180:
        x = '-80px';
        break;
}
Share Improve this question edited Jul 15, 2015 at 5:16 davidcondrey asked Sep 24, 2014 at 21:03 davidcondreydavidcondrey 36.1k18 gold badges119 silver badges138 bronze badges 1
  • I wouldn't bine since case 0 and case 180 have different x values (might possibly if the branches were identical). I would avoid bining cases due to difficult readability/later maintainability alone. – Will Commented Sep 24, 2014 at 21:10
Add a ment  | 

4 Answers 4

Reset to default 6

When it es to do more than one operation per element in a switch statement, it's always better to not repeat the same case twice. You can easily achieve this by summing up all the fragments of code that are under the same case.

For example, if you want to perform operation A on case 0 and operation B on case 0 and case 1 then you should do something like this:

switch(variable) {
    case 0:
        // operation A;
    case 1:
        // operation B;
        break;
}

This will execute both operation A and B on case 0, because there's no break on case 0.

Now let's assume you write something like this:

switch(variable) {
    case 1:
        x = 1;
        break;
    case 1:
        x = 2;
        break;
}

The above code will end up assigning the value 1 to the variable x. The second case 1, saying x = 2 will never be reached, because of the break statement in the first case 1.

So if you have got to perform different operations on case 0 and case 1, but they share some operation, that's better to separate the cases repeating some lines of code instead of writing case 1 twice, because this makes your code easier to read and slightly faster.

So in your code, the best way to achieve what you want is this one:

switch(window.orientation) {
    case   0:
        x = '-180px';
        w = 330;
        break;
    case 180:
        x = '-80px';
        w = 330;
        break;
    case -90:
    case  90:
        w = 480;
        x = '0';
        break;
}

I would say don't bine - too high of a chance of making a mistake...like you did. The 2nd case 180 will never be reached, since the first one has a break; after it.

If everything is the same, I think it's ok to bine though (like the -90, 90).

If you run static analysis tool like Coverity it report minor error if you bine multiple switch cases like below.

   case -90:
   // Intentionally fall through
   case  90:
        w = 480;
        x = '0';
        break;

Better to repeat some lines of code than to make some undesired error.

       case -90:
            w = 480;
            x = '0';
            break;
       case  90:
            w = 480;
            x = '0';
            break;

It can't work. As soon as a break instruction is executed, the remaining code in the switch block is not executed. You could fix it like this:

switch(window.orientation) {
    case   0:
    case 180:
        w = 330;
    case   0:
        x = '-180px';
        break;
    case 180:
        x = '-80px';
        break;
    case -90:
    case  90:
        w = 480;
        x = '0';
        break;
    }
发布评论

评论列表(0)

  1. 暂无评论