# Writing good comments: the why, not the how

DevFeed: [Writing good comments: the why, not the how](<https://devfeed.tech/articles/writing-good-comments-the-why-not-the-how-52452.md>)

Original publisher: [Read original article](<http://www.jackfranklin.co.uk/blog/code-comments-why-not-how/>)

Published: 2020-07-28T00:00:00Z

Content type: opinion

Language: en

Sources: [Jack Franklin](<https://devfeed.tech/sources/jack-franklin.md>)

Topics: [code comments](<https://devfeed.tech/topics/code-comments.md>), [Programming](<https://devfeed.tech/topics/programming.md>), [legacy systems](<https://devfeed.tech/topics/legacy-systems.md>)

Tags: [advice](<https://devfeed.tech/tags/advice.md>), [code](<https://devfeed.tech/tags/code.md>), [code-comments](<https://devfeed.tech/tags/code-comments.md>), [developers](<https://devfeed.tech/tags/developers.md>), [writing](<https://devfeed.tech/tags/writing.md>)

## AI overview

Comments that narrate what each line does add little when the code already shows it. The useful comments explain why code takes an unusual form: they supply missing context, such as a bug workaround, legacy constraint, or business edge case, and may document the problem, examples, and intended long-term fix. The article illustrates this with React code: a comment explains a temporary handling of spread `key` props, why the current logic cannot distinguish an explicitly undefined key, and the plan to deprecate key spreading. The author argues that code can be traced to learn what it does, while the reason for a decision is harder to recover later.

## Source excerpt

Code comments often get a bad reputation amongst developers as a waste of time, or a sign that your code could be improved. Here's a quote from a CONTRIBUTING.md file I found on GitHub (and there's many, many more like it): Comments should be avoided. If the code cannot be understood without comments, re-write the code to make it self-explanatory. I think this is pretty poor, incorrect advice the vast majority of the time. I think this stems back to most people's experience learning code. I have a strong memory of a lecturer in my first term of my Computer Science degree (although you'll find this advice in many courses, regardless of if it's University or not) telling us: Every line of code should have a comment explaining what it does. Your upcoming coursework will be marked on this criteria. So, if you're a new student fresh into the course, what do you do? You comment your code of course! // set the input value from the ENV value bar const inputValue = process.ENV.bar // now multiply it by 2 const newValue = inputValue * 2 // now pass it to the square function const finalValue = square(newValue) // this function squares a number and returns the new number const square = (x) => x * x The people who say that comments are bad are thinking of this style of commenting, and they'd be absolutely right! Comments like this that describe the "how" of programming add absolutely no value. Each of those comments above added nothing that couldn't be understood from the code immediately below it. Comment the why The problem with the comments above is that they comment the how. They describe the steps we take. Those comments are very rarely useful; code does a good job at explaining how we do something. After all, lines of code are instructions to tell the computer how to do something. Most of the time you'll find that you don't need to leave a myriad of comments because you can write the code you want to and you don't hit any oddities or quirks that cause the code to look unus