编写备用变量选择的更好方法

时间:2012-09-12 05:27:59

标签: 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.ConvertToMillimetres())
            {
                lowIndex = 1;
                highIndex = 0;
            }
        }
        else
        {
            if (end[0].X.ConvertToMillimetres() > end[1].X.ConvertToMillimetres())
            {
                lowIndex = 1;
                highIndex = 0;
            }
        }

7 个答案:

答案 0 :(得分:4)

这样的东西
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; 
} 

答案 1 :(得分:1)

也许是这样的:

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;

答案 2 :(得分:1)

我认为你要做的就是消除两条设置lowIndexhighIndex两行的行。您可以像这样组合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;
}

答案 3 :(得分: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;
}

end[0].X.ConvertToMillimetres() != end[1].X.ConvertToMillimetres() && end[0].X.ConvertToMillimetres() > end[1].X.ConvertToMillimetres()始终等同于end[0].X.ConvertToMillimetres() > end[1].X.ConvertToMillimetres(),因此:

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

最后,我不确定ConvertToMillimetres的结果是什么或它有多复杂/如果ConvertToMillimetres是时间密集的,使用一些局部变量来捕获这些方法的值以减少计算,那么这可能是有意义的...再次,如果不是,可能不值得污染您的本地范围节省一点时间。可能,这是一个相当微不足道的功能,所以它不会非常有利。 (结束[0]和结束1可能更好地作为局部变量,正如克里希纳所说的那样。甚至结束1。X并结束1。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块,从而减少将来某人陷入困境的可能性。但是,你仍然需要有条件地做一些事情。下一个代码块假设您知道C#'s ternary operator的存在并理解。

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
bool testCase = (end0Xm  == end1Xm 
    && end0Ym  >  end1Ym  
    || end0Xm  >  end1Xm);

int lowIndex = testCase? 1 : 0;
int highIndex = testCase? 0 : 1; 

或者您可能更喜欢highIndex = !testcase? 1: 0,甚至更喜欢highIndex = 1 - lowIndex

Etcetera,etcetera。

答案 4 :(得分:1)

我比简洁代码更喜欢可读性! :) 重命名变量,因为它最适合您的代码...

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

答案 5 :(得分:0)

不要认为这是特定于C#4.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;

答案 6 :(得分:0)

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

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


    }