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

FroglegRank #1,020 · 1.9K reputationPosted Jan 4, 2011, 12:16 AM EST
G van Damn4 reputationPosted Jan 4, 2011, 1:54 AM EST
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
FroglegRank #1,020 · 1.9K reputationPosted Jan 4, 2011, 3:57 AM EST
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