The Tyranny of ElseIf

I don't understand it. I've seen this phenomenon over and over in VB.NET, in code from experienced programmers:

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

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

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

I mention this because I see it constantly from many different programmers. What gives?

I think the ELSEIF keyword is destructive and, like GOTO, has no good use outside of a very specialized niche. ELSEIF, in my experience, is abused far more than it is used properly. Here's another example from a project I work on:

If result = DialogResult.Retry Then
strProjectName = .ProjDetailsPnl.PnlGenInfo.TxtProjectName.Text
ElseIf result = DialogResult.OK Then
intNewProjectID = .ProjectID
End If

I have a very hard time coming up with any valid justification for the use of ELSEIF. 98 times out of 100, SELECT CASE is easier to read and offers the CASE ELSE condition, which encourages developers to handle unknown conditions properly. In the above example, what happens when the dialog returns DialogResult.Cancel?

Related posts

I Fight For The Users

I Fight For The Users

If you haven’t been able to keep up with my blistering pace of one blog post per year, I don’t blame you. There’s a lot going on right now. It’s a busy time. But let’s pause and take a moment to celebrate that Elon Musk

By Jeff Atwood ·
Comments
Updating The Single Most Influential Book of the BASIC Era

Updating The Single Most Influential Book of the BASIC Era

In a way, these two books are responsible for my entire professional career. With early computers, you didn’t boot up to a fancy schmancy desktop, or a screen full of apps you could easily poke and prod with your finger. No, those computers booted up to the command line.

By Jeff Atwood ·
Comments
To Serve Man, with Software

To Serve Man, with Software

I didn’t choose to be a programmer. Somehow, it seemed, the computers chose me. For a long time, that was fine, that was enough; that was all I needed. But along the way I never felt that being a programmer was this unambiguously great-for-everyone career field with zero downsides.

By Jeff Atwood ·
Comments
The Raspberry Pi Has Revolutionized Emulation

The Raspberry Pi Has Revolutionized Emulation

very geek goes through a phase where they discover emulation. It’s practically a rite of passage. I think I spent most of my childhood – and a large part of my life as a young adult – desperately wishing I was in a video game arcade. When I finally obtained my

By Jeff Atwood ·
Comments

Recent Posts

Stay Gold, America

Stay Gold, America

We are at an unprecedented point in American history, and I'm concerned we may lose sight of the American Dream.

By Jeff Atwood ·
Comments
The Great Filter Comes For Us All

The Great Filter Comes For Us All

With a 13 billion year head start on evolution, why haven’t any other forms of life in the universe contacted us by now? (Arrival is a fantastic movie. Watch it, but don’t stop there – read the Story of Your Life novella it was based on for so much

By Jeff Atwood ·
Comments
I Fight For The Users

I Fight For The Users

If you haven’t been able to keep up with my blistering pace of one blog post per year, I don’t blame you. There’s a lot going on right now. It’s a busy time. But let’s pause and take a moment to celebrate that Elon Musk

By Jeff Atwood ·
Comments
The 2030 Self-Driving Car Bet

The 2030 Self-Driving Car Bet

It’s my honor to announce that John Carmack and I have initiated a friendly bet of $10,000* to the 501(c)(3) charity of the winner’s choice: By January 1st, 2030, completely autonomous self-driving cars meeting SAE J3016 level 5 will be commercially available for passenger use

By Jeff Atwood ·
Comments