break in a case with return.. and for default

前端 未结 13 2098
予麋鹿
予麋鹿 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 15:13

    Neither break does anything for you, but neither does harm.

    Personally, I usually leave them out if I have a return - but I also try to avoid having multiple return points in a function if possible.

    However, I do think the break in the default: case is good - for one reason: If you were to leave it out, and somebody added a new case after default:, the behavior would be different if they "forget" to add in a break.

提交回复
热议问题