CodeSOD: A Select Sample

"I work with very bad developers," writes Henry.

It's a pretty simple example of some bad code:

If Me.ddlStatus.SelectedItem.Value = 2 Then Dim statuscode As Integer statuscode = Me.ddlStatus.SelectedItem.Value Select Case statuscode Case 2 ' snip a few lines of code Me.In_Preparation() Case 3 Me.In_Fabrication() Case 5 Me.Sent() Case 7 Me.Rejected() Case 8 Me.Cancel() Case 10 Me.Waiting_for_payment() End Select Else Dim statuscode As Integer statuscode = Me.ddlStatus.SelectedItem.Value Select Case statuscode Case 2 Me.In_Preparation() Case 3 Me.In_Fabrication() Case 5 Me.Sent() Case 7 Me.Rejected() Case 8 Me.Cancel() Case 10 Me.Waiting_for_payment() End Select End If

This is a special twist on the "branching logic that doesn't actually branch", because each branch of the If contains a Select/switch statement. In this case, both the If and the Select check the same field- Me.ddlStatus.SelectedItem.Value. This means the first branch doesn't need a Select at all, as it could only possibly be 2. It also means, as written, the Else branch would never be 2. In the end, that's probably good, because as the comments provided by Henry point out- there's some elided code which means the branches don't do the same thing.

This has the sense of code that just evolved without anybody understanding what it was doing or why. Lines got written and copy/pasted and re-written until it worked, the developer committed the code, and nobody thought anything more about it, if they thought anything in the first place.

Henry adds:

It's only a simple example. Most of the codebase would make your head implode. Or explode, depending on a hidden parameter in the database.

[Advertisement] Continuously monitor your servers for configuration changes, and report when there's configuration drift. Get started with Otter today!

This post originally appeared on The Daily WTF.

Leave a Reply

Your email address will not be published.