Blog / Tutorials

How to Document Your Code So Future You Doesn't Hate Present You

, 7 min read

You wrote some code three months ago. It worked perfectly. Now you need to change it and you have no idea what it does or why you wrote it that way. You have no comments, no README, and the variable names are things like x, temp, and data2. Sound familiar?

Bad documentation is a universal developer problem, and it hits teens especially hard because nobody teaches you how to document code — they just tell you that you should.

Why Documentation Matters

Code is read far more often than it is written. Even on a solo project, you will come back to your code weeks or months later and need to understand it. On a team project, other people need to understand your code without asking you to explain every line.

Good documentation is not about writing more — it is about writing the right things. You do not need to document what the code does (the code itself shows that). You need to document why the code does what it does.

The README

Every project needs a README.md file. This is the first thing anyone sees when they visit your repository. A good README includes:

What the project is and what problem it solves. One or two sentences, not a paragraph. How to install and run it. Step by step, assume the reader is starting from scratch. What technologies it uses and why. Any environment variables or configuration needed. How to contribute if it is open source.

Write your README as if you are explaining the project to a smart person who has never seen it before. Because that is exactly who will read it — including future you.

Comments: Less is More

Bad comments describe what the code does: "// increment counter by 1" above counter++. The code already says that. This comment adds nothing.

Good comments explain why: "// We increment by 2 instead of 1 because the API returns interleaved results and we only want even-indexed items." This tells the reader something the code alone cannot communicate.

Comment on complex algorithms, non-obvious business logic, and workarounds for bugs. Do not comment on obvious operations.

Naming Things Well

The best documentation is code that documents itself through clear naming. Compare these:

Bad: const d = new Date(); const x = d.getTime() - s.getTime();

Good: const currentDate = new Date(); const elapsedMilliseconds = currentDate.getTime() - startDate.getTime();

The second version needs no comments because the names tell you what is happening. Variable names should describe what the variable contains. Function names should describe what the function does. Class names should describe what the class represents.

Function Documentation

For complex functions, write a brief docstring that explains: what the function does, what parameters it takes and what types they should be, what it returns, and any side effects or exceptions it might throw.

In JavaScript and TypeScript, JSDoc format is standard. In Python, use docstrings. These are not just for humans — many IDEs read these and show them as tooltips when you hover over a function call.

Commit Messages

Your Git commit messages are documentation too. "fixed stuff" and "update" tell future readers nothing. "Fix off-by-one error in pagination logic" and "Add rate limiting to login endpoint" tell the full story.

Good commit messages follow a pattern: imperative mood, present tense, under 72 characters for the subject line. If more context is needed, add a blank line and then a longer description.

API Documentation

If you build an API, document every endpoint. What URL does it respond to? What HTTP method does it use? What parameters does it accept? What does it return? What errors can it produce?

Tools like Swagger/OpenAPI can generate interactive API documentation from your code. For smaller projects, a well-organized section in your README works fine.

The Practical Approach

You do not need to document everything perfectly from the start. But build these habits: write a README before you write code (it forces you to think about what you are building). Name variables and functions descriptively. Write a commit message that future you will understand. Add a comment when you write code that you had to think hard about.

Documentation is not extra work on top of coding. It is part of coding. The sooner you internalize this, the less time you will spend confused by your own code.