November 26, 20269 minutes
A payroll comment says overtime is paid at 1.5x while the code says 1.25. Learn to replace what-comments with code that explains itself, and to keep the why-comments that are worth keeping.
Here’s the pay method from a payroll system. Every few lines, a comment explains the next step. One comment says overtime is paid at one and a half times. But the code right under it multiplies by 1.25. Last year, someone changed the code and forgot the comment. So which one is right? You can’t tell by reading. That’s a smell called Comments: notes that explain unclear code.
Prefer to watch? The narrated video covers the same lesson (about 8 minutes). The article follows it section by section, with diagrams and a little extra depth.
Here’s the method. Look at the overtime comment and the line under it:
double pay(Employee e) {
// hours worked this period, never negative
double hours = e.hours();
// regular pay: up to 40 hours at the base rate
double pay = Math.min(hours, 40) * e.rate();
// check overtime: over 40 hours, 1.5x
if (hours > 40) {
pay += (hours - 40) * e.rate() * 1.25;
}
// subtract unpaid leave
pay -= e.unpaidDays() * 8 * e.rate();
// never pay less than zero
if (pay < 0) {
pay = 0;
}
// round to whole cents
return Math.round(pay * 100) / 100.0;
}The comment says 1.5x. The code says 1.25. (The rates, hours and ticket numbers in this article are
made-up example values; Employee stands for a small class with hours(), rate() and
unpaidDays().)
Think of a door with a pull handle, and a sign taped on that says “PUSH”. Every visitor reads the sign, tries to push, and bumps into the door. The sign isn’t helping. It’s covering for a bad door. The fix isn’t a bigger sign. It’s a door shaped so that nobody needs to read anything.
A comment that explains what code does is that sign. If the code needs a sign, fix the code.
A sign that says why the door is locked is different, say “fire exit”. That tells you something the door can’t.
Let’s read it fairly. Someone took the time to explain every step, and that’s kind. The method goes step by step: regular pay, overtime, unpaid leave, a floor at zero, and rounding. A newcomer can follow it without reading any code. And that’s the trouble: the comments are doing the code’s job. The course reference page puts it this way: if the code needs a comment to be understood, that is a hint that the code isn’t clear enough on its own. And in a very long method, comments feel like the only map you have, which is why they spread (see Long Method).
Nothing checks a comment. The compiler ignores it, and so do your tests.
Code changes, and comments stay put. That’s how the overtime comment ended up saying one and a half while the code says one and a quarter. Now every reader has to choose. Trust the comment, and you misquote the policy. Trust the code, and you wonder whether it’s a bug.
Comments double the reading. You read the comment, then the code, then decide whether they agree.
A what-comment is a deodorant. It covers the smell and doesn’t remove it. (refactoring.guru uses the same image: comments written with good intentions can mask code that could be improved.)
Every one of those comments explains what. Keep that word in mind. Its partner, why, comes next.
A comment can answer two questions: what does this code do, and why is it this way?
A what comment is a failure of the code. The name, the structure or the types should already say it. A why comment is a gift. It holds a business reason, a workaround, or a surprise that no code can express. So when you want to write a what comment, try this first: change the code until the comment isn’t needed.
Don’t explain the code. Let the code explain itself, and keep the why.
Start with Extract Method. Take the commented block, move it into a method, and use the comment
as the name. Overtime becomes overtimePay. Regular pay and rounding get names too. Unpaid leave
becomes a variable called unpaidLeave. That last move is Extract Variable (Fowler’s older name
is Introduce Explaining Variable).
The magic numbers get names too: forty hours is REGULAR_HOURS, and the multiplier is a constant, so
the number lives in one place. And the comment about hours never being negative becomes an assertion,
Introduce Assertion:
class Payroll {
private static final int REGULAR_HOURS = 40;
private static final int HOURS_PER_DAY = 8;
// Reduced by the pay policy change (ticket HR-1187).
private static final double OVERTIME_MULTIPLIER = 1.25;
double pay(Employee e) {
assert e.hours() >= 0 : "hours are never negative";
double unpaidLeave = e.unpaidDays() * HOURS_PER_DAY * e.rate();
double total = regularPay(e) + overtimePay(e) - unpaidLeave;
return roundToCents(Math.max(total, 0));
}The methods behind the names:
private double regularPay(Employee e) {
return Math.min(e.hours(), REGULAR_HOURS) * e.rate();
}
private double overtimePay(Employee e) {
double extraHours = Math.max(e.hours() - REGULAR_HOURS, 0);
return extraHours * e.rate() * OVERTIME_MULTIPLIER;
}
private double roundToCents(double amount) {
return Math.round(amount * 100) / 100.0;
}
}Assertions are switched on with -ea (they are off by default), and they suit assumptions in
non-public methods. Don’t use them to check the arguments of public methods; there, throw an
exception, as the second example shows (
Oracle’s assertion guide).
Take small steps. Extract one comment at a time, and run the tests after each one. The course page lists Extract Method (for a commented block) and Introduce Assertion (for a stated assumption); refactoring.guru adds Extract Variable (for a complex expression) and Rename Method (for a method that still needs a comment).
Now replay the question: which multiplier is right? There’s one place to look, and the code uses what it finds there.
But why did it drop from the old rate? The code can’t say that. So one comment stays, with its ticket:
the line above OVERTIME_MULTIPLIER. Notice it repeats no number. A why comment that never repeats the
code can’t contradict it. And it points at a ticket, so anyone can open it and judge whether the
reason still holds.
A smaller case. A discount stores a value called p, and a comment says it’s the percent off,
between 0 and 100:
class Discount {
private final double p; // percent off, between 0 and 100
Discount(double p) {
this.p = p;
}
// price after the discount
double calc(double price) {
return price - price * p / 100;
}
}That comment does two jobs. It’s a footnote for a bad name, and it states a rule that nothing checks.
Rename p to percentOff, and the footnote has nothing left to say. Rename calc to priceAfter,
and its comment goes too. In Fowler’s catalog those are Rename Variable and Change Function
Declaration (also called Rename Method). The rule becomes a real check:
class Discount {
private final double percentOff;
Discount(double percentOff) {
if (percentOff < 0 || percentOff > 100) {
throw new IllegalArgumentException(
"percentOff must be 0 to 100");
}
this.percentOff = percentOff;
}
double priceAfter(double price) {
return price - price * percentOff / 100;
}
}It’s an exception here, not an assertion, because this constructor is public and assertions can be switched off. (The course page’s own example for Introduce Assertion is a constructor comment that says RGB values must be in range while nothing enforces it. Either way, the stated rule becomes something the program checks.)
Not every comment is a smell. Keep four kinds:
// Copyright (c) 2026 Acme Payroll. All rights reserved.
/** Pays one employee for one period. The result is never negative. */
public double pay(Employee e) {
// Finance rounds down here, by rule (ticket FIN-212).
double paid = Math.floor(e.gross() * 100) / 100;
// TODO(HR-1190): add bonus days once the rules are approved.
return Math.max(paid, 0);
}(The ticket numbers are made-up. The four-kinds list is our own judgment, consistent with the course page’s when-to-ignore list.)
Myth: good code has no comments. Not quite. The aim is no what comments. Why comments are valuable. A codebase with no why comments loses its reasons, and then the next person removes your workaround, because it looks wrong.
So leave a comment alone when it is:
One exception: commented-out code. Delete it. That’s what Dead Code is.
A comment that says what the code does is a deodorant for unclear code. Spot it by restated lines, headings, footnotes and unchecked rules. Fix it with Extract Method, Rename, Extract Variable and Introduce Assertion. A long method is where what-comments gather, so fix that first. And keep the why.
Checklist:
Don’t explain the code. Let the code explain itself, and keep the why.