“else if” – just in case … – and switches

Believe it or not, I have seen a code like this in one of the most reputable products:

if ( x == 0 ) {

… /* 50 lines of code */

} else if ( x == 1 ) {

… /* another two “page down” */

} …

When I asked the developer why he chose not to use switch cases instead, his answer was: “It has very unintutive syntax.”

What? Switches are perhaps the best control structures procedural programming could have. Just because most times you use “if”, you shouldn’t use them always!

Then there is OO crowd, which eliminates most switch structures.

Personally I favor “flat code” with minimal decisions – and that is why I love OO. Still I find there are cases where switches are necessary. I am going to talk about those conditions later. Currently let us focus on procedural programming.

The biggest advantage of switch is that it declares the focus of the code that follows. If I am not interested in the variable on which the code switches, I will simply skip it to the next matching paranthesis.

Yet another advantage of switch will be clear if we use our logical parallel notation |||. Assuming there is no “fall through”,
switch (x) {

case 0: foo[0]; ||| case 1: foo[1]; ||| …

}

Arranging the code this way will inspire you to think which code is common across the segments. You will be tempted to push that code out of the control structure and make code smaller and more maintainable.

[Please don’t assume people always reduce the code to logical minimal. I have actually converted experienced programmers’ code to switches and showed them the redundancy. When code becomes large, this is inevitable.]

Yet another interesting point is that once you put your code in ||| parallelism inside a switch, you often realize that you might not need a lot of logic at all.

Yet another advantage is that when you get a “null code fall through”, you are essentially coding for an “OR” condition. To “OR” challenged person like me, this is fantastic.

switch 5:

switch 6:

switch 7: foo();

I am sure that when x becomes any of 5, 6 or 7, the same code is going to be executed. That is graphically intutive. I don’t need to decipher this:

if ( x==5 || x==6 || x==7 ) foo();

Yet another advantage is also graphical. The “AND” condition is nothing but a switch within a switch. The code gets longer but the condition block becomes so small that it is very comprehensible.

Compare this:

if ( x==3 && y==4 && z==5) foo(); /* and fifteen such decisions in a tree */

with

switch (x) {

3: switch (y) {

4: switch (z) {

5: foo();

}

}

}

The code has gone “beyond horizon” but the comprehension has become idiot proof. The huge condition block has reduced to single condition at every step.

At last, the “NOT” is taken care. If a code has “not” logic, it will not exist in a block.

Still, switch structure in C are far from perfect. I will discuss that in a later posting.

Advertisements

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