代码之家  ›  专栏  ›  技术社区  ›  Amr ElGarhy

应该如何评论if-else结构?[复制]

  •  59
  • Amr ElGarhy  · 技术社区  · 6 年前

    假设你有:

    if(condition) {
        i = 1;
    } else {
        i = 2;
    }
    

    你需要发表评论来解释 if else

    我通常是这样做的:

    //check for condition
    if(condition) {
        i = 1;
    } else {
        //condition isn't met
        i = 2;
    }
    

    我发现这还不够好,因为评论是在不同的层次,所以快速浏览你会发现 评论和 注释看起来像是属于某个内部结构。

    这样说:

    if(condition) {
        //check for condition
        i = 1;
    } else {
        //condition isn't met
        i = 2;
    }
    

    像这样:

    //check for condition
    if(condition) {
        i = 1;
    //condition isn't met
    } else {
        i = 2;
    }
    

    你如何评论这些区块?

    附言。 我并不是要重构这两行代码,只问代码样式和注释格式。

    12 回复  |  直到 15 年前
        1
  •  35
  •   sbooth    9 年前

    如果需要对If-else语句进行注释,我更愿意描述一下是什么使代码达到了这一点。 尤其是在圈复杂度很高的代码中

    if (condition) { 
    // User is taking a course at college x:
        i = 1;
    } else { 
    // User is not taking any course at college x:
        i = 2;
    }
    
        2
  •  22
  •   David Johnstone    15 年前

    另一个选择是:

    if(condition) { //check for condition
        i = 1;
    } else { //condition isn't met
        i = 2;
    }
    
        3
  •  13
  •   Preet Sangha    15 年前

    bool fooIsNotReallyGood = ....;
    
    if(fooIsNotReallyGood) {
    ...
    } else {
    ...
    }
    
        4
  •  9
  •   BalusC    9 年前

    if (someCondition) {
        // If some condition, then do stuff 1.
        doStuff1();
    }
    else {
        // Else do stuff 2.
        doStuff2();
    }
    

    但同样,如果代码已经是自文档化的,那就没有多大意义了。如果由于某些复杂的情况而要添加注释,例如:

    if (x == null || x.startsWith("foo") || x.endsWith("bar") || x.equals("baz")) {
        doStuff1();
    }
    else {
        doStuff2();
    }
    

    然后我会考虑将其重构为:

    boolean someCondition = (x == null || x.startsWith("foo") || x.endsWith("baz") || x.equals("waa");
    
    if (someCondition) {
        doStuff1();
    } else {
        doStuff2();
    }
    

    其中变量名 someCondition 事实上 概括地概括了整个情况。例如。 usernameIsValid userIsAllowedToLogin 大概吧。

        5
  •  5
  •   Chris Knight    15 年前

    使用自我评论条件,那么就不需要额外的评论了。假设条件是贷款价值比达到最大值。这给了我们:

    if (maximumLoanToValueIsReached)
    {
       i=1;
    }
    else
    {
       i=2;
    }
    

    i 去做更有意义的事。

        6
  •  4
  •   sblom    15 年前

    在那些特殊的情况下,我根本不做任何评论——这些注释不会给已经清晰的代码增加任何价值。如果你有一个很难理解的复杂条件,我会考虑把它分解成一个函数(可能是 inline

        7
  •  4
  •   Andrew    12 年前

    注释在很大程度上是个人的事情,并且(正如前面的一些答案所看到的那样)引起了与代码一样多的争论。

    在简单的情况下,注释会影响代码。但假设情况更复杂,我更喜欢:

    /*
    ** Comment explaining what the condition
    ** is trying to determine
    */
    if ( condition )
    {
        /*
        ** Comment explaining the implications
        ** of the condition being met
        */
        do_something();
    }
    else
    {
        /*
        ** Comment explaining the implications
        ** of the condition not being met
        */
        do_something_else();
    }
    

        8
  •  2
  •   Egor Pavlikhin    15 年前

    //condition isn't met 似乎是一个无用的评论。但如果需要这样的评论,我会这样做(C#):

    //check for condition
    if(condition) 
    {
        i = 1;
    } 
    //some other condition
    else 
    {
        i = 2;
    }
    

    但是,如果块只是if else,那么我会在if之前合并两个注释。

    我更喜欢javascript

    //check for condition
    if(condition) {
        i = 1;
    } else { //some other condition
        i = 2;
    }
    

        9
  •  2
  •   Justen    15 年前

    这就是我对if-then语句的注释方式,尽管我通常不需要这样做。我喜欢把它和if/else放在一条直线上,然后在同一个地方加上标签

    if ( condition )    //if above the bar
    {
        i = 0;
        k = 1;
    }
    else                //else if below
    {
        i = 1;
        k = 2;
    }
    
        10
  •  1
  •   Ignacio Vazquez-Abrams    15 年前

    if condition: # <condition dependent variable> was <predicated>
      dosomething()
    elif othercondition: # <othercondition dependent variable> <predicated>
      dootherthing()
    else: # <all variables> <not predicated>
      doelsething()
    
        11
  •  1
  •   Lars    15 年前

    可读。但是,我认为大家一致认为,注释实际上应该为代码增加价值(否则就不言自明了),而且注释样式应该是一致的。

    我处理非直接解释条件的注释的方法如下:

      // If the condition for a local tree imbalance is met,
      // juggle the immediate nodes to re-establish the balance.
      // Otherwise, execute a global balancing pass.
      if ( somewhat muddled condition )
      {
         ...code...
      }
      else // Tree is in local balance
      {
         ... more code...
    
      } // if/else (tree locally imbalanced) 
    

        12
  •  -3
  •   Boris Pavlović    15 年前

    if-else 对方法进行编码并正确命名:

    function main() {
      checkForCondition(condition);
      conditionIsNotMet(condition);
    }
    
    function checkForCondition(boolean condition) {
      if (condition) {
        i = 1;
      }
    }
    
    function conditionIsNotMet(boolean condition) {
      if (!condition) {
        i = 2;
      }
    }
    

    在这样一个很小的例子中,这似乎是一个过度使用,但是想象一下每个 如果还有