The Tyranny of ElseIf

Why in the world would you express logic this way, instead of the much simpler SELECT CASE statement?


This is a companion discussion topic for the original blog entry at: http://www.codinghorror.com/blog/2004/06/the-tyranny-of-elseif.html

I agree. And from my experience, VB seems to encourage deviant logic flow. I’ve run across

If Not Foo Then
Else
Console.WriteLine(“Baz”)
End If

as well as things like

If Foo IsNot Nothing Then
If Foo.Length 6 Then
Console.WriteLine(“Foo must be at least six digits!”)
End If
End If

I assume the second case is due to non-shortcircuiting boolean operator evaluation (OrElse/AndAlso don’t seem to be very popular), but I have no idea what could have brought about the first. Really sloppy refactoring, perhaps?

Okay, but the code is just going to the next Sunday if today is not already Sunday. Given that, why would you use if-then-else or select-case? You can use a simple aritmetic operation: addition.

return dt.AddDays((7 - (int) x.Date.DayOfWeek) % 7)

Yeah, it relies on Sunday having value 0. We can change that for other values of Sunday.

Im no .net programmer but I have to agree. My primary language is PHP and I see it all to often. especaily the

If Foo IsNot Nothing Then
If Foo.Length 6 Then
Console.WriteLine(“Foo must be at least six digits!”)
End If
End If

when it is much simpler to express it as

if(count($foo) ‘6’)
{
echo “Foo must be at least six digits!”;
}

at least in PHP.

I was just thinking about which one to use today when I remembered I needed ranges, i.e.:

if (x 100) …
else if (x 200) …
else …

That, and the really short (2-3 cases, perhaps even 4) are the only times when using a strand of if’s can be justified. (And when switch…case is broken like with AS3.0’s XML objects)

Another evilness of the elseif keyword is that it makes it harder to realize that ‘if…elseif…elseif’ constructs are equivalent to a set of nested if’s, i.e. ‘if…else if…else if’.

Don’t forget as well that “select case” is easier for the compiler to refactor into jumps based on a fast to calculate value, rather than forcing a load of comparisons like if statements might. This leads to faster code in some cases… of course a large enough select/case will become ifs and elseifs anyway once the compiler has it in its hands, but there is a middle ground too…

Short story, select/case is good for your compiler, if/elseif is not.

Or you can make it look even prettier using the old school colon.

Select Case dt.DayOfWeek
Case DayOfWeek.Sunday: Return dt
Case DayOfWeek.Monday: Return dt.AddDays(6)
Case DayOfWeek.Tuesday: Return dt.AddDays(5)
Case DayOfWeek.Wednesday: Return dt.AddDays(4)
Case DayOfWeek.Thursday: Return dt.AddDays(3)
Case DayOfWeek.Friday: Return dt.AddDays(2)
Case DayOfWeek.Saturday: Return dt.AddDays(1)
End Select

That ElseIf is bad, but I’ve seen worse! I inherited some code that was like this:

If dt.DayOfWeek = DayOfWeek.Sunday Then
Return dt
Else
If dt.DayOfWeek = DayOfWeek.Monday Then
Return dt.AddDays(6)
Else
If dt.DayOfWeek = DayOfWeek.Tuesday Then
Return dt.AddDays(5)
Else
If dt.DayOfWeek = DayOfWeek.Wednesday Then
Return dt.AddDays(4)
Else
If dt.DayOfWeek = DayOfWeek.Thursday Then
Return dt.AddDays(3)
Else
If dt.DayOfWeek = DayOfWeek.Friday Then
Return dt.AddDays(2)
Else
If dt.DayOfWeek = DayOfWeek.Saturday Then
Return dt.AddDays(1)
End If
End If
End If
End If
End If
End If
End If

I must admit, I have used

If Not Foo Then
Else
do something
End If

When working with Boolean values(null, 0, 1)

if not x then will only execute for 0
do something

if x then
else
do something will execute for 0 and null

I realize that i could just AND/OR for the null condition, but four letters seems simpler. I have no problem glancing at that statement and knowing what it is doing…but then again, i work with databases, and am all the time branching on null/0/1 conditions.

I have used the
if (not condition) then
expression
else
expression
in a few cases, actually in a lot of cases. This was in cases when it seemed logical to follow the THEN branch in most cases and the ELSE was only for a few exceptions. It makes perfect sense when it’s easier to evaluate something as being wrong than being right (if you you have only conditions that tell you a value is wrong, instead of having conditions that tell you it’s right; for example, a basic word filter). So if the value is NOT WRONG (like DOH, IT’S RIGHT), you do what your normally do. If you have an exception from that rule, you treat it in the ELSE branch.
Saying that you should NEVER EVER USE IF NOT X THEN … ELSE … constructs is just plain stupid, because code redesign just to fit the IF X … ELSE … pattern might add some complexity to the code.

There are a few cases where it makes more sense to do it this way.

Don’t forget as well that select case is easier for the compiler to refactor into jumps based on a fast to calculate value, rather than forcing a load of comparisons like if statements might. This leads to faster code in some cases… of course a large enough select/case will become ifs and elseifs anyway once the compiler has it in its hands, but there is a middle ground too…
http://tour-elite.ru

Did you guys know that the code:

If Foo IsNot Nothing Then
If Foo.Length 6 Then
Console.WriteLine(“Foo must be at least six digits!”)
End If
End If

is necessary because you can’t test the length of a null object? If you try, you’ll get an exception, so first you test that object reference is not null (or nothing in VB).

As far as the original if … elseif is concerned, it can be a common structure in C. The switch statement in C only accepts a literal value, so you can’t test strings ( strcmp(var, “dafdf”) == 0 ) in a switch statement and you have to resort to if … elseif. In C its not so bad as long as the whole structure is laid out cleanly.

I’m guessing that some programmers have stepped out of the C world into VB and taken some of the C habits with them!

Either that or they are not aware the select case even exists, because they are expecting to use the “switch” or “case” keywords - the keywords “Select Case” are unsual and particular to VB.

Wow, commenting on a 6-year old post?

Sure, why not.

In many programming languages switch is limited to 256 options - at least it was when I was working in VS98.

Also, many times people use String.Compare’s when they should use Enums and that lends itself to an elseif format.

-S!