Commented Out Code

ID

go.commented_out_code

Severity

info

Remediation Complexity

medium

Remediation Risk

low

Remediation Effort

medium

Resource

Code Smell

Language

Go

Tags

code-style

Description

Reports blocks of commented-out Go code. A run of three or more // comment lines, or a block comment spanning three or more lines, is flagged when its text parses as Go code rather than as prose.

Rationale

Commented-out code rots: it drifts out of sync with the surrounding code, confuses readers about whether it still matters, and clutters the file. Version control already preserves deleted code, so disabled code should be removed rather than left behind as a comment. Because the Go parser accepts ordinary prose as a run of bare identifiers, the rule distinguishes code from prose by requiring the parsed text to contain a real construct (a declaration, an assignment, a call, a binary expression, or a control-flow statement); plain explanatory comments are left alone.

func compute(value int) int {
    // The lines below explain the algorithm in plain English
    // and describe the trade-offs that were considered here.
    // They are prose, not code, so they are not reported.

    // value = value * 2                  // FLAW
    // value = value + 1
    // return value

    return value
}

Remediation

Delete the commented-out code. If the code may be needed again, rely on version control history to recover it instead of keeping it as a comment.

References