A long time ago, I worked for a company where the developer who had been there the longest didn’t write code comments. At all. Apparently, once you had occupied a chair for enough years, explaining yourself became something other people did.
One day, I asked him about it.
“John, how come you don’t write code comments?”
He said, “David, if you don’t understand what my code is doing by reading it, maybe you’re not smart enough to be working here.”
Rude, but whatevs.
I replied, “John, if you don’t understand that code comments are designed to explain why something is happening, then maybe you’re not smart enough to be working here.”
I stand by that response. Although these days I might put it in a comment so the next person could understand why John and I weren’t having lunch together.
I Can Read the Code, John
Let’s start with something simple:
z = x + y;I can see that we’re adding x and y and assigning the result to z. Thank you. My decades of software development experience have prepared me for this harrowing encounter with addition.
What I want to know is:
- What does
xrepresent? - What does
yrepresent? - Why are we adding them together?
- What are we going to do with
z?
Are these dollar amounts? Counts? Measurements? Are they in the same units? Is one of them a temporary adjustment introduced because a customer’s accounting system has been wrong since 2008 and everyone has quietly agreed to keep feeding it?
The plus sign is doing a magnificent job of explaining the addition. It is doing absolutely nothing to explain the decision. That information existed in somebody’s head when the code was written. If it never escaped that head, the rest of us are left performing forensic accounting on three letters and a semicolon.
While We’re Here, Those Variable Names Are Terrible
Unless we’re working with actual coordinates or a mathematical formula where those names make sense, x, y, and z are stupid variable names. There. We’ve saved everyone some time.
Let’s try this:
invoice_total = merchandise_subtotal + shipping_total;Already better. I know what the values represent. I know what the result represents. Nobody had to summon the author from a meeting... or the shallow grave I put them in.
Now compare it with this:
inv_tot = merch_sub + shp_tot;Why? What emergency required us to evacuate the vowels?
You have an editor that completes variable names. You have a keyboard with all twenty-six letters. You are probably sitting in front of several thousand dollars’ worth of equipment, connected to a global communications network.
Write “shipping.” We can afford it.
I have spent enough time staring at names like usr_acct_cfg to wonder whether the original developer was naming a variable or trying to cough something out of a lung.
And tmp? Temporary what? A temporary filename? A temporary balance? A temporary workaround that has now outlived two managers and the company’s original business model?
Calling something data isn’t much help either. Yes, it’s data. We’re in a computer program. That narrows it down about as much as labelling a container “matter.”
Use names that tell me what I’m looking at:
customer_account_settings = get_customer_account_settings();
shipping_quote_expires_at = dateAdd("n", 30, now());I’m fine with familiar abbreviations such as id, url, or html. I don’t need hypertext_markup_language_response_body turning every assignment into a municipal planning application. But if your abbreviation requires an explanation, consider spending those same keystrokes on the actual word. Typing is cheap. Deciphering your private dialect is expensive.
Better Names Still Don’t Explain the Whole Decision
Descriptive names solve part of the problem. They don’t magically preserve the conversation that led to the code. Consider this hypothetical example:
shipping_quote_expires_at = dateAdd("n", 30, now());I can read that perfectly well. However... Why thirty minutes? Was that a product decision? A provider requirement? A number someone selected because lunch arrived?
Now give me this:
// The carrier only guarantees a quoted rate for 30 minutes.
// After that, request a fresh quote before allowing checkout.
shipping_quote_expires_at = dateAdd("n", 30, now());That comment gives the next developer something useful: a reason to preserve the limit, and a clue about what must happen when it expires. Without it, someone might decide that thirty minutes feels inconvenient and change it to twenty-four hours. The code would still run. The variable name would still be excellent. The business would simply start paying for someone’s thoughtful improvement.
This is the context I want comments to preserve. External constraints. Business rules. Unusual ordering requirements. Reasons we deliberately avoided the obvious approach. Especially the reasons we avoided the obvious approach. Because someone will eventually notice it, and they will feel very clever.
Please Don’t Narrate the Semicolon
None of this means I want a comment above every line.
// Add shipping to the merchandise subtotal.
invoice_total = merchandise_subtotal + shipping_total;
I don't need comments like this. Yes. It's a cat. I get it. I appreciate the enthusiasm, but I was there for the addition.
Also, meet Shay. Isn't she adorable?
A useful comment earns its space:
// The legacy export expects shipping in the invoice total,
// even though the checkout screen displays it separately.
invoice_total = merchandise_subtotal + shipping_total;Now I understand why this calculation exists in this particular form. I also know that changing it may affect something outside the function I’m currently reading. That is useful.
Likewise, this deserves a little more effort:
// Do not remove. Things break.Which things? Under what conditions? What happened when someone tried? You’ve left me a warning sign with the word “BEWARE” on it and no indication whether I’m dealing with a wet floor or an escaped rhinoceros.
Explain the failure. Link the relevant issue when there is one. Leave enough context that the next developer can make a decision without rebuilding the entire incident from witness testimony. And when the behaviour changes, update the comment. An outdated comment can confidently guide someone into doing the wrong thing. We already have enough meetings for that.
I Used to Want My Code to Be Beautiful
Earlier in my career, I put considerable effort into crafting elegant solutions. I wanted the neat abstraction. The beautifully compact function. The clever expression that handled six cases in one line and made me feel briefly qualified to own a black turtleneck.
Sometimes that effort produced good software. Sometimes it produced a little intellectual obstacle course that I would later encounter with absolutely no memory of building it. There is a particular horror in opening a file, wondering what kind of lunatic wrote it, and then checking the history.
Oh. That lunatic has my email address.
These days, I write code so future me can figure it out easily. Future me will have forgotten the meeting. He will have worked on seventeen other things. He may be tired, distracted, or trying to fix a production issue while someone asks whether we have an estimated resolution time yet.
Future me does not need a puzzle. He needs clear names, visible steps, and an explanation of the bit that looks wrong but exists for a reason.
I still appreciate elegance. A solution that expresses a complicated idea clearly is a beautiful thing. But I have become deeply suspicious of elegance that requires the author to stand beside the reader and explain how impressive it is.
If separating an expression into three named steps makes it easier to understand, I’ll take the three steps.
If a short comment saves someone an hour of archaeology, I’ll write the comment.
The code review is unlikely to end with someone presenting me an award for Fewest Lines Used Before Lunch. I can live with that.
Your Memory Is Not Part of the Repository
When you write a piece of code, you have an enormous advantage over everyone who reads it later. You remember what you were trying to accomplish. You remember the rejected approach. The broken integration. The customer requirement that sounds unreasonable until someone explains the contract. All of that makes the code feel obvious.
Then time passes.
The code stays exactly where you left it. The context evaporates. That happens to experienced developers too. Intelligence does not grant permanent recall of a Thursday afternoon six years ago. Expecting the next person to reconstruct your reasoning from the finished implementation is a remarkable way to spend someone else’s salary.
Give them meaningful names. Keep the logic readable. Preserve the decisions they cannot recover by staring harder at the syntax.
John thought the ability to understand his code was a test of whether someone deserved to work there.
I think leaving useful context is part of doing the job.
Eventually, John leaves. Or goes on holiday. Or forgets. Or I take him to a farm upstate. Eventually, somebody has to change the thing he was so certain explained itself. I’d like that person to have a fighting chance.
Especially when that person is me.
Write the damn comment.
