Every line you write will one day be read by someone who was not in the room when you wrote it. It might be a new starter in their second week, or an agent with a ticket number and nothing else. I have inherited a lot of code, and the developers I most enjoyed inheriting from had one habit in common. Their comments told me the things I could not have worked out from the code.
The other kind of codebase is more familiar. You open a class to change one line, and the comment above it says // add one to the counter. The line reads i += 2. Somebody was told to document their code and did so faithfully, and then somebody else changed the code. A comment that restates the code is a second copy of the code, maintained by hand, and the two copies rarely stay in step for long.
Everyone I have worked with agrees that the comment that matters most is the ‘why’: why the line exists and what breaks if it goes. Almost nobody writes it. They write the ‘what’ instead, and they have good reasons for it.
Why the ‘what’ gets written
Picture the moment the comment gets written. The code works and the pull request is half drafted. There is a blank line above the method, and the developer has two things they could put in it, only one of which is in front of them.
The ‘what’ is on the screen while they type, and describing it costs nothing. The ‘why’ was in a design review an hour ago, or on a vendor call nobody minuted. With the context still fresh, the decision feels too obvious to write down.
The standards push the same way. Most coding standards I have inherited say “comment your code” without saying which half, so developers comment the half in front of them and the standard is met. Nobody checks which half got written, because until someone needs the reason, the two look much the same.
What the missing ‘why’ costs
The need arrives later, and usually for someone else. Every codebase has lines nobody dares touch. Someone once said “don’t change that”, nobody wrote down why, and the next developer sensibly leaves the line alone and builds around it. So does the one after. Each of those lines is cheap on its own. A few hundred of them and the org can only be added to.
I have watched the more expensive version happen more than once. A developer needs to change one small block inside a class they do not understand, and nobody can tell them what the surrounding lines are for. The safe move, from where they stand, is to copy the whole class, hundreds of lines of it, change the one block in the copy, and wire the copy in. Now the org has two near-identical classes, and every future fix has to land in both. The decision was sensible on the day.
Coding agents write the ‘what’ by default
The default is about to get harder to escape. Ask a coding agent for a service method and you get a header restating its name and a comment on every branch describing the condition. I have seen one add a closing line noting that the method returns. All of it passes a “comment your code” standard, and none of it says anything the code does not. Worse, a reviewer trained by forty ‘what’ comments to skip the grey text will skip the one ‘why’ comment sitting among them that matters.
The agent is also the newest reader, and it reads comments as context. A ‘why’ comment next to a limit changes what the agent does when asked to raise it. Without one it raises it. With one, in my experience, it usually pushes back: “this is capped for the vendor timeout, do you want to confirm with them first”.
Generated code also has no author to ask. When an agent wrote a skip for contacts with no email because the prompt said they came from the 2022 migration, the ‘why’ lived in the prompt. The prompt ends up in a session transcript nobody will open again.
The argument for writing less
Good names already carry most of the ‘what’. selectOpenCasesWithAccountById needs no comment saying it selects open cases with an account by Id.
Tests can carry some of the ‘why’. Where a constraint can be asserted, a unit test does better than a comment. A test like batchSizeStaysUnderVendorLimit fails the moment someone raises a batch size past the vendor’s limit of 60, and no comment can do that.
The ‘why’ is also supposed to be in the ticket. Every change has one, and the pull request links to it. The commit message is an even better home, because it is pinned to the exact diff and git blame will find it. Writing the reason again in the code looks like duplication, which is the problem this post started with.
Comments rot as well, good ones included. I have one in production that explains a requery working around a platform bug. The bug was fixed a couple of releases later, the requery is still there, and so is the comment, patiently explaining a problem that no longer exists.
And some readers really do need the ‘what’. A class header that says which layer it sits in and which object it owns saves a reviewer opening three files. The description on an invocable method is read by an admin in Flow Builder who will never see the Apex. A field description is read by an agent and by whoever builds the next report. A regular expression or a date calculation is faster to read as a sentence than as itself.
All of that is true. So the ‘what’ keeps a job, and the ‘why’ has several other places it could live.
What the comment is for
John Ousterhout’s test, from A Philosophy of Software Design, is that a comment should describe what is not obvious from the code. That covers a good ‘what’ as well: a summary of a dense method, or a description for someone who will never open the class. The ‘what’ can be very important at times. Usually, though, it is the ‘why’ that carries the weight, because it is the part that gets skipped and the only part nobody can work out later.
The reason lives next to the line, as the decision and its cause in a sentence or two, with the ticket beside it. A ticket number on its own is not a ‘why’, and I say that as someone who has followed a lot of them. Plenty of the links go to a tool that was decommissioned or a page you no longer have access to. The ones still open were written for a delivery team that has since dispersed, and they only make sense if you were in the room. If the link is dead by the time someone follows it, the comment has already told them what they needed.
The commit message is a better home than the ticket, because it travels with the repository. But it is only read by someone who goes looking. And git blame shows the last commit to touch the line, which after a formatter run or a squash merge is rarely the one that explains it. The comment is the only ‘why’ a reader meets without going looking.
A decision big enough to have its own record, such as which integration pattern to use or whether to build custom, gets a key design decision record in our org. The comment carries the decision and its cause, and points at the record by link or number.
A date and a ticket are also the answer to rot. A ‘why’ comment that carries both rots visibly, because the reader can see how old it is and where to check. My requery comment is wrong, but the next person will at least know what they are deleting. A ‘what’ comment that has drifted just lies, with the same confidence it had on day one.
The same line, two comments
Each pair below is the same line twice: first with the comment most developers write, then with what the next developer needed to know.
// Skip contacts with no email
if (String.isBlank(con.Email)) { continue; }
// Skip, do not addError (NotificationService runs from the Contact trigger handler).
// Contacts came from the 2022 CRM migration with no email and the business declined to backfill them
// (DEC-0087). Erroring here blocked every Apex and Flow bulk update that touched one of those records.
// If the backfill ever happens, this becomes an addError.
if (String.isBlank(con.Email)) { continue; }
A reviewer reading the first version will ask why it does not call addError. The second has already answered.
// Update cases, allow partial success
Database.SaveResult[] results = Database.update(cases, false);
// Partial success on purpose. This runs from the nightly service-level batch across ~40k cases, and
// one record locked by the overnight integration (UNABLE_TO_LOCK_ROW) used to fail the whole
// 200-record scope (INC-1874). Failures are written to Batch_Error__c below, and the 2am retry
// picks them up. Do not switch to allOrNone=true.
Database.SaveResult[] results = Database.update(cases, false);
Every allOrNone = false is a decision to let some records fail quietly, and nothing on the line tells a reviewer whether that was chosen or copied from the class above it.
private static final Integer BATCH_SIZE = 50; // batch size
// Vendor limit. The address validation API times out above roughly 60 records per call
// (INC-2231, March 2025). Raise it only after the vendor confirms the fix.
private static final Integer BATCH_SIZE = 50;
// Publish an event for each closed case
EventBus.publish(closedCaseEvents);
// Platform event, not a Queueable callout. The Queueable was tried first (PR-2210) and replaced after
// a warehouse outage left failed jobs with nothing to replay them, and the contact centre re-closed
// cases by hand.
// The warehouse middleware subscribes and replays from its last replay ID after an outage.
// Events are kept for 72 hours, so an outage longer than that still needs the reconciliation report.
EventBus.publish(closedCaseEvents);
The next developer who wants to “just call the API directly” now knows they are re-opening PR-2210.
Flow has the same gap, in the element description.
Element: Update_Case_Priority
Description: Updates the case priority.
Element: Update_Case_Priority
Description: Sets Priority to High for Platinum accounts only. Gold was included until August 2024
and pulled because it doubled the escalation queue (DEC-0112). Do not widen without Service Ops.
The first description is the element name with spaces in it. An admin who opens this flow in three years will know why Gold is missing.
The commit message has the same gap, and it is the ‘why’ most often written as a ‘what’.
Fix contact notification bug
Skip contacts with no email in NotificationService
Contacts from the 2022 CRM migration have no email and the business declined
to backfill (DEC-0087). The addError here was failing Apex and Flow bulk updates that touched
those records. Skip them instead. Fixes INC-0931.
None of these took more than a couple of minutes to write. Getting them written is the hard part.
Getting the ‘why’ written
A reviewer can tell padding from a comment worth keeping with two tests. The first is the deletion test. Remove the comment, and if the code still explains itself, the comment was only restating it and can stay deleted. A comment like // loop through the cases fails it on every line it has ever been written above. The second is the change test. Imagine changing the line, and if the comment is the only thing telling you that would be unsafe, it is a ‘why’ and it stays. “Raise this above 60 and the vendor times out” passes. “Batch size” does not. Where the constraint can be asserted, write the unit test as well. The comment keeps what the test cannot say: who set the limit and what would lift it.
The person who made the decision writes it, at the moment they make it, because a week later it has become “that’s just how it’s done here”. So the prompt to write it sits where the decision happens. The pull request template asks what was rejected and why, and the design review produces the one-line reason that becomes the comment. When a reviewer hits a surprising line, they ask where the ‘why’ is, and if the answer is a link, the reason itself still goes above the line. With a coding agent, the ‘why’ goes in from the prompt, because that is where it was. If you asked for the skip because the migrated contacts have no email, tell the agent to say so above the line. Strip the ‘what’ comments that only restate the line before review.
A lint rule can catch some of the gaps, because what it is looking for is an absence. It can flag a WITH SYSTEM_MODE or AccessLevel.SYSTEM_MODE with no comment beside it, or a numeric literal in a LIMIT. It can also flag a literal false passed to Database.update, or a // TODO with no ticket number. Each one marks a place where a ‘why’ almost certainly belongs, and the rule costs an afternoon.
If you want to know whether your org has the problem, take the last five pull requests and sort every comment into ‘what’ or ‘why’. Most lines do not need a ‘why’, so the ratio will be low, and that is fine. The trouble is that the lines that need one look exactly like the lines that do not. Then pick the strangest line you found and run git blame on it. If the ‘why’ is anywhere on the trail from commit to pull request to ticket, copy it above the line. If it is not, ask whoever wrote it, and put their answer above the line before they move on.
Every copied class I have come across started as a line somebody was afraid to change. The comment was always the cheaper option. It just never looked urgent.