Let\'s say I have code in C with approximately this structure:
switch (something)
{
case 0:
return \"blah\";
break;
case 1:
case 4:
Interesting. The consensus from most of these answers seems to be that the redundant break
statement is unnecessary clutter. On the other hand, I read the break
statement in a switch as the 'closing' of a case. case
blocks that don't end in a break
tend to jump out at me as potential fall though bugs.
I know that that's not how it is when there's a return
instead of a break
, but that's how my eyes 'read' the case blocks in a switch, so I personally would prefer that each case
be paired with a break
. But many compilers do complain about the break
after a return
being superfluous/unreachable, and apparently I seem to be in the minority anyway.
So get rid of the break
following a return
.
NB: all of this is ignoring whether violating the single entry/exit rule is a good idea or not. As far as that goes, I have an opinion that unfortunately changes depending on the circumstances...