Skip to content

Explaining Comments: When a Comment Covers for Unclear Code

How to spot comments that narrate what confusing code does, and how to refactor the code so the comment becomes unnecessary.

7 min readCode smellDead weight

An explaining comment translates the code beside it into plain English: // h = hours left until the class starts, // make sure we never return a negative number. The comment exists because the code cannot speak for itself. A short variable name, an unnamed formula or a tangled condition forced someone to add a caption. The comment is not the problem. It is the symptom, and it is a fragile one. Code gets tested and run. Comments do neither. The first time someone changes the rule and forgets the caption, the comment starts lying. Usually the fix is to move the explanation into names: a variable, a function or a type that says what the comment said.

Signs

How to spot it in a review.

  • A comment explains what a variable holds because its name is one or two letters, like h, r or tmp.
  • A comment introduces a block of code, and that block could become a function named after the comment.
  • A comment restates a boolean condition in words because the condition itself is hard to read.
  • Magic numbers are explained inline (1 credit = 500 cents) instead of being named constants or types.
  • Comments and code disagree after a change, and reviewers have to work out which one is right.

What it looks like

A typical case, in three languages.

type MemberTier = "basic" | "plus" | "founder";

interface Booking {
  pricePaidCents: number;
  classStartsAt: Date;
  memberTier: MemberTier;
  creditsUsed: number;
}

export function calcRefund(b: Booking, now: Date): number {
  // h = hours left until the class starts
  const h = (b.classStartsAt.getTime() - now.getTime()) / 3_600_000;

  // founders always get everything back, and so does anyone
  // cancelling 24 hours or more ahead
  if (b.memberTier === "founder" || h >= 24) {
    return b.pricePaidCents;
  }

  // inside 24h: plus members get 50%, basic members get 50% only if
  // more than 6h are left. Credits are never refunded in this window,
  // so subtract their value first (1 credit = 500 cents)
  let r = 0;
  if (b.memberTier === "plus" || h > 6) {
    r = Math.floor((b.pricePaidCents - b.creditsUsed * 500) / 2);
  }

  // make sure we never return a negative number
  return Math.max(r, 0);
}

This is the cancellation-refund rule for Pulsehall, a fitness studio booking app. Every comment narrates something the code could say itself. Rename h to hoursUntilClass and r to refundCents. Name 500 as CREDIT_VALUE_CENTS. Extract isFullRefund(booking, hoursUntilClass) and qualifiesForHalfRefund(...) from the two conditions. Extract refundableCashCents(booking) for the credit deduction. Then rename calcRefund to cancellationRefundCents. Once that is done, no comment is left that adds information.

Why it happens

How code ends up like this.

  • Comments are not executed or tested, so they drift out of date when the rule changes. A refund policy that moves from 6 to 12 hours is easy to change in code and easy to forget in the caption.
  • A comment that explains a block tells you a function is missing. While the block stays inline, it cannot be tested on its own, reused or understood without reading the whole method.
  • Readers end up reading everything twice, once as prose and once as code, and then checking that the two agree. That doubles the review cost and lets mistakes hide in the gap between them.
  • Explaining comments can make a messy function look acceptable, so it keeps growing. Each new rule arrives with its own caption instead of its own name.

Treatment

The refactorings that cure it, and when to choose each.

Payoff

What you get once it's gone.

  • The rule is stated once, in code that runs, so it cannot silently disagree with its own documentation.
  • Extracted predicates and calculations can be unit-tested directly, for example the 6-hour boundary for basic members.
  • Reviewers and interviewers can read the top-level function as a sentence and only drill into helpers when they need detail.
  • The comments that remain carry real information, such as why a rule exists, so people read them instead of skimming past.

When to leave it

Sometimes it's not worth fixing.

  • Comments that explain why rather than what: a legal requirement, a link to a policy decision, or the reason an obvious approach was rejected. Code cannot express intent like that.
  • Workarounds for bugs in a library, driver or browser, where the code looks wrong on purpose. Keep the comment and say which version it applies to.
  • Public API documentation such as JSDoc, docstrings or PHPDoc that tools and IDEs consume. It describes the contract for callers, not the implementation.
  • Non-obvious algorithms or maths where a short reference, invariant or complexity note helps more than any name could, such as the invariant of a hand-written binary search.

Common questions

Short answers to the usual doubts.

Does this smell mean I should delete all comments?

No. It targets comments that paraphrase code. Before you delete one, ask whether a better name or an extracted function could say the same thing. If it could, do the refactoring and the comment becomes redundant. If the comment records a reason, a constraint or a warning that the code cannot express, keep it and keep it accurate.

How do I tell an explaining comment from a useful one quickly?

Cover the code and read only the comment. If it just describes the mechanics, such as 'loop over items and add the price', it is explaining. If it tells you something you could not recover from perfectly clear code, such as 'finance requires rounding down here, see the 2023 audit', it is useful.

Won't extracting lots of tiny functions make the code harder to follow?

Only if the names are poor. A function called qualifiesForHalfRefund is faster to understand than a three-line comment plus a compound condition, and good editors let you jump to the definition. If a helper's name is no clearer than its body, inline it back.

How should I bring this up in a code review interview?

Point to one specific comment, name the refactoring that would replace it, and show the result. For example: rename h to hoursUntilClass, extract the founder || hours >= 24 check into isFullRefund, then delete the comments. Then mention which comments you would keep and why. That shows you are making a judgement call, not following a rule mechanically.

Are commented-out blocks of code the same smell?

They are related dead weight but not the same thing. Commented-out code is unused code kept just in case. Delete it, because version control already keeps the history. Explaining comments sit on top of live code and point to a naming or structure problem.