I have two flavours of a method in a class, one with an extra parameter
first one:
public override void CalcV(IV iv)
{
initializations
otherOperations
for (int i=0; i < NUM; ++i)
{
SomeOtherOperations
double v = GetV(a,b,c);
SomeOtherOperationsUsing_v
}
restOfOperations
}
and second one:
public override void CalcV(IV iv, int index)
{
initializations
otherOperations
for (int i=0; i < NUM; ++i)
{
SomeOtherOperations
double v = GetV(a,b,c, index);
SomeOtherOperationsUsing_v
}
restOfOperations
}
as you can see the only difference is that the first one calls GetV() with 3 parameters and the second one calls an overload of GetV() with 4 parameters.
How best I can avoid code duplication here?
Thanks!
If you're using .Net 4.0, you can make it an optional parameter:
public override void CalcV(IV iv, int index = -1)
{
....
double v = index > -1 ? GetV(a,b,c, index) : GetV(a,b,c);
....
}