DEV Community

Discussion on: Code comments are (mostly) a violation of DRY

Collapse
 
yaireo profile image
Yair Even Or • Edited

I have been writing comments for 20 years now and the reason for commenting code is to let my future-self / others understand the reasoning behind it.
A comment shouldn't explain the code itself unless it's very complex (no way around complexity sometimes)

Even a function which returns something very simple sometimes needs a comment, as to why does it even exist?

For example I have a project with many bizzare functions and timeouts which must have a comment to explain they exist solely because of some browser bug.

Many times I also put a link to a stackoverflow/github discussion or to a browser's bug-tracker url.

I also write comments such as instructions to myself to improve some function and how to do it, if it takes too much time to write it perfectly and right now i need to write it dirty just to continue my work.

I LOVE comments and believe a code without comments it like cookbook with just the ingredients and no instructions. yeah ok, this cake needs 3 eggs and a lemon. now please tell me WHY it needs those or I will not comply. I must know the "why" of everything.

Collapse
 
danrot90 profile image
Daniel Rotter

The last analogy is not really correct... The code is some kind of instruction. The other suggestions you have mentioned I would consider good comments: E.g. that something is necessary because of a browser bug is not clear by just looking at the code. Also adding a comment if you know how to improve but you don't have time is a good use case (of course it would be better to just fix it right away, but that is very idealistic).