An email from Andrea Ci arrived in our inbox, with nothing more than some code and a very simple subject line: “VB Conversion: a year in hell”.
A lot of people have that experience when encountering Visual Basic code, especially when it’s VB6, not VB.Net. Even so, could it really be that bad? Well, let’s look at the sample Andrea provided.
Public Sub sort(maxItems As Integer)
If m_Col.Count > maxItems Then
Dim colNew As Collection: Set colNew = New Collection
Dim modulo As Integer: modulo = m_Col.Count / maxItems
Dim I As Integer
For I = 1 To m_Col.Count
If I Mod modulo = 0 Then
colNew.Add m_Col(I)
End If
Next I
Set m_Col = colNew
End If
End Sub
This subroutine is actually not too bad, though thanks to a weird logical approach and a terrible name, it took a solid five minutes of staring at this code before I got what was going on here. First off, this shouldn’t be called sort
. That much, I think, is obvious. Try sample
or skip
. That’s all this does- if you have a collection with 9 items in it, and you want a collection with only 3 items in it, this will take every third item of the original collection.
In the end, there are three key problems with this code that lead to it being posted here. First, is the function name. Talk about misleading. Second is creating a variable called modulo
and initializing it by division. Sure, it’s used as part of a modulus expression later, but still.
The real source of confusion, at least for me, arises from what I believe is a lack of language knowledge- they didn’t know how to change the increment on the For
loop. Instead of the If I Mod modulo = 0
expression, they could have simply written the for loop thus: For I = 1 to m_Col.Count Step modulo
.
So, Andrea, I’m sorry, but, this isn’t Hell. A codebase full of code like this, it’s like a third-ring suburb of Hell. Sure, you’ve got a 85 minute commute into one of the first-ring suburbs for work, the only source of entertainment within twenty miles is the shopping mall, and your HOA just fined you for planting tomatoes in your garden, but at least the schools are probably decent.