How to remove comments from code safely

Removing comments makes a snippet shorter and easier to scan. But some comments carry knowledge the code cannot, and some are instructions to a tool. The skill is knowing which is which.

When removing comments helps

Comments that should stay

Worked example: four kinds of comment

This small C# class has four comments, and each needs a different decision:

// Copyright (c) 2026 Example Ltd. Licensed under the MIT License.
using System;

public static class PriceCalculator
{
    /// <summary>Returns the price including VAT, rounded to 2 decimals.</summary>
    public static decimal WithVat(decimal net)
    {
        // TODO: remove after testing
        Console.WriteLine("debug: " + net);

        // Saudi VAT is 15%. Keep decimal to avoid rounding errors on invoices.
        return Math.Round(net * 1.15m, 2, MidpointRounding.AwayFromZero);
    }
}

Remove Comments with the C-like preset removes all four comments and keeps the debug line:

using System;

public static class PriceCalculator
{
    public static decimal WithVat(decimal net)
    {
        Console.WriteLine("debug: " + net);

        return Math.Round(net * 1.15m, 2, MidpointRounding.AwayFromZero);
    }
}

That is fine for a short example in an article. For the real file, the right result keeps three comments and removes one comment plus one line of code:

// Copyright (c) 2026 Example Ltd. Licensed under the MIT License.
using System;

public static class PriceCalculator
{
    /// <summary>Returns the price including VAT, rounded to 2 decimals.</summary>
    public static decimal WithVat(decimal net)
    {
        // Saudi VAT is 15%. Keep decimal to avoid rounding errors on invoices.
        return Math.Round(net * 1.15m, 2, MidpointRounding.AwayFromZero);
    }
}

Automatic removal works well for snippets you are about to explain in your own words. For files that stay in a project, use it as a first pass, then restore the comments that carry knowledge.

Safe cleanup workflow

  1. Keep the original. Never clean the only copy.
  2. Run the cleanup with the preset for your language.
  3. Put the original and the result in Compare Codes. Every deleted or changed row should be a comment.
  4. Build or run the cleaned code. A syntax error right after cleanup almost always points to a string or regular expression that was misread. The edge cases guide shows the usual suspects.
  5. Restore license headers, warnings and directives that should stay.