Skip to content
Clean CodeChapter 4 / 57Beginner6 min read

Self-Documenting Code (No Comments)

How to write self-documenting code without comments - use names, named functions and constants instead, and learn when a comment is truly needed.

On this page

1. What is this practice?

The strict version in this course is: no explanatory comments in code. No section banners, no docblocks, no "why is this line here" comments. If a line needs explaining, the code is not clear enough yet.

Only a few comments are allowed, the ones meant for machines or that cannot live anywhere else:

AllowedExample
Linter or type-checker directive// eslint-disable-next-line
Code generator markerThe header of a generated file
Blocks that generate API docsDoc comments for OpenAPI, if the tool builds docs from them
SQL migration headerWhy this migration exists
An explanation of each key in .env.exampleWhat breaks if this variable is missing

A "why" that must be remembered for a long time (a strange vendor limit, a deliberate decision) goes in the project's decisions document, not in the code.

2. Why does it matter?

The biggest problem with comments: the compiler does not check them. The code changes, the comment stays, and now it is lying.

Explaining with commentsExplaining with code
The code changed, the comment stayed old and misleadsA rename changes everywhere, and the compiler checks it
A bad name and a comment above it, you have to read bothA good name, only one thing to read
Bad code hides behind a commentPressure to clean it up, so the code gets better
A pile of commented-out codeDead code is deleted, and the history is in Git

3. When to follow it

LevelHow importantWhy
Learning projectComments are fine for notes, but build the habit of improving the name firstComments are reminders while learning, but start practicing naming now
Production codeFollow the team's ruleStale comments in production cause confusion and bugs
Senior levelDecide which comments are allowedA senior makes a clear list for the team

4. Bad vs good code

ts
function check(u: User) {
  // check if trial is expired (14 days)
  if (Date.now() - u.createdAt.getTime() > 14 * 24 * 60 * 60 * 1000) {
    // user is not on a paid plan
    if (u.plan === "free") {
      return false;
    }
  }

  return true;
}
ts
const TRIAL_LENGTH_MS = 14 * 24 * 60 * 60 * 1000;

function isTrialExpired(user: User) {
  return Date.now() - user.createdAt.getTime() > TRIAL_LENGTH_MS;
}

function canAccess(user: User) {
  if (!isTrialExpired(user)) {
    return true;
  }

  return user.plan !== "free";
}
What is the difference?BadGood
Function namecheck says nothingcanAccess tells you the answer
ConditionYou understand it by reading the commentisTrialExpired speaks for itself
Number14 in a comment, a long calculation in the codeTRIAL_LENGTH_MS in one place
RiskIf the trial becomes 30 days, the comment stays wrongChange the constant, and everything is right

Every comment was a hint about what to extract. The first comment became a predicate, and the number became a constant.

5. How to apply it

When you feel like writing a comment
Ask: what is the comment explaining?
The meaning of a value? Make a named constant
The meaning of a condition? Make an isX or hasX predicate
What a block does? Make that block a named function
A long decision or a why? Write it in the decisions doc or the PR description
Delete the comment
Kind of commentBetter place
"what this does"The name of the function or variable
"what this number means"A named constant
"why this condition"A named predicate
"why we made this decision"The decisions document
"why this change"The commit message or PR description
Old codeDelete it, Git history keeps it

6. Exceptions and common mistakes

An honest note: "no comments at all" is a strict house rule. Many good teams allow "why" comments, like a workaround for a browser bug or the source of an algorithm. JSDoc is also common in public libraries because editors show it. The trade-off: a strict rule forces you to keep the code clean, but sometimes you have to look somewhere else for a one-line "why". Whatever your team's rule is, "what" comments are useless everywhere.

Mistake: A comment that repeats the code

Why? A comment like increment i by 1 is just noise, and it goes wrong when the code changes.

Better: Delete the comment. If the code is not clear on its own, improve the name.

Mistake: Leaving commented-out code behind

Why? Nobody knows whether it will come back, and it slowly stops matching the code around it.

Better: Delete it. If you need it, bring it back from Git history.

Mistake: Organizing a long file with section banners

Why? A banner shows the file does several different jobs, which means the file is too big.

Better: Move each section into its own file or function.

Mistake: Adding a linter directive without a reason

Why? eslint-disable is an allowed comment, but it hides the real problem.

Better: First fix what the rule is complaining about, and use a directive only when there is no other way.

7. What to learn before and after

Suggested order
Self-Documenting Code
Naming Things
Early Returns & Braces
Small Functions

Final quick revision

CheckQuestion
NameDoes the function name say what it returns?
PredicateDoes every tricky condition have a name?
ConstantIs any unnamed number or string left?
Dead codeIs there any commented-out code? Delete it
WhyIs the long decision in the decisions doc?
Allowed listOnly directives, generator markers, migration headers, env example