C# 编写备用变量选择代码的更好方法

C# 编写备用变量选择代码的更好方法,c#,c#-4.0,C#,C# 4.0,考虑到下面的简单代码块,我想知道是否有更好的方法用C编写代码 int lowIndex = 0; int highIndex = 1; if (end[0].X.ConvertToMillimetres() == end[1].X.ConvertToMillimetres()) { if (end[0].Y.ConvertToMillimetres() > end[1].Y.ConvertToMilli

考虑到下面的简单代码块,我想知道是否有更好的方法用C编写代码

        int lowIndex = 0;
        int highIndex = 1;
        if (end[0].X.ConvertToMillimetres() == end[1].X.ConvertToMillimetres())
        {
            if (end[0].Y.ConvertToMillimetres() > end[1].Y.ConvertToMillimetres())
            {
                lowIndex = 1;
                highIndex = 0;
            }
        }
        else
        {
            if (end[0].X.ConvertToMillimetres() > end[1].X.ConvertToMillimetres())
            {
                lowIndex = 1;
                highIndex = 0;
            }
        }

像这样的怎么样

int lowIndex = 0; 
int highIndex = 1; 
if ((end[0].X.ConvertToMillimetres() == end[1].X.ConvertToMillimetres() && end[0].Y.ConvertToMillimetres() > end[1].Y.ConvertToMillimetres()) ||
    (end[0].X.ConvertToMillimetres() > end[1].X.ConvertToMillimetres()))
{ 
    lowIndex = 1; 
    highIndex = 0; 
} 

也许是这样的:

int X0mm = end[0].X.ConvertToMillimetres();
int X1mm = end[1].X.ConvertToMillimetres();
int Y0mm = end[0].Y.ConvertToMillimetres();
int Y1mm = end[1].Y.ConvertToMillimetres();

int lowIndex = (X0mm == X1mm && Y0mm > Y1mm) || (X0mm > X1mm) ? 1 : 0;
int highIndex = lowIndex == 1 ? 0 :1;

我想你要做的是消除两行设置lowIndex和highIndex的代码。您可以像这样组合IF语句

int lowIndex = 0;
int highIndex = 1;
if ( (end[0].X.ConvertToMillimetres() == end[1].X.ConvertToMillimetres() &&
      end[0].Y.ConvertToMillimetres() > end[1].Y.ConvertToMillimetres()) ||
      end[0].X.ConvertToMillimetres() > end[1].X.ConvertToMillimetres() )
{
    lowIndex = 1;
    highIndex = 0;
}

不要认为这是针对C4.0的,但您可以让它更具可读性:

var endOne = end[0];
var endTwo = end[1];

//now, if you would override the == operator for the type of X and Y to compare using ConvertToMillimetres(), you can have something like:

int lowIndex = (endOne.X == endTwo.X && endOne.Y > endTwo.Y) || (endOne.X > endTwo.X) ? 1 : 0;
int highIndex = lowIndex == 1 ? 0 : 1;
当然可以:

int lowIndex = 0;
int highIndex = 1;
if (   end[0].X.ConvertToMillimetres() == end[1].X.ConvertToMillimetres() 
    && end[0].Y.ConvertToMillimetres() >  end[1].Y.ConvertToMillimetres()  
    || end[0].X.ConvertToMillimetres() != end[1].X.ConvertToMillimetres()
    && end[0].X.ConvertToMillimetres() >  end[1].X.ConvertToMillimetres())
{
    lowIndex = 1;
    highIndex = 0;
}
结束[0]。X.converttomillimeters!=结束[1]。X.1毫米 &&end[0]。X.converttomillimeters>end[1]。X.converttomillimeters将始终等同于end[0]。X.converttomillimeters>end[1]。X.converttomillimeters,因此:

int lowIndex = 0;
int highIndex = 1;
if (   end[0].X.ConvertToMillimetres() == end[1].X.ConvertToMillimetres() 
    && end[0].Y.ConvertToMillimetres() >  end[1].Y.ConvertToMillimetres()  
    || end[0].X.ConvertToMillimetres() >  end[1].X.ConvertToMillimetres())
{
    lowIndex = 1;
    highIndex = 0;
}
最后,我不确定ConvertToMillimeters的结果是什么,或者它有多复杂/如果ConvertToMillimeters使用一些局部变量来捕获这些方法的值以减少计算量,这可能是有意义的。。。再说一次,如果不是的话,为了节省一点时间,可能不值得污染你当地的环境。很可能,这是一个相当简单的函数,所以它不会很有优势。然而,正如克里希纳所说,end[0]和end作为局部变量可能工作得更好。或者甚至是end.X和end.Y,等等,但是如果你这样做,最好保存结果

//capture values

var end0Xm = end[0].X.ConvertToMillimetres();
var end1Xm = end[1].X.ConvertToMillimetres();
var end0Ym = end[0].Y.ConvertToMillimetres();
var end1Ym = end[1].Y.ConvertToMillimetres();

//define proper lowIndex, highIndex
int lowIndex = 0;
int highIndex = 1;
if (   end0Xm  == end1Xm 
    && end0Ym  >  end1Ym  
    || end0Xm  >  end1Xm )
{
    lowIndex = 1;
    highIndex = 0;
}
保存测试结果以备将来使用可能会很有用,同时也可以消除if块,从而减少将来有人出错的机会。然而,你仍然必须有条件地做一些事情。下一个代码块假定您知道的存在并理解

或者你更喜欢highIndex=!测试用例?1:0,甚至高索引=1-低索引


等等,等等。

我会做一个方法:代码可维护

private void GetValueM(List<EndType> end,out int  lowIndex,out int  highIndex)
    {
         lowIndex = 0;
         highIndex = 1;

         if ((end != null) && (end.Count > 2))
         {
             var x0 = end[0].X;
             var x1 = end[1].X;
             var y0 = end[0].Y;
             var y1 = end[1].Y;

             if (x0 != null && x1 != null && y0 != null && y1 != null)
             {
                 if ((x0.ConvertToMillimetres() == x1.ConvertToMillimetres() && y0.ConvertToMillimetres() > y1.ConvertToMillimetres()) ||
                     (x0.ConvertToMillimetres() > x1.ConvertToMillimetres()))
                 {
                     lowIndex = 1;
                     highIndex = 0;
                 }

             }
             else
             {
                 //Any is null  set your value or throw exception
             }
         }


    }

与紧凑的代码相比,我更喜欢可读性!: 重命名变量,使其最适合您的代码

int xComparison = end[0].X.ConvertToMillimetres().CompareTo(end[1].X.ConvertToMillimetres());
int yComparison = end[0].Y.ConvertToMillimetres().CompareTo(end[1].Y.ConvertToMillimetres());

bool isMatch = ((xComparison == 0 && yComparison > 0) || xComparison > 0);

int lowIndex = (isMatch ? 1 : 0);
int highIndex = (isMatch ? 0 : 1);

如前所述,从不使用lowIndex和highIndex的默认值。您可以在底部定义这些整数,使代码更加紧凑。我已经编写了完全相同的代码块,但您比我快了几分钟。为什么需要转换为mm来比较值?这似乎是多余的。您自己也说过这是一个简单的代码块。因此,是否有必要使其更加简单?可读性是最重要的。@Peter需要转换为毫米,因为主值是一个双精度值,转换时等于同一个值,否则我处理的是非精确测量的公差。@CodeSparke-谢谢,我不知道这个位置
int xComparison = end[0].X.ConvertToMillimetres().CompareTo(end[1].X.ConvertToMillimetres());
int yComparison = end[0].Y.ConvertToMillimetres().CompareTo(end[1].Y.ConvertToMillimetres());

bool isMatch = ((xComparison == 0 && yComparison > 0) || xComparison > 0);

int lowIndex = (isMatch ? 1 : 0);
int highIndex = (isMatch ? 0 : 1);