0

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!

jambodev
  • 351
  • 2
  • 3
  • 9

5 Answers5

2

Assuming you don't know a reasonable default, a very simple way would be:

public override void CalcV(IV iv)
{
    CalcV(iv, null);
}

public override void CalcV(IV iv, int? index)
{
     ...
     double v = index.HasValue ? GetV(a,b,c,index.Value) : GetV(a,b,c);
     ...
}
Digitalex
  • 1,494
  • 9
  • 11
  • Some of the other answers assume either 1) That GetV(a,b,c,0) == GetV(a,b,c) or/and 2) that `index` is strictly non-negative. This is not necessarily true now and in the future. My approach assumes neither. – Digitalex May 23 '12 at 10:31
1

Having a guess at what GetV does (you'll need to change this to suit:

public override void CalcV(IV iv)
{
     CalcV(iv, 0);
}


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
}
Paddy
  • 33,309
  • 15
  • 79
  • 114
  • Yep, either that or default parameter - whatever floats your boat :) – Patryk Ćwiek May 23 '12 at 10:18
  • 1
    Well... This will probably not do it since as you said you're guessing that `GetV` with three parameters does the same as `GetV` with four. – Sani Huttunen May 23 '12 at 10:21
  • @Sani Huttunen - I agree - that's what the comment at the start of the answer is for - not enough detail in the original question, but gives the general gist of how to go about it... – Paddy May 23 '12 at 10:53
1

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);

    ....
}
Dave Bish
  • 19,263
  • 7
  • 46
  • 63
1
public override void CalcV(IV iv, int? index = null)
{
     initializations
     otherOperations

     for (int i=0; i < NUM; ++i)
     {
         SomeOtherOperations
         double v = index != null ? GetV(a,b,c, index) : GetV(a,b,c);
         SomeOtherOperationsUsing_v
     }

     restOfOperations
}

Then you can remove the first override and this will deal with both scenarios.

Lloyd Powell
  • 18,270
  • 17
  • 87
  • 123
0

I assume index is 0-based and positive:

public override void CalcV(IV iv, int index)
{
  initializations
  otherOperations

  for (int i=0; i < NUM; ++i)
  {
    SomeOtherOperations
    double v = index == -1 ? GetV(a, b, c) : GetV(a,b,c, index);
    SomeOtherOperationsUsing_v
  }

  restOfOperations
}

Then you call the function with the index of -1 of you want to use GetV with three parameters or a "correct" index if you want to call GetV with four parameters.

public override void CalcV(IV iv)
{
  return CalcV(iv, -1);
}
Sani Huttunen
  • 23,620
  • 6
  • 72
  • 79