Dealing with combining cases & duplicate cases in switch statements

前端 未结 4 605
猫巷女王i
猫巷女王i 2021-01-15 14:28

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

4条回答
  •  再見小時候
    2021-01-15 14:54

    If you run static analysis tool like Coverity it report minor error if you combine 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;
    

提交回复
热议问题