在if-then-else结构中将注释放在什么位置?[已结束]

Where to put comments in an if-then-else construct? [closed]

提问人:Rabarberski 提问时间:3/9/2009 最后编辑:FlipRabarberski 更新时间:9/25/2020 访问量:13338

问:


想改进这个问题吗?更新问题,以便可以通过编辑这篇文章来用事实和引文来回答。

3年前关闭。

我从来没有决定过评论结构的最佳方式是什么,所以我从来没有标准化过一致的注释方式。 我很欣赏任何见解。if-then-else

一些选项:

a)

if (blabla) { 
   // this comment explains what happens in the IF case
   dothis();
} else { 
  // this comment explains what happens in the ELSE case
   dosomethingelse();
}

缺点:在有多个 dothis() 语句的情况下,我喜欢注释主要块,在这种情况下,并不总是清楚 IF 注释是属于第一个 dothis() 块还是属于整个 IF 情况

或 b)

if (blabla) { // this comment explains what happens in the IF case
   dothis();
} else { // this comment explains what happens in the ELSE case
   dosomethingelse();
}

缺点:仅适用于简短的评论。如果 IF 和 ELSE 大小写不能直接从代码中清除,我通常会注释 IF-THEN-ELSE 构造,这通常需要超过一行的注释。

或 c)

// if the following happens
if (blabla) { // then do this
   dothis();
} else { // or else do this
   dosomethingelse();
}

PS:我知道“代码应该是不言自明的”,但情况并非总是如此......

注释 if-statement

评论

1赞 Alister Bulman 4/17/2009
评论应该解释为什么,而不是正在发生的事情。如果发生了什么不清楚 - 你需要修复它,也许将条件旋转到一个可以逐步分解的函数中。

答:

34赞 David Webb 3/9/2009 #1

对我来说,上面的评论解释了声明本身。例如,如果正在测试的条件特别复杂。IFIF

或下方块中的注释描述了评估条件并做出选择后会发生什么。IFELSE

所以像这样:

//Is this a favoured customer and do we have a promotion?
if customer.special() and monthly.hasOffer() {
  //Add discount
  invoice.addDiscount();
} 
else {
  //Add note about favoured customer scheme
  invoice.addNotes(JOIN_OUR_DISCOUNT_SCHEME);
}

评论

1赞 Surgical Coder 4/17/2009
你知道这些评论对可读性没有任何作用,对吧?
14赞 Mattias Andersson 4/17/2009
@pzycoman:你知道这是一个关于风格而不是内容的讨论,对吧?你也知道评论确实可以提高可读性,对吧?
4赞 workmad3 3/9/2009 #2

我会做案例 a),但有一点额外的空格:

if (blabla) {
   // This explains the whole if case

   // Can comment here for specific block comments
   doThis();
} else {
   // This explains the else case

   // Same again
   doSomethingElse();
}
1赞 David Z 3/9/2009 #3

我猜,使用对你有意义的东西(除非你在指定评论风格的编码标准下工作)。就我个人而言,我不使用 (c),因为它在第一种和另一种情况之间不一致。我偶尔会用 (b) 来做一个简短的评论,但一般来说我更喜欢 (a)。如果我在 if 块中注释几个子块,我可能会在 case 注释后留下一个空行:

if (blabla) {
    // here's a comment about this case

    // comment about this bit of code
    bit_of_code();
    // comment about this other bit of code
    other_bit_of_code();
}

等等。

8赞 Richard Slater 3/9/2009 #4

我从来没有想过太多;就个人而言,在需要时,我会在 IF 和 ELSE 语句上方添加注释。这让我很好地区分了关于分支语句的注释和关于代码的注释。

// comment about the if statement
if (expression)
{
    // comment about the code
    doSomething();
}
// comment about the else statement
else
{
    // comment about the code
    doSomethingElse();
}

我还注意到,到目前为止,我是唯一使用“开放大括号样式”的答案,这可能是回到我的 Pascal 时代,尽管我确实更喜欢代码块的开始和结束的视觉理由,所以我的评论风格可能不适用于“封闭大括号样式社区”。

评论

0赞 David Webb 3/9/2009
你对 else 声明有何评论?通常它们是不言自明的。:-)
0赞 Richard Slater 3/9/2009
如果一个分支在函数的输出上,它可能需要一些扩展,一个更好的例子可能是 elseif。
2赞 Jonathan Leffler 4/17/2009
如果不需要对其中一个进行评论,请省略该评论。这就像函数开始时的样板;如果在某些标题下没有什么可说的,请省略标题。(我不提倡按功能使用样板;我看到的大多数内容都是过时的和错误的!
0赞 Pieter De Bie 10/4/2017
如果其他似乎不太可能,但由于某些边缘情况,它不是?
2赞 John Saunders 3/9/2009 #5

就我个人而言,我发现最好编写不需要小注释的代码,比如“about do do x”,然后是“DoX()”。如有必要,与其写一条注释说“因为 y 而做 x”,我更愿意编写一个名为“DoXBecauseOfY”的方法。如果以后的重构删除了“BecauseOfY”部分,那么在 if 语句之前添加注释以记录整体逻辑仍然更有意义。

当然,您需要将每个分支中的代码量减少到可以一次读取整个 if 语句的程度。

1赞 Rakesh Rawat 11/16/2013 #6
// Not very much sure, but here is a snippet of my code

// tweak URL as per query params and hash index positions
if (hasQueryParams && hashPos > -1) {
    // both query params and hash available
    ...
    ...
} else if (hasQueryParams) {
    // only query params available
    ...
    ...
} else if (hashPos > -1) {
    // only hash available
    ...
    ...
} else {
    // neither query params nor hash available
    ...
    ...
}
1赞 Abdullah Khan 2/27/2016 #7

来自代码约定的 oracle java 文档

if-else 的单行注释:

if (condition) {
 /* Here is a single line comment. */
 ...
}

if-else 的单行非常短的注释:

if (a == 2) {
   return TRUE; /* special case */
} else {
 return isprime(a); /* works only for odd a */
}

if-else 的多行注释:

if (condition) {
 /*
  * Here is a block comment.
  */
}
0赞 Alberto Rivelli 4/15/2016 #8

只是为了为 else 的注释位置添加缺失的答案,在我看来,这是代码可读性的最佳位置,原因如下:

  • 如果注释放在 else 之上,则会破坏 if-else 的连续性
  • 如果放在里面,它可以与else中第一个语句的注释混合


// match jth arc
if (j < Count)
{
     // arc matched
     if (arcs[j].IsBlue) List.Add(arcs[j])
}
else // all arcs were matched
{
     // check if there more arcs
     if (arcs[j + 1] != null) continue;
}

如果你折叠块,它看起来真的很好

// match jth arc
if (j < Count)|...|
else // all arcs were matched|...|
0赞 Heedo Kim 9/5/2016 #9

这种风格怎么样?使用 // 注释来描述整个 if-else 语句, 和 /* */ 注释用于内部描述。我使用 comment 是为了不与 if-else 语句的内部注释混淆。/* */

    // Process1
    if (cond1-1) {
        /* Process1 > Process1-1 */
        Process1-1();
        // Process1-1 description...
        Process1-1();
        Process1-1();
        ... 

    } else if (cond1-2) {
        /* Process1 > Process1-2 */
        // Process1-2 description...
        Process1-2();
        Process1-2();
        Process1-2();
        ... 

        // Process1-2
        if (cond1-2-1) {
            /* Process1 > Process1-2 > Process1-2-1 */
            Process1-2-1();
            Process1-2-1();
            Process1-2-1();
            ...

        } else if (cond1-2-2) {
            /* Process1 > Process1-2 > Process1-2-2 */
            Process1-2-2();
            // Process1-2-2 description...
            Process1-2-2();
            // Process1-2-2 description...
            Process1-2-2();
            ...

        } else {
            /* Process1 > Process1-2 > Process1-2-else */
            Process1-2-else();
            Process1-2-else();
            Process1-2-else();
            ...
        }

    } else {
        /* Process1 > Process1-else */
        Process1-else();
        Process1-else();
        Process1-else();
        ...
    }
0赞 Sinus the Tentacular 9/25/2020 #10

这个怎么样?在 if 关键字后面添加注释。像自然语言一样可读,只为那些真正感兴趣的人留下可能复杂的条件代码。

    if /* user is logged in */ (user && user.loggedin()) {
        ...
    } else if /* user was logged in before */ (cookies.user && sizeof(cookies.user)>0 && cookies.user.value=="foobar" && some_other_things_in_a_long_condition) {
        ...
    } else /* apparently there's no user */ {
        ...
    }