C#: Why default fall-through in switch statement is not allowed?


I was working on this particular piece of logic which I saw as a multiple sieve like structure. An if-else statement which would check the value of a number and based on the value, it would also execute the code for all the values less than it.

For example, user could submit x number of projects to be processed in one request. User could add a minimum of 1 to a maximum of 5 projects in one request.

Based on the number of projects user added, I had to write a logic to check a certain value in each project form. So if user had added 3 projects, I would check project 1, 2 and 3.

if(totalProjects <= 5)
{
//some logic
}

if(totalProjects <= 4)
{
//some logic
}

if(totalProjects <= 3)
{
//some logic
}
.. 

I started off with writing a switch statement. I realized if I could revert the switch statement, starting from 5 and had no break statement then it would by default, go to the next case which would be 4. Based on my knowledge of switch statement from my C programming class in college, I knew this was possible. And this would work perfectly for me.

So I completed my switch case statement, and I compiled the code. And the compiler spat out an error on me.

Control cannot fall through from one case label ('case 5:') 
to another

I thought it might be a syntax error in code. So I checked the syntax again. No problem. So then I called on my best friend. “Google”. And finally came to a realization that in C# switch cases without break statement are not allowed. Further searching for a solution, I found that using goto statement instead of break would work. I made the changes, and the code compiled and I went ahead on further developing the logic.

Based on the above encounter, I realized 2 things. First, goto statement is not as evil as most programmers point out it to be. This is one scenario where you would use goto statement very effectively.

Second, why would .NET makers (Microsoft) not allow default fall-through? If switch is said to be as identical as an if-else statement, then there should not be any compulsion of adding a break statement. I thought about it a bit more and came to a conclusion that default fall-through should not be allowed.

Here are my reasons.

1. If switch is really identical to if-else, then it can never have default fall-through. Because in if-else statement after code in a particular ‘if’ clause is executed, then code execution would simply bypass all the other if-else and else statements in that . So in that regard, it is identical. Just as far the syntax goes, you should add break statement to denote the end of your case. It would have been go if it was simply down to adding { } inside a case and be done with it. But I think that is down to C and C++ rather then C#.

2. I had never encountered this error before. That means all the switch cases I have written so far have be the ones which did not require fall-through. I believe that would be the case with most of the developers around. So to avoid the unintended behavior and bugs, having default fall-through does not make sense.

So I am fairly convinced that default fall-through in switch statements is not a good idea.

Advertisements

About Rupesh

.NET and SharePoint 2010 developer
This entry was posted in Misc and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s