the blog
, a 1-minute read
why comment your code
Code tells the computer what to do; comments tell the next person why. The kinds there are, why they pay off, and how to write ones that stay true.
Code tells the computer what to do. Comments tell the next person why, and the next person is usually you, six months later.
what a comment is
Text in the source that the language ignores. Most languages have two kinds: one for a single line, // in JavaScript or # in Python, and one for a block, /* … */ in JavaScript, C and their family.
// a whole line
const TAX = 0.2; // or the end of one
/*
a block,
over several lines
*/why bother
- Readability: the reason behind a line is often the one thing the line can't say.
- Teamwork: the people you work with understand your code faster, and you theirs.
- Maintenance: old code is easier to change when it says what it was for.
- Debugging: when the logic is written down, it's easier to see where the code stopped following it.
how to write them
- Say why, not what.
retries += 1doesn't need “add one to retries”; a workaround needs to say what it works around. - Be consistent: one style across the codebase.
- Keep them plain: short sentences, no jargon you don't need.
- Don't comment every line. Comment the decisions, the tricky parts, the algorithms.
- Change them with the code. A comment that's no longer true is worse than none.
// says what the code already says
retries += 1; // add one to retries
// says why
// the API rejects bursts: count this try, and wait before the next
retries += 1;First published on dev.to, 11 April 2023; rewritten here, 24 November 2026. The original, on dev.to