I am a bit puzzled on whenever or not to include break
after the last case, often default
.
switch (type) {
case 'product':
// Do behavior
break;
default:
// Do default behavior
break; // Is it considered to be needed?
}
break
s sole purpose is in my understanding to stop the code from running through the rest of the switch
-case.
Is it then considered more logical to have a break
last due to consistency or skip having it due to the break
applying no functional use whatsoever? Both are logical in different ways in my opinion.
This could to a certain degree be compared with ending a .php
file with ?>
. I never end with ?>
mostly due to the risk of outputting blank spaces, but one could argue that it would be the logical thing to end the file with.
break
isn’t technically needed after the last alternative (which, mind you, doesn’t have to be default
: it is perfectly legal, and sometimes even useful to put the default
branch first); whether your code falls through the end of the switch
statement or breaks
out at the end of its last branch has the same result.
However, I’d still end every branch, including the last one, with a return
or break
statement, for three reasons:
- Refactorability. If all your branches end with
break
orreturn
, you can reorder them without changing the meaning. This makes it less likely for such a reordering to introduce a regression. - Consistency, and Least Surprise. Consistency says your branches should end consistently, unless they are actually different in meaning. The Principle of Least Surprise dictates that similar things should look similar. Ending the last branch of a
switch
block exactly like the preceding ones fulfills both, which makes for easier reading and understanding. If you leave out the explicitbreak
, the last branch will be optically different (which is especially important for quick scanning), and in order to see that it’s really not different, the reader has to descend to the nitty-gritty level of reading individual statements. - Protecting yourself. If you make a habit of ending all your
switch
branches with abreak
, it will become automatic after a while, and you’ll be less likely to accidentally forget it where it does matter. Training yourself to expect thebreak
at the end of every branch also helps detecting missingbreak
statements, which is great for debugging and troubleshooting.
6
Given the ambiguity that exists around the use of switch-case
in most languages, when using it I would suggest always using a break
statement, except when it is explicitly and by design not wanted.
Partly this is because is makes every case
call look the same, which in my opinion would improve readability. But it also means if someone (even you) chooses to insert a case
after the last one at a later stage, they don’t need to concern themselves with checking the prior block, which could help reduce bugs when adding new code.
3
There is no break
necessary after the last case. I use the word “last” (not default )because it is not necessary default case is the last case.
switch(x)
{
case 1:
//do stuff
break;
default:
//do default work
break;
case 3:
//do stuff
}
And we know, a break
is necessary between two consecutive case
s. Sometimes, I use if(a!=0)
in my code for more readability when others refer my code. I can choose to use if(a)
, that would be a matter of my choice
6