Replace comments with better code

Comments in the code are needed and I strongly believe in the comments as documentation for certain functions or entire classes.

When you work in a team, comments often cause discussion and disagreement. Let us agree on the concept of “comments”.

class Math
{
   public function calc() 
   {
     // Add b to a 
     $c = $this->a + $this->b;

     // return the result of a + b 
     return $c;
   }
}

This above could be the result of a meeting where the team is pushed to comment carefully.

Repeating the code is the worst you can do, adding comments that describe what your code is doing when it would be much clearer to read the code itself means that you waste time and other developers will spend time too to investigate unless documentation.

Junior developers rely on comments to tell stories when they should rely on code to write their stories. Less experienced developers tend to use comments to describe the story behind a code block.

We can even be more expressive taking greater care on the names of classes, functions and variables without writing a line of comments.

If you feel the need to write comments to explain what your function is for, the first thing you need to do is consider the possibility of restructuring the code you wrote to make it explain its purpose alone. Take this example:

**
 * Find low rent at 15% gross monthly income
 */
public function rent($income)
{
  return round(($income*0.15) / 12);
}

Only one comment line could be acceptable. Or could we review the code to make it clearer, modular and avoid any comment?

public function calculateLowRent($monthlyIncome): int
{
  return $this->calculateMonthlyRent($monthlyIncome, 0.15);
}

public function calculateMonthlyRent($monthlyIncome, $percentage)
{
  return round(($monthlyIncome*$percentage)/12);
}

The code is more verbose and there is no need for comments.

The numbers now have a label and the functions have a name that clearly explains what they do. This example may seem a little excessive if taken individually. What you need to focus your attention on is the strategy, the value of explaining how and why that code is found there using the code itself.

My advice is to not underestimate this aspect. If too many comments are present in the code it increases exponentially the risk that you, and the other developers, will pay less attention to their presence also propagating in the documentation of the old and wrong information.

Very often comments are obviously needed to explain more complex scenarios or link to bugs and it is not possible to do so using only the names in the code. In modern IDE comments are often useful to improve code navigation. In any case, the next time you feel the need to write comments you can ask yourself if it is possible to have the same readability using the code itself improving maintainability of your code base.


I'm creator of Inspector a real-time monitoring tool for Web Agencies and Freelancers. Other developers feedback are driving my product roadmap and I would be happy if you joined me on this journey.

Try Inspector, it’s free, and write me what you think about: inspector.dev

No Comments Yet

Add a comment