break in a case with return.. and for default

前端 未结 13 1988
予麋鹿
予麋鹿 2020-12-11 14:19

My OCD makes me add \"break\" when writing case statements, even if they will not be executed. Consider the following code example:

switch(option) {
    cas         


        
相关标签:
13条回答
  • 2020-12-11 14:59

    Please excuse my limited knowledge, but what's OCD?
    Apart from that, Brian Kernighan provides a good explanation on when you should (not) use break within a switch statement.

    0 讨论(0)
  • 2020-12-11 15:03

    I'm told that in C, C++, java and C#, if you don't put those "breaks" the program code flow will fall into the other "cases" and will execute the instructions inside them, not matter if the variable doesn't have the values assignned to the "cases".

    0 讨论(0)
  • 2020-12-11 15:07

    I prefer always have a break in each case including the default and avoid doing return at all inside switch's. For short switches with just 2-3 cases(including default) return is ok but only if all cases do it the same way. 'pointless' break i see as pointless and only make's it more code to read. Same goes for empty defaults that just do break, totally pointless. The ease to read the code is in my opinion more important that what happens if someone happens to change this or that.

    0 讨论(0)
  • 2020-12-11 15:08

    As others have pointed out, placing a break after a return or in the default case is mostly a matter of personal style.

    When I don't have to follow any specific style rules, I prefer something like this:

        switch(foo){
             case 0:
                 baz = 1;
                 break;
             case 1:
                 bar %= 2;
                 return -1;
                 /* NOTREACHED */
             case 2:
                 bar = -1;
                 return -2;
                 /* NOTREACHED */
                 break;
             default:
                 break;
        }
    

    Between cases 1 and 2, I tend to prefer 2. Even though the comment says NOTREACHED, comments can lie ( unintentionally of course ) when the code changes. I like the NOTREACHED comment since it can satisfy lint that you know what you are doing and serves as a reminder that you exiting the function early. The reasoning that placing a break after the return will mitigate errors if the return is deleted seem flawed to me. You are still going to get bogus behavior regardless if you fall through to the next case or you exit the switch and continue on as before.

    Of course, if I can avoid it I would not return from a function within the body of a switch.

    0 讨论(0)
  • 2020-12-11 15:09

    You don't need either break, but there's no harm in having them. In my opinion, keeping your code structured is worth having a couple of extraneous statements.

    0 讨论(0)
  • 2020-12-11 15:09

    I agree with having a break in a final default case, and don't agree with breaks after returns. (A colleague does those and it hurts my eyes.)

    I also indent switches so as to reduce proliferation of indent levels. :) i.e.:

    switch(option) {
    case 1:
        a = 1;
        b = 7;
        break;
    case 2:
        a = 2;
        b = 4;
        return -1;
    default:
        a = -1;
        break;
    }
    

    (I also think that, since the return statement is not a function, it isn't appropriate to enforce a superfluous style that makes it look like one.)

    0 讨论(0)
提交回复
热议问题