Blog / Build with AI /

Commenting code for your future self

Open a file you wrote six months ago. There’s a function called process. It takes three arguments, loops over something, and returns a number. You have no idea why.

Your past self knew. They were sharp, focused, full of context. Then they moved on and left you a puzzle.

Comments are the note you leave for that stranger, who is you. If you build things with AI, they matter even more: you didn’t write every line, so you need an explanation of what each part is for. Here’s how to do comments well, and how to make your AI tool help instead of cluttering.

What a comment is for

A comment is text in the code that the computer ignores. People read it. Its job is to answer the questions the code can’t.

The code tells you what happens. A good comment tells you why.

Compare:

// add 1 to count
count = count + 1;

That comment repeats the line. It’s noise. Now:

// The API counts pages from 0, but our users see them from 1.
page = page + 1;

That one answers a question the reader would have had. It saves ten minutes of confusion.

What to comment

1. The reason behind a decision

Why did you choose this approach? Why this number? Why this order?

# Wait 2 seconds between requests; the site blocks faster traffic.
time.sleep(2)

2. Surprising or non-obvious behaviour

If something looks wrong but is deliberate, say so, or the next person will “fix” it.

// Don't remove: Safari fires this event twice.

3. The purpose of a function or file

A line or two at the top of a function or file saying what it does and what it expects.

def clean_email(raw):
    # Returns the email in lower case with spaces removed,
    # or None if it doesn't contain an @.

4. Warnings

Things that will bite: “Deleting this also deletes the backups” or “Must run before the import step.”

5. Workarounds and temporary fixes

Say what the problem was, and when you can remove the workaround.

// TEMP: the plugin update broke the layout. Remove after version 2.1.

6. Where a decision came from

A link to documentation, a ticket or an article that explains a tricky choice. If the code depends on a particular API behaviour, point to where it’s described.

7. Settings and magic numbers

A number alone means nothing. 0.2 could be a tax rate, a delay, a threshold. Name it, or say what it is.

TAX_RATE = 0.2  # 20% VAT; change here if the rate changes

Better still, a clear name makes the comment unnecessary. Naming things in code: why AI needs good names too argues that good names are the first layer of documentation.

What not to comment

  • The obvious. i = 0 # set i to zero helps nobody.
  • Code that you could make clearer instead. If you need three lines to explain a confusing block, consider rewriting the block with better names.
  • Old, dead code. Don’t leave commented-out code lying around “just in case”. Use version control to keep history. Git basics for non-developers and GitHub for builders who are not developers show how.
  • Your feelings or jokes about colleagues. Code outlives moods.
  • Anything that will go out of date quickly and be wrong tomorrow.

The last point is a real hazard: a wrong comment is worse than none. If you change the code, change the comment. Otherwise it becomes a confident lie.

How AI changes commenting

AI tools love to comment. Ask for “comments on every function” and you get them, but often the noise kind:

// Loop through the items
for (const item of items) {

That’s the code in English. It adds clutter without information.

Prompt for the useful kind

Add comments to this code. Explain why, not what. Do not comment obvious lines. Add a short description at the top of each function covering what it expects and returns, and note any assumptions or edge cases. Flag anything that looks fragile.

That prompt steers it towards reasons, summaries and warnings. Prompting for clean, readable code and using AI to document your code and processes go further.

Beware wrong explanations

AI can read code and misunderstand what it’s for. It sees the what but guesses the why. A comment that sounds confident and invents a reason is dangerous. Read every AI comment and ask: is that really why? Only you know the context. If the AI says “this handles rate limiting” and it handles something else, the comment misleads.

So treat AI comments as a first draft. Keep what is true and useful, fix what is wrong and delete the rest.

Ask the AI to explain, then you write the why

A good split: have AI explain what the code does, to check you both understand it the same way. Then you add the reasons you know. How to ask AI to explain code line by line is a helpful technique, and reading code you did not write is the other side of this.

Comments aren’t the only documentation

Different notes serve different jobs:

Where What goes there
Comments in code Why this line or function is the way it is
A README file What the project is, how to run it, how to install it
A changelog What changed between versions (writing a changelog people read)
A build log What you tried and decided as you worked (keeping a build log while working with AI)
Commit messages Why each change was made

If a reader needs the story of the whole project, a README is the place, not a comment buried in line 214.

A note for people who handle sites, not software

Even if you only paste snippets into WordPress or Shopify, comments help. A snippet in your theme with no label becomes a mystery in a year. A single line saying what it does, where it came from and the date you added it can prevent real mistakes:

// Added Aug 2026: hides the sale banner on checkout. Remove if theme is replaced.

This is the habit behind WordPress snippets: where to put AI-written code safely and backing up your code properly: leave a trail.

A simple test

Read your comments without the code. Do they tell a useful story? Now read the code without the comments. Can you follow it? If yes to both, you’ve got the balance. If the code only makes sense with the comments, it may want clearer names. If the comments only repeat the code, delete them.

Another test: imagine a stranger opens the file in a year, with no one to ask. What would they be confused by? Write comments for those spots.

A short checklist

Key takeaways

  • Code says what; comments say why.
  • Comment decisions, surprises, warnings and workarounds.
  • Don’t narrate obvious lines.
  • A wrong comment is worse than no comment.
  • Prompt AI for reasons, then verify them.

Pick one file you wrote recently and read it as a stranger. Which line would you ask yourself about first? Comment that one.