DEV Community

Discussion on: A short comment on comments in the code

Collapse
 
ddaypunk profile image
Andy Delso

I’ve read Clean code and I’m pretty sure he’ he didn’t say comments are bad. He provided cases in his experience where they were and were not useful.

Thread Thread
 
paulknulst profile image
Paul Knulst

No he did not say that in his book.

But within his presentation (the YouTube link) he explains how he personally does it with his IDE just to avoid having comments all over the place.

The main problem with comments is that they will not be updated after changing some lines of codes. Or maybe a function has a comment how this function solves a problem by using XY. But if XY gets changed maybe the comment for XY will be updated but the calling functions will not receive an comment update.

as an example lets think about you have a function that uses another function to sort an array:

  • the comment says this method uses bubble sort to sort the array
  • now someone goes to the function that actually sorts the array and updates it to use quick sort algorithm
  • he changes the sort comments
  • because the method still does the same - sorting an input array - the underlying code can be changed.
  • outer functions that uses the sort function have documentation that maybe says: calls an sort function that uses bubble sort.
  • this leads to wrong documentation if the person who changes one function only change the comments above the changed function and not in EVERY function that uses the function

This is just an very easy, understandable problem that should describe the problem with having comments everywhere. You can easily exchange the sorting algorithms and the call hierarchy with other things, but the comment-problem still exist.