break in a case with return.. and for default

2019-01-20 06:58发布

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

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

My two questions are:
For "case 2:", I don't really need the break, but is it a good idea to have it there anyway? For "default:". Is it purely OCD, or is there any real reason to have the break here?

13条回答
Root(大扎)
2楼-- · 2019-01-20 07:59

The break after your default case is just a matter of personal preference.

Putting a break after return almost seems contradictory to me. I'd remove the break, just to make the return statement really stand out.

查看更多
登录 后发表回答