提问人:Rabarberski 提问时间:3/9/2009 最后编辑:FlipRabarberski 更新时间:9/25/2020 访问量:13338
在if-then-else结构中将注释放在什么位置?[已结束]
Where to put comments in an if-then-else construct? [closed]
问:
我从来没有决定过评论结构的最佳方式是什么,所以我从来没有标准化过一致的注释方式。
我很欣赏任何见解。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
IF
或下方块中的注释描述了评估条件并做出选择后会发生什么。IF
ELSE
所以像这样:
//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);
}
评论
我会做案例 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();
}
我猜,使用对你有意义的东西(除非你在指定评论风格的编码标准下工作)。就我个人而言,我不使用 (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();
}
等等。
我从来没有想过太多;就个人而言,在需要时,我会在 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 时代,尽管我确实更喜欢代码块的开始和结束的视觉理由,所以我的评论风格可能不适用于“封闭大括号样式社区”。
评论
就我个人而言,我发现最好编写不需要小注释的代码,比如“about do do x”,然后是“DoX()”。如有必要,与其写一条注释说“因为 y 而做 x”,我更愿意编写一个名为“DoXBecauseOfY”的方法。如果以后的重构删除了“BecauseOfY”部分,那么在 if 语句之前添加注释以记录整体逻辑仍然更有意义。
当然,您需要将每个分支中的代码量减少到可以一次读取整个 if 语句的程度。
// 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
...
...
}
来自代码约定的 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.
*/
}
只是为了为 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|...|
这种风格怎么样?使用 //
注释来描述整个 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();
...
}
这个怎么样?在 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 */ {
...
}
评论