Commented Out Code

ID

csharp.commented_out_code

Severity

low

Remediation Complexity

medium

Remediation Risk

low

Remediation Effort

medium

Resource

Code Smell

Language

CSharp

Tags

code-style

Description

Reports blocks of commented-out C# 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 C# code rather than as prose.

Units carrying a generated-code marker (// <auto-generated> header or [GeneratedCode] attribute) are skipped.

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. The rule distinguishes code from prose by attempting to parse the stripped comment text, so ordinary explanatory comments are left alone.

public class Sample
{
    public int Compute(int value)
    {
        // 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