DEV Community

[Comment from a deleted post]
Collapse
 
codevault profile image
Sergiu Mureşan

Too many comments (one per line, for example) is probably a sign of poorly written code.

Definitely, I think you can always group some parts of your code together (5-10 lines) and tell what it does in one line of comment.

Code without comments is suboptimal.

In some cases comments just don't help at all, they are simply repeating what the definition of the function/method/class does.

Comments should explain why, not what.

I don't really understand this one...
Should it explain why a function/method/class exists? Or why you should use it? Here is my train of thought:

Needs method that gets the capacity of a list -> sees size as a method -> does it do what I want? -> looks at comment that says "Gets the current capacity (the number of elements it can store) of the list" -> uses it.

My view on this is quite different (as you can see from this video). In this situation I think it's ok to tell in your comments what that method does.