switch statement
-
I was cleaning up the code in a parser. There is a switch/case statement that spans over 2,200 lines. I removed a bunch of exits from various case statements and moved it up as a global test on the errorCode. It just didn't want to exit when the errorCode was set. :-O
state=1;
do
{
result = token( tokstr, FALSE, NULL, FALSE);
switch(state)
{
if (errorCode != 0)
return FALSE;
case 1:
switch(result)
{
// 2000+ lines snipped
} /* switch(state) */} while( result != Keyword::EOFTOK ); /* do */
“Although distant in time, Homobonus does in fact figure as a saint for the Church and society of our time.” -- Pope John Paul II
-
I was cleaning up the code in a parser. There is a switch/case statement that spans over 2,200 lines. I removed a bunch of exits from various case statements and moved it up as a global test on the errorCode. It just didn't want to exit when the errorCode was set. :-O
state=1;
do
{
result = token( tokstr, FALSE, NULL, FALSE);
switch(state)
{
if (errorCode != 0)
return FALSE;
case 1:
switch(result)
{
// 2000+ lines snipped
} /* switch(state) */} while( result != Keyword::EOFTOK ); /* do */
“Although distant in time, Homobonus does in fact figure as a saint for the Church and society of our time.” -- Pope John Paul II
2200 lines of cases? Impressive, at least to me, humble employee of small companies.
-
2200 lines of cases? Impressive, at least to me, humble employee of small companies.
Oshtri Deka wrote:
2200 lines of cases? Impressive, at least to me, humble employee of small companies
I'm not happy with 2200 lines of cases. I'm seeking to clean this up and reduce complexity. Things tend to just keeping growing. I'm curious, do you see the problem with my posted code?
-
I was cleaning up the code in a parser. There is a switch/case statement that spans over 2,200 lines. I removed a bunch of exits from various case statements and moved it up as a global test on the errorCode. It just didn't want to exit when the errorCode was set. :-O
state=1;
do
{
result = token( tokstr, FALSE, NULL, FALSE);
switch(state)
{
if (errorCode != 0)
return FALSE;
case 1:
switch(result)
{
// 2000+ lines snipped
} /* switch(state) */} while( result != Keyword::EOFTOK ); /* do */
“Although distant in time, Homobonus does in fact figure as a saint for the Church and society of our time.” -- Pope John Paul II
switch(state) { if (errorCode != 0) return FALSE; case 1: switch(result) {
My guess is that execution jumps to one of case labels and skip the if statement entirely?
[My Blog]
"Visual studio desperately needs some performance improvements. It is sometimes almost as slow as eclipse." - Rüdiger Klaehn
"Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe -
Oshtri Deka wrote:
2200 lines of cases? Impressive, at least to me, humble employee of small companies
I'm not happy with 2200 lines of cases. I'm seeking to clean this up and reduce complexity. Things tend to just keeping growing. I'm curious, do you see the problem with my posted code?
I was sarcastic, though I am impressed that someone wrote so many lines of code for one switch-case block. What does that application do? checking before first case?
-
switch(state) { if (errorCode != 0) return FALSE; case 1: switch(result) {
My guess is that execution jumps to one of case labels and skip the if statement entirely?
[My Blog]
"Visual studio desperately needs some performance improvements. It is sometimes almost as slow as eclipse." - Rüdiger Klaehn
"Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe -
I was cleaning up the code in a parser. There is a switch/case statement that spans over 2,200 lines. I removed a bunch of exits from various case statements and moved it up as a global test on the errorCode. It just didn't want to exit when the errorCode was set. :-O
state=1;
do
{
result = token( tokstr, FALSE, NULL, FALSE);
switch(state)
{
if (errorCode != 0)
return FALSE;
case 1:
switch(result)
{
// 2000+ lines snipped
} /* switch(state) */} while( result != Keyword::EOFTOK ); /* do */
“Although distant in time, Homobonus does in fact figure as a saint for the Church and society of our time.” -- Pope John Paul II
-
I was sarcastic, though I am impressed that someone wrote so many lines of code for one switch-case block. What does that application do? checking before first case?
Oshtri Deka wrote:
I was sarcastic, though I am impressed that someone wrote so many lines of code for one switch-case block. What does that application do?
That module has to do with parsing a parameter file. It probably was state-of-the-art in 1983. It has just grown and grown. We need an option at to handle FOOBAR? No problem, add a #DEFINE FOOBAR 1742 and one more case statement. Continue the process for almost a quarter century. The application classifies inpatient hospital records into disease categories and stages of the disease. The portion of the program that does that has been modernized.
checking before first case?
Yeah, that code was unreachable. I checked it into source control. It worked great if there were no errors. :sigh:
-
bsaksida wrote:
My compiler woud throw exception
Are you sure? I think it's legal C++ albeit stupid programming. I wish it had thrown an exception. But Visual Studio was happy with it, as were the compliers on AIX, Solaris, and HP/UX.
-
why is there a switch inside switch This style of progrmaing introduce more compiling errors than the bugs
bsaksida wrote:
why is there a switch inside switch
Well, it fits the logic. When the parser is starting a statement, the state is 1. Then it does a switch statement based on the first token. The code all works, I'm just not happy with how complicated it is. I used SourceMonitor[^] to get a metric on the complexity. Mike Swanson[^] suggests that Anything with a greater complexity than 10 or so is an excellent candidate for simplification and refactoring. Well, the code in question has a complexity number of 638 as measured by SourceMonitor. You don't go from 638 to 10 in one step. So one of the things I did was to have a single exit with the error code at the top of the case statement. Or at least, I tried to. My logic error was a little embarrassing. But an error like that is too good to keep to myself. I had to share it.
-
Oshtri Deka wrote:
2200 lines of cases? Impressive, at least to me, humble employee of small companies
I'm not happy with 2200 lines of cases. I'm seeking to clean this up and reduce complexity. Things tend to just keeping growing. I'm curious, do you see the problem with my posted code?
You should construct a data structure and keep the possible values in case statements in an ordered way in your data structure; then your incoming value should be compared with the values inside your data structure in binary search fashion... this will be faster and save you from time consuming sequential searching
-
You should construct a data structure and keep the possible values in case statements in an ordered way in your data structure; then your incoming value should be compared with the values inside your data structure in binary search fashion... this will be faster and save you from time consuming sequential searching
xenonysf wrote:
You should construct a data structure and keep the possible values in case statements in an ordered way in your data structure; then your incoming value should be compared with the values inside your data structure in binary search fashion... this will be faster and save you from time consuming sequential searchin
I agree with what you say but not for the reasons you give. A switch/case statement has better performance than a sequence of if/else statements. The compiler may generate a jump table. For large tables, some use binary searches. See http://www.eventhelix.com/RealtimeMantra/Basics/CToAssemblyTranslation3.htm[^]. So performance isn't the driving factor here. But I do agree that the cases -- where possible -- should be encapsulated in a data structure. We would like to have actions for the various cases in a data structure to simply the logic. Complexity leads to increased probability of error. We have unneccessary complexity. However, it takes a while to untangle code that has grown for decades.