English 中文(简体)
如何避免这里的代码重复?
原标题:How to avoid code duplication here?

我有一个班级有两种方法的口味, 一种是附加参数的口味

第一个:

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
}

和第二个:

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
}

唯一不同的是,第一个呼叫GetV(GetV)(有3个参数),第二个呼叫GetV(有4个参数)超载。

我如何才能最好地避免这里的代码重复?

谢谢!

最佳回答

如果您正在使用.Net 4.0, 您可以把它变成一个可选参数 :

public override void CalcV(IV iv, int index = -1)
{
    ....
    double v = index > -1 ? GetV(a,b,c, index) : GetV(a,b,c);

    ....
}
问题回答

假设你不知道合理的违约 很简单的方法就是:

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

猜猜GetV会做什么(你需要改变这个来适应:

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
}
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
}

然后您就可以移除第一个超位, 这将处理两种情况 。

我想index 是基于0且积极的:

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
}

然后,如果您想用四个参数调用 GetV 或“correct” 索引,您要用 -1 的索引调用函数。

public override void CalcV(IV iv)
{
  return CalcV(iv, -1);
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签