Code comments are your code autobiography
Tags: softwaredevelopment, softwareengineering, cleancode, refactoring
Published: 2019-07-25 10:34:42

---
title: Code comments are your code autobiography
published: true
date: '2019-07-25T10:34:42.794Z'
tags: softwaredevelopment, softwareengineering, cleancode, refactoring
canonical_url: https://medium.com/@dvirsegal/code-comments-are-your-code-autobiography-7202c45b5e37
---

![Photo by [Nick Morrison](https://unsplash.com/@nickmorrison?utm_source=medium&utm_medium=referral) on [Unsplash](https://unsplash.com?utm_source=medium&utm_medium=referral)](https://cdn-images-1.medium.com/max/1200/0*pnowXdHHLnnyjagr)

<span>
<em>Originally published at <a href="https://medium.com/@dvirsegal/code-comments-are-your-code-autobiography-7202c45b5e37" style="color:#1395b8">Medium</a> on <time datetime="2019-07-25T10:34:42.794Z" title="Thursday, July 25, 2019, 10:34:42 AM">Jul 25, 2019 </time>
</em>
</span>

Writing about your code is redundant; the code is all you need. I can invest my time on unit tests or develop other features, and other **excuses** of why writing code comments is a waste of time. It’s a common belief among developers.

Well, you’ve invested all your efforts in cultivating the design of your code, why should you comment it, right? As a result, code documentation is either absent or useless.

{% twitter 1014165500056596481 %}

While in some cases, it is true that writing code comments can be counterproductive. Yet, we are the only ones who can benefit the most out of it if being done right.

#### Code comments as a contract

A self-documented code doesn’t exist. You can write meaningful variables names, methods, classes, etc. but as good as it gets, it doesn’t make your code automatically describe the meaning of it.

{% twitter 1095721294216089600 %}

Yes, sure during the implementation while sharpening it to perfection, you are in the context of it, but what would happen in a month from now? How much will you remember? Can you trust your future self to be in the same mindset when approaching it?

Realizing what you thought when you wrote this piece of code would assist you in the time ahead. Proper code documentation is making a contract with the maintainer to be, which is probably you.

#### Code comments are an inherent part of clean code

In some cases, code comments can point on code smells. If you find yourself writing that the method does this “and” that while writing your method’s code documentation. You probably should refactor the method, since it has to do one thing and one thing only. Therefore, break the code into two methods and immediately mend the code comments to fit the change. In such way, you adhere to a clear code concept.

Furthermore, digging into the code to better understand the context of the method is an unnecessary waste of time, while you could easily read the method header and quickly recall the _what (_obvious) and _why_ parts of the method and in particular focus on the _how_ it does what it was written for_._

Take for the example the comment in the below gist, it just states the obvious and tells what it does — basically, it repeats the property’s name, which makes it redundant. While removing it will have the same effect as not writing it in the first place:

{% gist https://gist.github.com/dvirsegal/afacd23028e2911684280fcaa6f07d0d %}

A better comment will have more details which state what the code can’t tell us. Well, you can say — why should I write it? The property’s name and logic are straightforward; it just returns _weight_. But hey, nothing is ever simple in software. A useful comment can save us the time and cognitive efforts of digging deeper into the code for the method’s context, as can be seen below:

{% gist https://gist.github.com/dvirsegal/3791b967b9eebd5de2a78688ac633865 %}

This approach has a different mindset that you should adopt. Each method has something to tell, at least one new detail that the code doesn’t state, whether it’s a side effect, restriction, or any other abnormality. Writing about these detail will lead the developer to confront with the hidden details of its implementation and in times refactor the _“code smell”_ parts. Thus, not only it helps the future readers of the code but also assists the writer to gain a better understanding of its implementation and improve it.

#### “Code never lies, comments sometimes do.” — [**Ron Jeffries**](https://www.azquotes.com/author/30249-Ron_Jeffries)

Have you ever heard that sentence? Indeed, comments get outdated when the code changes. The fact they do is because they are not considered as part of the code itself while they should. The code comments are coupled to it. It is in the developer’s responsibility to update it too. A fact should be checked during a code review.

![Self-documenting code by [geekandpoke](http://geek-and-poke.com/geekandpoke/2013/2/14/self-documenting-code.html)](https://cdn-images-1.medium.com/max/800/0*Ni2scXEd6h8tvBkH)
Self-documenting code by [geekandpoke](http://geek-and-poke.com/geekandpoke/2013/2/14/self-documenting-code.html)

One last thing, the scope of the code is not relevant whether you should or shouldn’t put a comment on it. By that I mean, private access modified code should be documented too. Otherwise, we neglect the actual users of it, ourselves.

Without a doubt, for some developers, it will take time, practice, and getting used to this new way of thinking. But, in the long run, you’ll reap the rewards — in terms of code quality and a better understanding of your code circumstances, and eventually, your code will tell his story, its autobiography.