DEV Community

Discussion on: Is Commenting Code that necessary?

Collapse
 
patferraggi profile image
Patricio Ferraggi • Edited

In my opinion, comments should not explain what the code does. There are a few reasons to use comments though:

  1. to express the developer's intent, why something was done in a certain way.
  2. to describe API functionality (if the code is gonna be used by an external project or as part of a web service)
  3. when performance is so critical that there is a reason to avoid abstractions.

After these considerations, if you need to add a comment to express what the code does, then your code is crap and you should improve it instead of adding a comment.

I like this quote from Kevlin Henney: "A common fallacy is to assume authors of incomprehensible code will somehow be able to express themselves lucidly and clearly in comments."

Collapse
 
ashleyjsheridan profile image
Ashley Sheridan

As always, there is an exception: regular expressions.

Of course, we could write 20+ lines of code to replace a 1-line regex and make it more readable, but sometimes a regex really is the best fit for a problem. The downside is that they tend to be as legible as ancient Egyptian hieroglyphics. So those I comment what they do.

Collapse
 
aashrithashiva29 profile image
Aashritha Shiva

Yeah ! Well explained :)

Collapse
 
patferraggi profile image
Patricio Ferraggi

Thanks, to be fair I wrote a few articles on clean code so I had time to practice ahaha

Thread Thread
 
aashrithashiva29 profile image
Aashritha Shiva

Hahaa greatt !! :)