我从来没有决定评论if-then-else结构的最佳方式,所以我从来没有标准化以一致的方式来评论它们.我很欣赏任何见解.
一些选择:
一个)
if (blabla) {
// this comment explains what happens in the IF case
dothis();
} else {
// this comment explains what happens in the ELSE case
dosomethingelse();
}
Run Code Online (Sandbox Code Playgroud)
缺点:在多个dothis()语句的情况下,我喜欢评论主要块,在这种情况下,IF-comment是否属于第一个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();
}
Run Code Online (Sandbox Code Playgroud)
缺点:仅适用于简短评论.如果IF和ELSE情况没有直接从代码中清除,我通常会注释IF-THEN-ELSE结构,这通常需要长于一行的注释.
或c)
// if the following happens
if (blabla) { // then do this
dothis(); …Run Code Online (Sandbox Code Playgroud)