2012-09-12 16 views
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; 
      } 
     } 
+2

[codereview.se] – Adam

+0

爲什麼你需要轉換爲毫米來比較值?似乎多餘。你自己也說過這是一個簡單的代碼塊。因此是否需要使它更簡單?可讀性是國王。 –

+0

[標題不需要標籤](http://meta.stackexchange。com/questions/19190/should-questions-include-tags-in-titles) – Default

回答

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

我已經寫了完全相同的代碼塊,但你幾分鐘就打敗了我。 –

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; 
+0

正如寫入'lowIndex'和'highIndex'的默認值從不使用。您可以在底部定義這些整數,而不是使代碼更加緊湊。 –

+0

@ScottLemmon是的,你是對的。 – Reniuz

+0

@ScottLemmon - 再看一眼... – sweetfa

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

這可能是保存測試以備將來使用的結果是有用的,也即消除了,如果塊,這給少了機會爲別人弄亂的未來。但是,你仍然必須有條件地做一些事情。下一個代碼塊假定您知道並瞭解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

等等等等。

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


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