General

How to Write Better Code Comments: A Practical Guide

2026-11-09 · 5 min · Lint Team

Why Most Code Comments Fail



Most developers have seen the classic bad comment:

``python
i += 1 # increment i by 1
`

This comment adds zero value because it restates exactly what the code does. It creates noise, doubles maintenance burden, and can drift out of sync. The goal of a comment is not to describe *what* the code does—the code already does that. The goal is to explain *why* the code exists, *why* a particular approach was chosen, or *what* assumptions are baked in.

When comments are done well, they act as a map for future maintainers (including your future self). When done poorly, they are at best clutter and at worst lies. This guide will walk through the principles of writing better code comments, common anti-patterns to avoid, and how AI tools—including Lint—can help you draft, refine, and proofread comments without over-automating.

The Golden Rule: Explain Why, Not What



Effective comments answer questions that cannot be answered by reading the code. Consider this example:

`javascript
// Good: explains why the regex is needed
// ISO 8601 date strings may include fractional seconds, which Date.parse ignores.
const dateStr = isoString.replace(/\.\d{3}Z$/, 'Z');
`

The code shows *what* is being replaced, but the comment tells you *why* the replacement matters. Without the comment, a developer might "simplify" the regex and break edge cases.

Another principle: comments should document surprises, constraints, and workarounds. For example:

`python
# Workaround for a bug in libfoo 2.3.1; remove after upgrade to 2.4
if version == '2.3.1':
...
`

This comment carries essential context that is not visible anywhere else.

Common Anti-Patterns to Avoid



Recognizing bad comments is the first step. Here are the most frequent offenders:

1. The Restatement Comment


`java
// Set age to 30
int age = 30;
`

2. The Journal Comment


`python
# 2024-03-01: changed loop to use range
# 2024-03-05: fixed off-by-one error
# 2024-03-10: refactored to list comprehension
`
Use version control for history, not code comments.

3. The Commented-Out Code Block


`javascript
// function oldLogin() {
// ...
// }
`
Delete dead code. If you need it later, that's what git history is for.

4. The Apology Comment


`csharp
// I know this is ugly, but it works for now
`
This signals that you should probably refactor instead of leaving a note.

5. The Misleading or Outdated Comment


`python
# Returns the user's first name
def get_user_full_name():
...
`
Comments that drift from the code are worse than no comment at all. They actively mislead.

Principles for Writing Clear Comments



Follow these guidelines to keep comments useful and maintainable:

- Keep comments close to the code they describe. Place the comment on the line above or inline immediately after the statement.
- Use sentence case and plain language. Comments are prose, not code. Write them for humans.
- Be concise. If a comment is longer than the code it explains, it's probably too verbose.
- Use consistent terminology. Match the language of the domain and the codebase.
- Update comments when you update code. A code review checklist should include "Are comments still accurate?"
- Avoid jargon unless your team shares it. What seems obvious today may be cryptic in six months.

When Not to Write a Comment



Sometimes the best comment is no comment. If you can make the code self-documenting through clear naming, small functions, and well-chosen abstractions, do that first. For example, instead of:

`python
# If the user is active and not deleted
if user.status == 'active' and user.deleted_at is None:
`

You could extract a method:

`python
if user.is_active():
`

Now the code reads like English, and no comment is needed.

Similarly, don't comment to compensate for bad code. If a block is confusing, consider refactoring before adding a paragraph of explanation. However, there are always edge cases—performance hacks, API quirks, regulatory requirements—where a comment is the right tool.

Using AI to Draft and Polish Comments



AI writing tools can help you generate, rephrase, and proofread code comments—but they are not a substitute for human judgment. The best approach is to write a rough comment yourself, then use AI to improve clarity, grammar, and tone. Or, if you're stuck, ask an AI assistant to suggest a comment for a tricky block, then review and adjust.

Here are some tools that developers commonly use for this purpose:

- GitHub Copilot / Copilot Chat: Can suggest comments based on the surrounding code. Useful for first drafts.
- ChatGPT / Claude: General-purpose assistants that can explain complex logic and draft comments in plain English.
- Lint: A grammar and clarity checker designed for developers. It proofreads comments, docstrings, and commit messages, catching typos, ambiguous wording, and style issues. For example, if you write a comment with passive voice or inconsistent terminology, Lint flags it and suggests a clearer alternative.
- ESLint / language-specific linters: Some have rules for comment formatting (e.g., require a space after
//), but they don't check prose quality.

The key is to treat AI-generated comments as a starting point. Always verify that the comment matches the code and adds value. A well-trained AI can produce fluent but subtly incorrect explanations, so human review is mandatory.

A Quick Comparison of AI Comment Tools



| Tool | Best For | Prose Quality Checks | Developer-Focused | Typical Use Case |
|------|----------|----------------------|-------------------|------------------|
| GitHub Copilot | Code-aware comment suggestions | Limited | Yes | Generate first-draft comments from code context |
| ChatGPT / Claude | Explaining complex logic in plain English | Strong general language skills | No (general-purpose) | Brainstorm why-comments and docstrings |
| Lint | Grammar, clarity, and tone in comments and docs | Strong (proofreading, style) | Yes (built for devs) | Polish existing comments, catch typos, enforce consistency |
| ESLint plugins | Enforcing comment formatting rules | No (formatting only) | Yes | Ensure consistent comment syntax |

Note: None of these tools should replace code review. They are aids, not authorities.

Putting It All Together: A Before/After Example



Let's transform a typical poorly commented function into a well-documented one.

Before (bad comments):

`javascript
function processData(items) {
// loop through items
for (let i = 0; i < items.length; i++) {
// get the item
const item = items[i];
// if valid
if (item.status === 'active') {
// do the processing
process(item);
}
}
}
`

Every comment restates the code. No added value.

After (improved):

`javascript
/**
* Processes only active items from the input array.
*
* Why: Inactive items are already handled by the nightly archive job,
* so processing them here would cause duplicate side effects.
*
* @param {Array<{status: string}>} items - Array of item objects.
*/
function processData(items) {
for (const item of items) {
if (item.status === 'active') {
process(item);
}
}
}
``

The comment now explains the *why* (avoid duplicate side effects) and documents the function's contract. Notice we also removed the inline restatements entirely and let the code speak.

If you're using Lint, you could paste this comment into the editor and it would suggest changes like "Active voice in 'Processes only active items' is good" or flag any grammar slips. That final polish ensures your comments read as professionally as your code.

Conclusion



Writing better code comments is a skill that pays off every time someone—including you—returns to a codebase months later. Focus on explaining *why*, avoid restating *what*, and know when a comment is unnecessary. Use AI tools like Lint, Copilot, or ChatGPT to help draft and refine comments, but always keep human judgment at the center. The goal is not to write more comments; it's to write comments that matter.

Check your own text — free



Run your next commit message or doc paragraph through the free Lint grammar checker. Code, commands, and identifiers stay exactly as written; only the English gets fixed.
📌 Prices and tool count in this article may be outdated. Lint now ships 16 tools with Pro at $12/mo, or $60/year. See the current pricing page.
Try Lint free — no signup, code-aware
Free · no account needed · open full toolkit →
L
Lint Team
Lint Tools — AI writing for developers

Stop letting Grammarly break your code

17 AI tools built for developers. Code-aware parsing.

Try free — no signup View pricing
Or upgrade to Pro for 500 checks a day · All tools · No ads

Need more than free tools?

Unlock 500 checks a day, larger text capacity, and priority support.

View pricing → Upgrade Lint — Pro $12/mo or $60/yr
💬