...
Code Block | ||
---|---|---|
| ||
// */ /* commentComment, not syntax error */ f = g/**//h; /* equivalentEquivalent to f = g / h; */ /*//*/ l(); /* equivalentEquivalent to l(); */ m = n//**/o + p; /* equivalentEquivalent to m = n + p; */ a = b //*divisor:*/c + d; /* equivalentEquivalent to a = b + d; */ |
Compliant Solution
Use a consistent style of commenting:
Code Block | ||
---|---|---|
| ||
// Nice simple comment int i; // counterCounter |
Noncompliant Code Example
There are other misuses of comments that should be avoided. The following noncompliant example uses the character sequence /*
within a to begin a comment but neglects to use the delimiter */
to end the comment. Consequently, the call to the security-critical method is not executed. A reviewer examining this page could incorrectly assume that the code is executed.
Code Block | ||
---|---|---|
| ||
/* commentComment with end comment marker unintentionally omitted security_critical_method(); /* someSome other comment */ |
Using an editor that provides syntax highlighting or that formats the code to identify issues like missing end comment delimiters can help detect accidental omissions.
...
This compliant solution takes advantage of the compiler's ability to remove unreachable (dead) code. The code inside the if
block must remain acceptable to the compiler. If other parts of the program later change later in a way that would cause syntax errors, the unexecuted code must be brought up to date to correct the problem. Then, if it is needed again in the future, all the programmer must do is remove the surrounding if
statement and the NOTREACHED
comment.
...
Code Block | ||
---|---|---|
| ||
if (false) { /* useUse of critical security method no * longer necessary, for now */ /*NOTREACHED*/ security_critical_method(); /* someSome other comment */ } |
This is an instance of an exceptional situation described in MSC57-JG. Detect and remove superfluous code and values.
...
[Reddy 2000] | Java Coding Style Guide Section 5, §5.0, "Comments" |
Bibliography
...