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 ThenDim statuscode As Integerstatuscode = Me.ddlStatus.SelectedItem.ValueSelect Case statuscodeCase 2' snip a few lines of codeMe.In_Preparation()Case 3Me.In_Fabrication()Case 5Me.Sent()Case 7Me.Rejected()Case 8Me.Cancel()Case 10Me.Waiting_for_payment()End SelectElseDim statuscode As Integerstatuscode = Me.ddlStatus.SelectedItem.ValueSelect Case statuscodeCase 2Me.In_Preparation()Case 3Me.In_Fabrication()Case 5Me.Sent()Case 7Me.Rejected()Case 8Me.Cancel()Case 10Me.Waiting_for_payment()End SelectEnd 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:
[Advertisement] Continuously monitor your servers for configuration changes, and report when there's configuration drift. Get started with Otter today!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.