Skip to content
mentoor

Topic 5 of 6 · 4 min read

Misleading comments and stale docstrings

Goal: Find comments in AI-written code that are wrong or noise and decide which to fix, delete or replace with a test.

A comment is a claim the compiler never checks, so a wrong comment is worse than none: readers trust it, and it sends them the wrong way. AI tools write a lot of comments, and they are not always true.

Check yourself · 1 of 3

A docstring says a function returns None when the id is missing, but the code raises an error. What is the first thing to decide?

Choose one answer

Why it matters

Agents comment fluently. They explain a line the way a tutorial would: "increment the counter by one". They write docstrings that sound complete. And when an agent edits code, it often updates the logic but not the comment above it, or the other way round. The result is a docstring that describes the first draft. A future developer, or another agent, reads that text as fact and builds on it.

Three kinds of bad comment

  1. Narration. It repeats what the code says: // loop over users. It adds length, not information. Delete it.
  2. Stale. It describes an earlier version: "returns null if not found" over a function that now throws. Fix it or remove it.
  3. Promise the code lacks. "Thread-safe", "validates input", "retries 3 times", when the code does none of that. This is the dangerous kind. It can make a reviewer skip checking the very thing the comment claims.

What a good comment does

It explains why, not what: a constraint, a trade-off, a surprising reason. "Round half up; finance reconciles against the bank's rounding." If the comment states behaviour the code must keep, the better home is a test. A test fails when the claim stops being true; a comment never does.

How to review them

For each comment, read the code beneath it and ask: is this true right now? Check the docstring's parameters, return value and error cases against the real function. If the comment is true but only restates the code, delete it. If it states a rule worth keeping, write a test and then delete or shorten the comment.

Before and after

def get_user(user_id):
    """Return the user, or None if not found. Cached for 5 minutes."""
    return db.users.get(user_id)  # raises UserNotFound

The docstring says None and caching; the code does neither. The fixed version:

def get_user(user_id):
    """Return the user. Raises UserNotFound if the id does not exist."""
    return db.users.get(user_id)

What goes wrong

  • Trusting the comment over the code. Verify the claim, then trust.
  • Asking an agent to "add comments everywhere". You get narration in bulk.
  • Fixing the comment but not the bug. If the comment describes the intended behaviour, the code may be what is wrong.

Examples

The promise that is not kept

/** Sends the email, retrying up to 3 times on failure. */
export async function sendEmail(msg: Message) {
  return smtp.send(msg);
}

There is no retry. Either add the retry with a test that fails twice then succeeds, or correct the comment. Do not leave the claim.

Comment to test

# Totals must never be negative above a calculation is a rule. Replace it with test_total_never_negative, and the rule is enforced instead of just stated.

Key points

  • A comment is an unchecked claim, so a wrong one misleads more than a missing one.
  • Narration that restates the code adds length and should be deleted.
  • A comment that promises behaviour the code lacks, such as retries or thread safety, is the most dangerous kind.
  • Good comments explain why; rules the code must keep belong in a test that fails when they break.
  • When a comment disagrees with the code, decide which one is wrong before fixing either.

Common mistakes

  • Trusting a docstring without reading the function it describes.
  • Asking an agent to "comment everything" and accepting the narration.
  • Updating a comment to match code that is actually the bug.
  • Leaving a comment about an old version of the logic after a rewrite.

Check yourself · 2 of 3

Which comment is the most dangerous to leave in AI-written code?

Choose one answer

Check yourself · 3 of 3

A comment says "totals must never be negative". What is a better home for that rule?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic