3
Answers

Preventing duplicate code in different for loops

G van Damn

G van Damn

13y
4.5k
1

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.)
Answers (3)