Simplified version?

evios

Member
Joined
Jun 3, 2008
Messages
11
Programming Experience
Beginner
I'd implemented what i need for the project but i found that the code is lengthly and not well practice...
Some more i found that the code keep repeating in some kind of format.....
However, at the moment i still thinking on the way to simplify it...Thanks if anyone can help.....
VB.NET:
Do While Not rs.EOF()
            see = rs.Fields("" & process & "").Value
            Glide = rs.Fields("Glide").Value
            strmac = Mid(see, 1, 2)
            str = Mid(see, 6, 16)
            storedate = Split(str, " ")
            vDateTime = Convert.ToDateTime(storedate(1))
            DropDownList3.Text = storedate(0)

            Select Case (strmac)
                Case "" & Mid(mac(0), 5, 2) & ""
                    If (vDateTime.Hour = "01") Then
                        'Count1 = Count1 + 1
                        machinecount(1, 0) = machinecount(1, 0) + 1
                        If rs.Fields("Glide").Value = " 1 GPASS" Then
                            GHTcount(1, 0) = GHTcount(1, 0) + 1
                        End If
                        rs.MoveNext()
                    ElseIf (vDateTime.Hour = "02") Then
                        'Count2 = Count2 + 1
                        machinecount(2, 0) = machinecount(2, 0) + 1
                        If rs.Fields("Glide").Value = " 1 GPASS" Then
                            GHTcount(2, 0) = GHTcount(2, 0) + 1
                        End If
                        rs.MoveNext()
                    ElseIf (vDateTime.Hour = "03") Then
                        'Count3 = Count3 + 1
                        machinecount(3, 0) = machinecount(3, 0) + 1
                        If rs.Fields("Glide").Value = " 1 GPASS" Then
                            GHTcount(3, 0) = GHTcount(3, 0) + 1
                        End If
                        rs.MoveNext()
                    ElseIf (vDateTime.Hour = "04") Then
                        'Count4 = Count4 + 1
                        machinecount(4, 0) = machinecount(4, 0) + 1
                        If rs.Fields("Glide").Value = " 1 GPASS" Then
                            GHTcount(4, 0) = GHTcount(4, 0) + 1
                        End If
                        rs.MoveNext()
                    ElseIf (vDateTime.Hour = "05") Then
                        'Count5 = Count5 + 1
                        machinecount(5, 0) = machinecount(5, 0) + 1
                        If rs.Fields("Glide").Value = " 1 GPASS" Then
                            GHTcount(5, 0) = GHTcount(5, 0) + 1
                        End If
                        rs.MoveNext()
                    ElseIf (vDateTime.Hour = "06") Then
                        'Count6 = Count6 + 1
                        machinecount(6, 0) = machinecount(6, 0) + 1
                        If rs.Fields("Glide").Value = " 1 GPASS" Then
                            GHTcount(6, 0) = GHTcount(6, 0) + 1
                        End If
                        rs.MoveNext()
                    ElseIf (vDateTime.Hour = "07") Then
                        'Count7 = Count7 + 1
                        machinecount(7, 0) = machinecount(7, 0) + 1
                        If rs.Fields("Glide").Value = " 1 GPASS" Then
                            GHTcount(7, 0) = GHTcount(7, 0) + 1
                        End If
                        rs.MoveNext()
                    ElseIf (vDateTime.Hour = "08") Then
                        'Count8 = Count8 + 1
                        machinecount(8, 0) = machinecount(8, 0) + 1
                        If rs.Fields("Glide").Value = " 1 GPASS" Then
                            GHTcount(8, 0) = GHTcount(8, 0) + 1
                        End If
                        rs.MoveNext()
                    ElseIf (vDateTime.Hour = "09") Then
                        'Count9 = Count9 + 1
                        machinecount(9, 0) = machinecount(9, 0) + 1
                        If rs.Fields("Glide").Value = " 1 GPASS" Then
                            GHTcount(9, 0) = GHTcount(9, 0) + 1
                        End If
                        rs.MoveNext()
                    ElseIf (vDateTime.Hour = "10") Then
                        'Count10 = Count10 + 1
                        machinecount(10, 0) = machinecount(10, 0) + 1
                        If rs.Fields("Glide").Value = " 1 GPASS" Then
                            GHTcount(10, 0) = GHTcount(10, 0) + 1
                        End If
                        rs.MoveNext()
                    ElseIf (vDateTime.Hour = "11") Then
                        'Count11 = Count11 + 1
                        machinecount(11, 0) = machinecount(11, 0) + 1
                        If rs.Fields("Glide").Value = " 1 GPASS" Then
                            GHTcount(11, 0) = GHTcount(11, 0) + 1
                        End If
                        rs.MoveNext()
                    ElseIf (vDateTime.Hour = "12") Then
                        'Count12 = Count12 + 1
                        machinecount(12, 0) = machinecount(12, 0) + 1
                        If rs.Fields("Glide").Value = " 1 GPASS" Then
                            GHTcount(12, 0) = GHTcount(12, 0) + 1
                        End If
                        rs.MoveNext()
                    ElseIf (vDateTime.Hour = "13") Then
                        'Count13 = Count13 + 1
                        machinecount(13, 0) = machinecount(13, 0) + 1
                        If rs.Fields("Glide").Value = " 1 GPASS" Then
                            GHTcount(13, 0) = GHTcount(13, 0) + 1
                        End If
                        rs.MoveNext()
                    ElseIf (vDateTime.Hour = "14") Then
                        'Count14 = Count14 + 1
                        machinecount(14, 0) = machinecount(14, 0) + 1
                        If rs.Fields("Glide").Value = " 1 GPASS" Then
                            GHTcount(14, 0) = GHTcount(14, 0) + 1
                        End If
                        rs.MoveNext()
                    ElseIf (vDateTime.Hour = "15") Then
                        'Count15 = Count15 + 1
                        machinecount(15, 0) = machinecount(15, 0) + 1
                        If rs.Fields("Glide").Value = " 1 GPASS" Then
                            GHTcount(15, 0) = GHTcount(15, 0) + 1
                        End If
                        rs.MoveNext()
                    ElseIf (vDateTime.Hour = "16") Then
                        'Count16 = Count16 + 1
                        machinecount(16, 0) = machinecount(16, 0) + 1
                        If rs.Fields("Glide").Value = " 1 GPASS" Then
                            GHTcount(16, 0) = GHTcount(16, 0) + 1
                        End If
                        rs.MoveNext()
                    ElseIf (vDateTime.Hour = "17") Then
                        'Count17 = Count17 + 1
                        machinecount(17, 0) = machinecount(17, 0) + 1
                        If rs.Fields("Glide").Value = " 1 GPASS" Then
                            GHTcount(17, 0) = GHTcount(17, 0) + 1
                        End If
                        rs.MoveNext()
                    ElseIf (vDateTime.Hour = "18") Then
                        'Count18 = Count18 + 1
                        machinecount(18, 0) = machinecount(18, 0) + 1
                        If rs.Fields("Glide").Value = " 1 GPASS" Then
                            GHTcount(18, 0) = GHTcount(18, 0) + 1
                        End If
                        rs.MoveNext()
                    ElseIf (vDateTime.Hour = "19") Then
                        'Count19 = Count19 + 1
                        machinecount(19, 0) = machinecount(19, 0) + 1
                        If rs.Fields("Glide").Value = " 1 GPASS" Then
                            GHTcount(19, 0) = GHTcount(19, 0) + 1
                        End If
                        rs.MoveNext()
                    ElseIf (vDateTime.Hour = "20") Then
                        'Count20 = Count20 + 1
                        machinecount(20, 0) = machinecount(20, 0) + 1
                        If rs.Fields("Glide").Value = " 1 GPASS" Then
                            GHTcount(20, 0) = GHTcount(20, 0) + 1
                        End If
                        rs.MoveNext()
                    ElseIf (vDateTime.Hour = "21") Then
                        'Count21 = Count21 + 1
                        machinecount(21, 0) = machinecount(21, 0) + 1
                        If rs.Fields("Glide").Value = " 1 GPASS" Then
                            GHTcount(21, 0) = GHTcount(21, 0) + 1
                        End If
                        rs.MoveNext()
                    ElseIf (vDateTime.Hour = "22") Then
                        'Count22 = Count22 + 1
                        machinecount(22, 0) = machinecount(22, 0) + 1
                        If rs.Fields("Glide").Value = " 1 GPASS" Then
                            GHTcount(22, 0) = GHTcount(22, 0) + 1
                        End If
                        rs.MoveNext()
                    ElseIf (vDateTime.Hour = "23") Then
                        'Count23 = Count23 + 1
                        machinecount(23, 0) = machinecount(23, 0) + 1
                        If rs.Fields("Glide").Value = " 1 GPASS" Then
                            GHTcount(23, 0) = GHTcount(23, 0) + 1
                        End If
                        rs.MoveNext()
                    ElseIf (vDateTime.Hour = "00") Then
                        'Count24 = Count24 + 1
                        machinecount(24, 0) = machinecount(24, 0) + 1
                        If rs.Fields("Glide").Value = " 1 GPASS" Then
                            GHTcount(24, 0) = GHTcount(24, 0) + 1
                        End If
                        rs.MoveNext()
                    End If
.....
.....
End Select

Notes:
1.The BIG While loop is to read through what is contained in the database line by line...
2.The mac(i) is a dynamic array of unknown size, at this moment i reserve 10 elements for it.
3.machinecount(X,Y) ranging from X=1 to 24 for 1 day and Y ranging from 0 to maximum of 10 denoting 10 machines for one day record.
 
VB.NET:
                    If (vDateTime.Hour = "01") Then
                        'Count1 = Count1 + 1
                        machinecount(1, 0) = machinecount(1, 0) + 1
                        If rs.Fields("Glide").Value = " 1 GPASS" Then
                            GHTcount(1, 0) = GHTcount(1, 0) + 1
                        End If
                        rs.MoveNext()
                    ElseIf (vDateTime.Hour = "02") Then
                        'Count2 = Count2 + 1
                        machinecount(2, 0) = machinecount(2, 0) + 1
                        If rs.Fields("Glide").Value = " 1 GPASS" Then
                            GHTcount(2, 0) = GHTcount(2, 0) + 1
                        End If
                        rs.MoveNext()
                    ElseIf (vDateTime.Hour = "03") Then
                        'Count3 = Count3 + 1
                        machinecount(3, 0) = machinecount(3, 0) + 1
                        If rs.Fields("Glide").Value = " 1 GPASS" Then
                            GHTcount(3, 0) = GHTcount(3, 0) + 1
                        End If
                        rs.MoveNext()
                    ElseIf (vDateTime.Hour = "04") Then
                        'Count4 = Count4 + 1
                        machinecount(4, 0) = machinecount(4, 0) + 1
                        If rs.Fields("Glide").Value = " 1 GPASS" Then
                            GHTcount(4, 0) = GHTcount(4, 0) + 1
                        End If
                        rs.MoveNext()
                    ElseIf (vDateTime.Hour = "05") Then
                        'Count5 = Count5 + 1
                        machinecount(5, 0) = machinecount(5, 0) + 1
                        If rs.Fields("Glide").Value = " 1 GPASS" Then
                            GHTcount(5, 0) = GHTcount(5, 0) + 1
                        End If
                        rs.MoveNext()
                    ElseIf (vDateTime.Hour = "06") Then
                        'Count6 = Count6 + 1
                        machinecount(6, 0) = machinecount(6, 0) + 1
                        If rs.Fields("Glide").Value = " 1 GPASS" Then
                            GHTcount(6, 0) = GHTcount(6, 0) + 1
                        End If
                        rs.MoveNext()
                    ElseIf (vDateTime.Hour = "07") Then
                        'Count7 = Count7 + 1
                        machinecount(7, 0) = machinecount(7, 0) + 1
                        If rs.Fields("Glide").Value = " 1 GPASS" Then
                            GHTcount(7, 0) = GHTcount(7, 0) + 1
                        End If
                        rs.MoveNext()
                    ElseIf (vDateTime.Hour = "08") Then
                        'Count8 = Count8 + 1
                        machinecount(8, 0) = machinecount(8, 0) + 1
                        If rs.Fields("Glide").Value = " 1 GPASS" Then
                            GHTcount(8, 0) = GHTcount(8, 0) + 1
                        End If
                        rs.MoveNext()
                    ElseIf (vDateTime.Hour = "09") Then
                        'Count9 = Count9 + 1
                        machinecount(9, 0) = machinecount(9, 0) + 1
                        If rs.Fields("Glide").Value = " 1 GPASS" Then
                            GHTcount(9, 0) = GHTcount(9, 0) + 1
                        End If
                        rs.MoveNext()
                    ElseIf (vDateTime.Hour = "10") Then
                        'Count10 = Count10 + 1
                        machinecount(10, 0) = machinecount(10, 0) + 1
                        If rs.Fields("Glide").Value = " 1 GPASS" Then
                            GHTcount(10, 0) = GHTcount(10, 0) + 1
                        End If
                        rs.MoveNext()
                    ElseIf (vDateTime.Hour = "11") Then
                        'Count11 = Count11 + 1
                        machinecount(11, 0) = machinecount(11, 0) + 1
                        If rs.Fields("Glide").Value = " 1 GPASS" Then
                            GHTcount(11, 0) = GHTcount(11, 0) + 1
                        End If
                        rs.MoveNext()
                    ElseIf (vDateTime.Hour = "12") Then
                        'Count12 = Count12 + 1
                        machinecount(12, 0) = machinecount(12, 0) + 1
                        If rs.Fields("Glide").Value = " 1 GPASS" Then
                            GHTcount(12, 0) = GHTcount(12, 0) + 1
                        End If
                        rs.MoveNext()
                    ElseIf (vDateTime.Hour = "13") Then
                        'Count13 = Count13 + 1
                        machinecount(13, 0) = machinecount(13, 0) + 1
                        If rs.Fields("Glide").Value = " 1 GPASS" Then
                            GHTcount(13, 0) = GHTcount(13, 0) + 1
                        End If
                        rs.MoveNext()
                    ElseIf (vDateTime.Hour = "14") Then
                        'Count14 = Count14 + 1
                        machinecount(14, 0) = machinecount(14, 0) + 1
                        If rs.Fields("Glide").Value = " 1 GPASS" Then
                            GHTcount(14, 0) = GHTcount(14, 0) + 1
                        End If
                        rs.MoveNext()
                    ElseIf (vDateTime.Hour = "15") Then
                        'Count15 = Count15 + 1
                        machinecount(15, 0) = machinecount(15, 0) + 1
                        If rs.Fields("Glide").Value = " 1 GPASS" Then
                            GHTcount(15, 0) = GHTcount(15, 0) + 1
                        End If
                        rs.MoveNext()
                    ElseIf (vDateTime.Hour = "16") Then
                        'Count16 = Count16 + 1
                        machinecount(16, 0) = machinecount(16, 0) + 1
                        If rs.Fields("Glide").Value = " 1 GPASS" Then
                            GHTcount(16, 0) = GHTcount(16, 0) + 1
                        End If
                        rs.MoveNext()
                    ElseIf (vDateTime.Hour = "17") Then
                        'Count17 = Count17 + 1
                        machinecount(17, 0) = machinecount(17, 0) + 1
                        If rs.Fields("Glide").Value = " 1 GPASS" Then
                            GHTcount(17, 0) = GHTcount(17, 0) + 1
                        End If
                        rs.MoveNext()
                    ElseIf (vDateTime.Hour = "18") Then
                        'Count18 = Count18 + 1
                        machinecount(18, 0) = machinecount(18, 0) + 1
                        If rs.Fields("Glide").Value = " 1 GPASS" Then
                            GHTcount(18, 0) = GHTcount(18, 0) + 1
                        End If
                        rs.MoveNext()
                    ElseIf (vDateTime.Hour = "19") Then
                        'Count19 = Count19 + 1
                        machinecount(19, 0) = machinecount(19, 0) + 1
                        If rs.Fields("Glide").Value = " 1 GPASS" Then
                            GHTcount(19, 0) = GHTcount(19, 0) + 1
                        End If
                        rs.MoveNext()
                    ElseIf (vDateTime.Hour = "20") Then
                        'Count20 = Count20 + 1
                        machinecount(20, 0) = machinecount(20, 0) + 1
                        If rs.Fields("Glide").Value = " 1 GPASS" Then
                            GHTcount(20, 0) = GHTcount(20, 0) + 1
                        End If
                        rs.MoveNext()
                    ElseIf (vDateTime.Hour = "21") Then
                        'Count21 = Count21 + 1
                        machinecount(21, 0) = machinecount(21, 0) + 1
                        If rs.Fields("Glide").Value = " 1 GPASS" Then
                            GHTcount(21, 0) = GHTcount(21, 0) + 1
                        End If
                        rs.MoveNext()
                    ElseIf (vDateTime.Hour = "22") Then
                        'Count22 = Count22 + 1
                        machinecount(22, 0) = machinecount(22, 0) + 1
                        If rs.Fields("Glide").Value = " 1 GPASS" Then
                            GHTcount(22, 0) = GHTcount(22, 0) + 1
                        End If
                        rs.MoveNext()
                    ElseIf (vDateTime.Hour = "23") Then
                        'Count23 = Count23 + 1
                        machinecount(23, 0) = machinecount(23, 0) + 1
                        If rs.Fields("Glide").Value = " 1 GPASS" Then
                            GHTcount(23, 0) = GHTcount(23, 0) + 1
                        End If
                        rs.MoveNext()
                    ElseIf (vDateTime.Hour = "00") Then
                        'Count24 = Count24 + 1
                        machinecount(24, 0) = machinecount(24, 0) + 1
                        If rs.Fields("Glide").Value = " 1 GPASS" Then
                            GHTcount(24, 0) = GHTcount(24, 0) + 1
                        End If
                        rs.MoveNext()
                    End If

This seems like it could be replaced by this :

VB.NET:
if vDateTime.Hour >= 0 and vDateTime.Hour < 24 then
    dim hour as integer = vDateTime.Hour

    if hour = 0 then
        hour = 24
    end if

    machinecount(hour , 0) = machinecount(hour , 0) + 1

    If rs.Fields("Glide").Value = " 1 GPASS" Then
        GHTcount(hour , 0) = GHTcount(hour , 0) + 1
    End If

    rs.MoveNext()
end if

There may be some syntax errors because I coded this straight in the text box, but the logic should be there. :) There might be some further simplifications possible with the rest of the code, but this shortens it quite a lot ;) .

Basically, instead of using the parameter as a value, you copy/pasted the code and had multiple repetitions each with small changes that could be derived from the value. I've seen a 14k lines class used for printing 50 fancy bar codes on a 8½ x 11 sheet of stickers... The day I had to maintain that, I threw it all out and rewrote within 1k lines in less time it would have taken to do all the little modifications (even with advanced search and replace!).

Quick guidelines for simplifying that kind of code, look for what you change between the copies and see if you can deduce that from an existing value. If you can't, try to invent a new value by putting the code in a loop and using the loop index as a value. If the value does not map to what you need using simple mathematics and conditions, you can fill a dictionary with the necessary pairs at the beginning or you can threat exceptions in localized switches ("select case" statements).

I don't really know how to teach that way of thinking. I just see the patterns everywhere and I get to choose which one I think will work best, but how I find those patterns, I don't think there is a scientific step by step procedure for that. Think about how you would go about drawing a chess board with alternating colors on 8 x 8 for example. The solution is actually just a two levels loop, an addition, a modulo and a condition (but you can draw the background white and just draw the black squares over it or vice-versa). This can be done within 20 lines of code. It's just a matter of seeing the patterns in the logic flow. That helps isolate actions to functions, semantic meanings to classes. It is the very essence of programming!

By the way, are you sure your conditions work for hours 0 to 9?
 
Em...thanks buddy...
it shortens my coding
however
VB.NET:
                    If vDateTime.Hour >= 0 And vDateTime.Hour < 24 Then
                        Dim hour As Integer = vDateTime.Hour
                        If hour = 0 Then
                            hour = 24
                        End If

seems to have more than 24 hours already. Coz i found my array to be starts at 0 until 24, which is over. The statement if hour =0 Then hour=24 means that? Thanks again...
 
Back
Top