I have taken advantage of fallthoughs in switch
statements maybe twice. And that's a hard maybe. I can only really remember one instance where I did not break
after every case
block. For most of us, a switch statement will be mostly used as a super compact if...else
statement like the following:
function getSeasonFromMonth(month)
{
let season = null;
switch (month)
{
case December:
case January:
case February: {
season = Winter;
break;
}
case March:
case April:
case May: {
season = Spring;
break;
}
case June:
case July:
case August: {
season = Summer;
break;
}
case September:
case October:
case November: {
season = Autumn;
break;
}
}
return season;
}
While you would probably use return
in this case, I'm not to get the point across. What if, if you put a break
in front of the switch
then it automatically break
d after every block? So you could rewrite the above as:
function getSeasonFromMonth(month)
{
let season = null;
switch (month)
{
case 'December':
case 'January':
case 'February': {
season = 'Winter';
}
case 'March':
case 'April':
case 'May': {
season = 'Spring';
}
case 'June':
case 'July':
case 'August': {
season = 'Summer';
}
case 'September':
case 'October':
case 'November': {
season = 'Autumn';
}
}
return season;
}
Clean code doesn't repeat itself and I can only imagine the nightmares caused from the side-effects of a forgotten break
.
~So give us a break
TC39 and put this in!~