DEV Community

Discussion on: "Do not comment on your code, it should be self-documented". Well... I don't agree.

Collapse
 
matthewsalerno profile image
matthew-salerno

I have seen code with too many comments. Something like five lines of code having 5 lines of comments each, which really breaks the flow of reading code for such a simple function. That being said, I think it's important to describe the purpose of each block of code with some standard format. It also helps some IDEs provide you with infirmatiomnon mouse over, which is incredibly helpful. So that being said, I try (and usually fail) to use my comments like so:

  • 1 comment all functions, classes, blocks, etc with a brief description. Even if its something dumb like "this add function adds two numbers", it can help for the IDE
  • 2 if logic of implementation isn't obvious, and can't be made obvious, describe it as best you can either by pointing out subtleties on certain lines, or a full explainer before hand if it's really that bad. Most of my projects have been independent though, so I'm slightly ashamed to say I don't write as many comments as I'd like.