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
- A code sample for an article or an answer, where the text around it already explains the idea.
- Temporary notes and debug comments left over from development.
- Comparing two versions where only the logic matters, not the notes.
Comments that should stay
- License and copyright headers. They must stay in any copy you share.
- Comments that explain why. A workaround, a strict validation rule or a high timeout often makes sense only with its comment.
- Documentation comments such as
///in C# or/** */in Java, which feed editor tooltips and API docs. - Directives that only look like comments, such as
// eslint-disable-next-lineor#!/usr/bin/env python3. The comment syntax reference lists the common ones.
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);
}
}
- The copyright line is a license header. Keep it.
- The
///summary is a documentation comment. Keep it in production code. - The TODO is a temporary note. Remove it, and the debug line it refers to. A comment remover only removes the note, so the debug line has to go by hand.
- The VAT comment explains a business rule and a technical choice. It is the most valuable comment in the file. Keep it.
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
- Keep the original. Never clean the only copy.
- Run the cleanup with the preset for your language.
- Put the original and the result in Compare Codes. Every deleted or changed row should be a comment.
- 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.
- Restore license headers, warnings and directives that should stay.