Drugsort

Remy escaped the enterprise world and now makes LEDs blink pretty. Editor-in-Chief for TDWTF.

I did a brief contract with Hershey, the candy manufacturer, once. The biggest thing I recall from the experience was that they had bowls full of candy all over the place. You could just grab them by the handful.

I bring this up, because Brenda worked for a pharmaceutical company, and I can only assume that there are bowls full of random drugs scattered around, and someone has been chowing down on them by the handful. That’s the most logical explanation for the following code:

'From ...(7/21/2008 3:18:30 PM): Sort the additional additives in alphabetical order
'Albumin
'Ascorbic Acid
'Chromium
'Cysteine + Acetate
'Cysteine - NoAcetate
'Epogen
'Famotidine
'Fervens Canis (aquaeous)
'FolicAcid
'Heparin
'Insulin *R* Human
'IronDextran
'Levocarnitine
'MVI - Adult Without Vitamin K
'MVI Pediatric
'Metoclopramide
'Ped-Trace 4
'Phytonadione-K1
'Ranitidine
'Selenium
'Selenium > 5years
'Zinc
Dim additivescount As Integer = Me.treeIngredients.Nodes(4).GetNodeCount(True)
Dim node_albumin As TreeNode = Nothing
Dim node_ascorbic As TreeNode = Nothing
Dim node_chromium As TreeNode = Nothing
Dim node_cysteineplus As TreeNode = Nothing
Dim node_cysteineminus As TreeNode = Nothing
Dim node_epogen As TreeNode = Nothing
Dim node_famotidine As TreeNode = Nothing
Dim node_folic As TreeNode = Nothing
Dim node_heparin As TreeNode = Nothing
Dim node_insulin As TreeNode = Nothing
Dim node_iron As TreeNode = Nothing
Dim node_levocarnitine As TreeNode = Nothing
Dim node_lipids3in1 As TreeNode = Nothing
Dim node_mviadult As TreeNode = Nothing
Dim node_mviped As TreeNode = Nothing
Dim node_metoclopramide As TreeNode = Nothing
Dim node_pedtrace4 As TreeNode = Nothing
Dim node_phytonadione As TreeNode = Nothing
Dim node_ranitidine As TreeNode = Nothing
Dim node_selenium As TreeNode = Nothing
Dim node_selenium5 As TreeNode = Nothing
Dim node_zinc As TreeNode = Nothing
Dim node_hotdogwater As TreeNode = Nothing
For cnt = 0 To additivescount - 1
If Me.treeIngredients.Nodes(4).Nodes(cnt).Text.StartsWith("Albumin") Then
node_albumin = Me.treeIngredients.Nodes(4).Nodes(cnt)
End If
If Me.treeIngredients.Nodes(4).Nodes(cnt).Text.StartsWith("Ascorbic") Then
node_ascorbic = Me.treeIngredients.Nodes(4).Nodes(cnt)
End If
If Me.treeIngredients.Nodes(4).Nodes(cnt).Text.StartsWith("Chromium") Then
node_chromium = Me.treeIngredients.Nodes(4).Nodes(cnt)
End If
If Me.treeIngredients.Nodes(4).Nodes(cnt).Text.StartsWith("Cysteine + Acetate") Then
node_cysteineplus = Me.treeIngredients.Nodes(4).Nodes(cnt)
End If
If Me.treeIngredients.Nodes(4).Nodes(cnt).Text.StartsWith("Cysteine - No Acetate") Then
node_cysteineminus = Me.treeIngredients.Nodes(4).Nodes(cnt)
End If
If Me.treeIngredients.Nodes(4).Nodes(cnt).Text.StartsWith("Epogen") Then
node_epogen = Me.treeIngredients.Nodes(4).Nodes(cnt)
End If
If Me.treeIngredients.Nodes(4).Nodes(cnt).Text.StartsWith("Famotidine") Then
node_famotidine = Me.treeIngredients.Nodes(4).Nodes(cnt)
End If
If Me.treeIngredients.Nodes(4).Nodes(cnt).Text.StartsWith("Fervens Canis (aquaeous)") Then
node_hotdogwater = Me.treeIngredients.Nodes(4).Nodes(cnt)
End If
If Me.treeIngredients.Nodes(4).Nodes(cnt).Text.StartsWith("Folic") Then
node_folic = Me.treeIngredients.Nodes(4).Nodes(cnt)
End If
If Me.treeIngredients.Nodes(4).Nodes(cnt).Text.StartsWith("Heparin") Then
node_heparin = Me.treeIngredients.Nodes(4).Nodes(cnt)
End If
If Me.treeIngredients.Nodes(4).Nodes(cnt).Text.StartsWith("Insulin") Then
node_insulin = Me.treeIngredients.Nodes(4).Nodes(cnt)
End If
If Me.treeIngredients.Nodes(4).Nodes(cnt).Text.StartsWith("Iron") Then
node_iron = Me.treeIngredients.Nodes(4).Nodes(cnt)
End If
If Me.treeIngredients.Nodes(4).Nodes(cnt).Text.StartsWith("Levocarnitine") Then
node_levocarnitine = Me.treeIngredients.Nodes(4).Nodes(cnt)
End If
If Me.treeIngredients.Nodes(4).Nodes(cnt).Text.StartsWith("Lipids (3-In-1)") Then
node_lipids3in1 = Me.treeIngredients.Nodes(4).Nodes(cnt)
End If
If Me.treeIngredients.Nodes(4).Nodes(cnt).Text.StartsWith("MVI - Adult") Then
node_mviadult = Me.treeIngredients.Nodes(4).Nodes(cnt)
End If
If Me.treeIngredients.Nodes(4).Nodes(cnt).Text.StartsWith("MVI Pediatric") Then
node_mviped = Me.treeIngredients.Nodes(4).Nodes(cnt)
End If
If Me.treeIngredients.Nodes(4).Nodes(cnt).Text.StartsWith("Metoclopramide") Then
node_metoclopramide = Me.treeIngredients.Nodes(4).Nodes(cnt)
End If
If Me.treeIngredients.Nodes(4).Nodes(cnt).Text.StartsWith("Ped-Trace") Then
node_pedtrace4 = Me.treeIngredients.Nodes(4).Nodes(cnt)
End If
If Me.treeIngredients.Nodes(4).Nodes(cnt).Text.StartsWith("Phytonadione-K1") Then
node_phytonadione = Me.treeIngredients.Nodes(4).Nodes(cnt)
End If
If Me.treeIngredients.Nodes(4).Nodes(cnt).Text.StartsWith("Ranitidine") Then
node_ranitidine = Me.treeIngredients.Nodes(4).Nodes(cnt)
End If
If Me.treeIngredients.Nodes(4).Nodes(cnt).Text.StartsWith("Selenium") Then
If Me.treeIngredients.Nodes(4).Nodes(cnt).Text.IndexOf(">") > 0 Then
node_selenium5 = Me.treeIngredients.Nodes(4).Nodes(cnt)
Else
node_selenium = Me.treeIngredients.Nodes(4).Nodes(cnt)
End If
End If
If Me.treeIngredients.Nodes(4).Nodes(cnt).Text.StartsWith("Zinc") Then
node_zinc = Me.treeIngredients.Nodes(4).Nodes(cnt)
End If
Next
For cnt = 0 To additivescount - 1
Me.treeIngredients.Nodes(4).Nodes(0).Remove()
Next
If Not node_zinc Is Nothing Then Me.treeIngredients.Nodes(4).Nodes.Insert(0, node_zinc)
If Not node_selenium5 Is Nothing Then Me.treeIngredients.Nodes(4).Nodes.Insert(0, node_selenium5)
If Not node_selenium Is Nothing Then Me.treeIngredients.Nodes(4).Nodes.Insert(0, node_selenium)
If Not node_ranitidine Is Nothing Then Me.treeIngredients.Nodes(4).Nodes.Insert(0, node_ranitidine)
If Not node_phytonadione Is Nothing Then Me.treeIngredients.Nodes(4).Nodes.Insert(0, node_phytonadione)
If Not node_pedtrace4 Is Nothing Then Me.treeIngredients.Nodes(4).Nodes.Insert(0, node_pedtrace4)
If Not node_metoclopramide Is Nothing Then Me.treeIngredients.Nodes(4).Nodes.Insert(0, node_metoclopramide)
If Not node_mviped Is Nothing Then Me.treeIngredients.Nodes(4).Nodes.Insert(0, node_mviped)
If Not node_mviadult Is Nothing Then Me.treeIngredients.Nodes(4).Nodes.Insert(0, node_mviadult)
If Not node_lipids3in1 Is Nothing Then Me.treeIngredients.Nodes(4).Nodes.Insert(0, node_lipids3in1)
If Not node_levocarnitine Is Nothing Then Me.treeIngredients.Nodes(4).Nodes.Insert(0, node_levocarnitine)
If Not node_iron Is Nothing Then Me.treeIngredients.Nodes(4).Nodes.Insert(0, node_iron)
If Not node_insulin Is Nothing Then Me.treeIngredients.Nodes(4).Nodes.Insert(0, node_insulin)
If Not node_heparin Is Nothing Then Me.treeIngredients.Nodes(4).Nodes.Insert(0, node_heparin)
If Not node_folic Is Nothing Then Me.treeIngredients.Nodes(4).Nodes.Insert(0, node_folic)
If Not node_hotdogwater Is Nothing Then Me.treeIngredients.Nodes(4).Nodes.Insert(0, node_hotdogwater)
If Not node_famotidine Is Nothing Then Me.treeIngredients.Nodes(4).Nodes.Insert(0, node_famotidine)
If Not node_epogen Is Nothing Then Me.treeIngredients.Nodes(4).Nodes.Insert(0, node_epogen)
If Not node_cysteineminus Is Nothing Then Me.treeIngredients.Nodes(4).Nodes.Insert(0, node_cysteineminus)
If Not node_cysteineplus Is Nothing Then Me.treeIngredients.Nodes(4).Nodes.Insert(0, node_cysteineplus)
If Not node_chromium Is Nothing Then Me.treeIngredients.Nodes(4).Nodes.Insert(0, node_chromium)
If Not node_ascorbic Is Nothing Then Me.treeIngredients.Nodes(4).Nodes.Insert(0, node_ascorbic)
If Not node_albumin Is Nothing Then Me.treeIngredients.Nodes(4).Nodes.Insert(0, node_albumin)

The comment does us the courtesy of telling us what the goal of this code is: sort the list of possible additives.

Understanding how it accomplishes that goal is a bit trickier, and to do that, we need to understand something about old-timey VisualBasic. Prior to .NET, VisualBasic was a terrible language, and did not have a particularly rich set of collection primitives, and the API wasn’t that clear. Many VB programmers didn’t even bother to learn about them, since VisualBasic was all about slapping form controls together on a designer and then wiring code up to them. This meant that, instead of fighting their way through collection types, many VB developers used the hammer they were already familiar with to solve problems. Developers that learned this habit when it was common in, say, 1998, often continued in this habit until, say, 2008, when this code was written.

With that in mind, Me.treeIngredients is a TreeView control, meant to be used like well, a tree-view. Each variable declaration (the Dim statements) is creating a new TreeNode, which are the individual branches and leaves we want to see in the tree. This particular set of nodes is never actually used in a display (there’s a separate TreeView for that).

So, section by section, let’s look at what this does. First, it creates a pile of TreeNode objects. Then, for each element currently in the TreeView, we run a loop and check: if it starts with, say, “Metoclopramide”, grab that node and put it in the variable node_metoclopramide. Then loop across the TreeView again, but this time, remove all the nodes. Finally, look at that set of variables you just created, and if each one of them has a value, insert it into the first position in the list. So long as we do those inserts in reverse alphabetical order, the end result will be that items are added to the list in alphabetical order.

As you can imagine, this wasn’t written as a reusable function, but instead, was copied and pasted everywhere additives needed to be sorted. This kept Brenda very busy when she needed to add a few dozen new additives to the application.

[Advertisement]
BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!