DEV Community

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

Collapse
 
tqbit profile image
tq-bit • Edited

I don't think leaving out comments excludes beginners. Quite the opposite. Writing too much waste into your code sets a bad example. And I've seen more redundant comments than useful ones in codebases I worked on (including my own).

My favourite to this day is:

// Initial request to vendor API
// ... 3 uninformative lines explaining what's going on
// TODO: If fails, figure something out. First request should never fail
function getStuffFirstTime() { ... }
Enter fullscreen mode Exit fullscreen mode

Does this look beginner friendly to you?

If you do want to comment stuff, please write proper JavaDoc / JSDoc / whatever-Doc. That's what it's there for.

  • Use @desc, @property/s and @returns.
  • Give a 2-liner about what your code does if you must
  • If you must use a comment inline, you will probably be better off refactoring your function or method anyway

And if you want to go bonkers, at least be so kind and do so in your automated test suites. You can even use @see in your production code base. And everybody wins.