I've got a class, let's say a "mutual fund portfolio". It's got 3 methods that calculate 3 different numbers each using a for-loop. Most of the steps in the 3 different methods are exactly the same, except for 1 or 2 steps.
Is there a smarter way to prevent duplicate code than just writing 1 base method and using a case statement for the 3 different numbers to be calculated?
(I hope the description is clear enough.)
Loading
FroglegPosted Jan 4, 2011, 8:57 AM
Public Sub Test_Measure1_Historically(ByVal testType As String)
Dim j As Integer
Dim lNrOfPeriods As Integer '
lNrOfPeriods = 20
For j = 1 To lNrOfPeriods
DoCalculationsWithMemberVariables_Step1
DoCalculationsWithMemberVariables_Step2
Select Case testtype
Case "a"
DoCalculationsWithMemberVariables_Step3a
Case "b"
DoCalculationsWithMemberVariables_Step3b
Case "c"
DoCalculationsWithMemberVariables_Step3c
End Select
DoCalculationsWithMemberVariables_Step4
Next j
End Sub
G van DamnPosted Jan 4, 2011, 6:54 AM
Public Class MutualFundsPortfolio
Public Sub Test_Measure1_Historically()
Dim j As Integer
Dim lNrOfPeriods As Integer '
lNrOfPeriods = 20
For j = 1 To lNrOfPeriods
DoCalculationsWithMemberVariables_Step1
DoCalculationsWithMemberVariables_Step2
DoCalculationsWithMemberVariables_Step3a
DoCalculationsWithMemberVariables_Step4
Next j
End Sub
Public Sub Test_Measure2_Historically()
Dim j As Integer
Dim lNrOfPeriods As Integer '
lNrOfPeriods = 20
For j = 1 To lNrOfPeriods
DoCalculationsWithMemberVariables_Step1
DoCalculationsWithMemberVariables_Step2
DoCalculationsWithMemberVariables_Step3b
DoCalculationsWithMemberVariables_Step4
Next j
End Sub
Public Sub Test_Measure3_Historically()
Dim j As Integer
Dim lNrOfPeriods As Integer '
lNrOfPeriods = 20
For j = 1 To lNrOfPeriods
DoCalculationsWithMemberVariables_Step1
DoCalculationsWithMemberVariables_Step2
DoCalculationsWithMemberVariables_Step3c
DoCalculationsWithMemberVariables_Step4
Next j
End Sub
End Class
FroglegPosted Jan 4, 2011, 5:16 AM