2017-02-17 21 views
0

タイトルの言葉の正確な言い方ではありませんが、一般的な質問はこれらの2つのコードブロックを比較しています。最初のものは元のもので、2番目のものはそれを置き換えるものです。if、else if、else文の機能性と可読性

これは実際にコードを改善しますか、それは効率か、可読性を向上させるだけでしょうか?私は何かが欠けていない限り、彼らは機能的に異なるとは思わない。

1ST:

if(stl1YVal < stl2YVal){ 
     return -1; 
    }else if(stl1YVal > stl2YVal){ 
     return 1; 
    }else{ 
     if(stl1XVal < stl2XVal){ 
      return -1; 
     }else if(stl1XVal > stl2XVal){ 
      return 1; 
     }else{ 
      return 0; 
     } 
    } 

2ND:

if(stl1YVal < stl2YVal || stl1XVal < stl2XVal){ 
     return -1; 
    }else if(stl1YVal > stl2YVal || stl1XVal > stl2XVal){ 
     return 1; 
    }else { 
     return 0; 
    } 

編集:私は場合...それは人々に少しより多くの意味を行うことができるように全体の方法を入れています人々はそれをもっと読みやすくて機能的にするための答え/解決策を持っています(2番目のものは同等ではないようです)。

(彼らがあるため、単純なテキスト行のSTL命名されている - 私はそれらに名前を付けていなかった)

public int compare(SimpleTextLine stl1, SimpleTextLine stl2){ 
     //0 -> stl1 and stl2 are equivalent, maintain current order 
     //-1 -> stl1 comes before stl2 
     //1 -> stl2 comes before stl1 

     float stl1XVal = Math.round(stl1.getxLinePos()); 
     float stl1YVal = Math.round(stl1.getyLinePos()); 
     float stl2XVal = Math.round(stl2.getxLinePos()); 
     float stl2YVal = Math.round(stl2.getyLinePos()); 

     if(stl1YVal < stl2YVal){ 
      return -1; 
     }else if(stl1YVal > stl2YVal){ 
      return 1; 
     }else{ 
      if(stl1XVal < stl2XVal){ 
       return -1; 
      }else if(stl1XVal > stl2XVal){ 
       return 1; 
      }else{ 
       return 0; 
      } 
     } 
    } 
+7

は必ず例これらの種類の読みやすさを好みます。実際の効率は、あなたが使っている 'if'ステートメントの種類とは全く異なる場所で決まります。 – Kayaman

+2

2番目のソリューションは機能的には最初のソリューションと同等ではありません。例: 'stl1YVal> stl2YVal'が真で、' stl1XVal dunni

+0

私は@Kayamanに同意します。私はむしろ2番目のブロックに取り組んでいます。効率/実行時間は、言語と環境に大きく依存するものであり、何回も繰り返し実行されていなければ意味がありません。それでも、直接的な証拠(例えばプロファイリング)なしで「最適化」を開始するのは間違いでしょう。 –

答えて

2

は、私は元のコードの「クリーン」バージョンは、これらの一つだと思い:

// Option 1 (eliminating redundant 'else' clauses and nesting) 
if (stl1YVal < stl2YVal) 
    return -1; 
if (stl1YVal > stl2YVal) 
    return 1; 
if (stl1XVal < stl2XVal) 
    return -1; 
if (stl1XVal > stl2XVal) 
    return 1; 
return 0; 
// Option 2 (using conditional ternary operator) 
return (stl1YVal < stl2YVal ? -1 : 
     stl1YVal > stl2YVal ? 1 : 
     stl1XVal < stl2XVal ? -1 : 
     stl1XVal > stl2XVal ? 1 : 0); 

私はオプション2が素晴らしいと思います。オプション1が中括弧({})を必要とするかどうかは、あなたのコーディング基準に従います。

1

何かが紛失していない限り、機能的に異なるとは思いません。

この簡単なテスト検討してください:

public class Test { 

    public static void main(String[] args) { 

     for(int y1=-1;y1<2;y1++){ 
      for(int y2=-1;y2<2;y2++){ 
       for(int x1=-1;x1<2;x1++){ 
        for(int x2=-1;x2<2;x2++){ 
         System.out.format(" %2d %2d %2d %2d ", y1, y2, x1, x2); 
         System.out.print(" ---- "); 
         int first = first(y1, y2, x1, x2); 
         int second = second(y1, y2, x1, x2); 
         System.out.format(" %2d %2d %b\n", first, second, first == second); 
        } 
       } 
      } 

     } 
    } 

    public static int first(int stl1YVal, int stl2YVal, int stl1XVal, int stl2XVal) { 
     if (stl1YVal < stl2YVal) { 
      return -1; 
     } else if (stl1YVal > stl2YVal) { 
      return 1; 
     } else { 
      if (stl1XVal < stl2XVal) { 
       return -1; 
      } else if (stl1XVal > stl2XVal) { 
       return 1; 
      } else { 
       return 0; 
      } 
     } 
    } 

    public static int second(int stl1YVal, int stl2YVal, int stl1XVal, int stl2XVal) { 
     if (stl1YVal < stl2YVal || stl1XVal < stl2XVal) { 
      return -1; 
     } else if (stl1YVal > stl2YVal || stl1XVal > stl2XVal) { 
      return 1; 
     } else { 
      return 0; 
     } 
    } 
} 

を結果が等しくないいくつかの例が含まれ、その結果を検討してください:

-1 -1 -1 -1 ---- 0 0 true 
-1 -1 -1 0 ---- -1 -1 true 
-1 -1 -1 1 ---- -1 -1 true 
-1 -1 0 -1 ---- 1 1 true 
-1 -1 0 0 ---- 0 0 true 
-1 -1 0 1 ---- -1 -1 true 
-1 -1 1 -1 ---- 1 1 true 
-1 -1 1 0 ---- 1 1 true 
-1 -1 1 1 ---- 0 0 true 
-1 0 -1 -1 ---- -1 -1 true 
-1 0 -1 0 ---- -1 -1 true 
-1 0 -1 1 ---- -1 -1 true 
-1 0 0 -1 ---- -1 -1 true 
-1 0 0 0 ---- -1 -1 true 
-1 0 0 1 ---- -1 -1 true 
-1 0 1 -1 ---- -1 -1 true 
-1 0 1 0 ---- -1 -1 true 
-1 0 1 1 ---- -1 -1 true 
-1 1 -1 -1 ---- -1 -1 true 
-1 1 -1 0 ---- -1 -1 true 
-1 1 -1 1 ---- -1 -1 true 
-1 1 0 -1 ---- -1 -1 true 
-1 1 0 0 ---- -1 -1 true 
-1 1 0 1 ---- -1 -1 true 
-1 1 1 -1 ---- -1 -1 true 
-1 1 1 0 ---- -1 -1 true 
-1 1 1 1 ---- -1 -1 true 
    0 -1 -1 -1 ---- 1 1 true 
    0 -1 -1 0 ---- 1 -1 false 
    0 -1 -1 1 ---- 1 -1 false 
    0 -1 0 -1 ---- 1 1 true 
    0 -1 0 0 ---- 1 1 true 
    0 -1 0 1 ---- 1 -1 false 
    0 -1 1 -1 ---- 1 1 true 
    0 -1 1 0 ---- 1 1 true 
    0 -1 1 1 ---- 1 1 true 
    0 0 -1 -1 ---- 0 0 true 
    0 0 -1 0 ---- -1 -1 true 
    0 0 -1 1 ---- -1 -1 true 
    0 0 0 -1 ---- 1 1 true 
    0 0 0 0 ---- 0 0 true 
    0 0 0 1 ---- -1 -1 true 
    0 0 1 -1 ---- 1 1 true 
    0 0 1 0 ---- 1 1 true 
    0 0 1 1 ---- 0 0 true 
    0 1 -1 -1 ---- -1 -1 true 
    0 1 -1 0 ---- -1 -1 true 
    0 1 -1 1 ---- -1 -1 true 
    0 1 0 -1 ---- -1 -1 true 
    0 1 0 0 ---- -1 -1 true 
    0 1 0 1 ---- -1 -1 true 
    0 1 1 -1 ---- -1 -1 true 
    0 1 1 0 ---- -1 -1 true 
    0 1 1 1 ---- -1 -1 true 
    1 -1 -1 -1 ---- 1 1 true 
    1 -1 -1 0 ---- 1 -1 false 
    1 -1 -1 1 ---- 1 -1 false 
    1 -1 0 -1 ---- 1 1 true 
    1 -1 0 0 ---- 1 1 true 
    1 -1 0 1 ---- 1 -1 false 
    1 -1 1 -1 ---- 1 1 true 
    1 -1 1 0 ---- 1 1 true 
    1 -1 1 1 ---- 1 1 true 
    1 0 -1 -1 ---- 1 1 true 
    1 0 -1 0 ---- 1 -1 false 
    1 0 -1 1 ---- 1 -1 false 
    1 0 0 -1 ---- 1 1 true 
    1 0 0 0 ---- 1 1 true 
    1 0 0 1 ---- 1 -1 false 
    1 0 1 -1 ---- 1 1 true 
    1 0 1 0 ---- 1 1 true 
    1 0 1 1 ---- 1 1 true 
    1 1 -1 -1 ---- 0 0 true 
    1 1 -1 0 ---- -1 -1 true 
    1 1 -1 1 ---- -1 -1 true 
    1 1 0 -1 ---- 1 1 true 
    1 1 0 0 ---- 0 0 true 
    1 1 0 1 ---- -1 -1 true 
    1 1 1 -1 ---- 1 1 true 
    1 1 1 0 ---- 1 1 true 
    1 1 1 1 ---- 0 0 true 
+1

それは彼らが異なっていたと結論づけられましたが、詳細についてはまだ+1です、ありがとう –