Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Commenting practices?

As a student in computer engineering I have been pressured to type up very detailed comments for everything I do. I can see this being very useful for group projects or in the work place but when you work on your own projects do you spend as much time commenting?

As a personal project I am working on grows more and more complicated I sometimes feel as though I should be commenting more but I also feel as though it's a waste of time since I will probably be the only one working on it. Is it worth the time and cluttered code?

Thoughts?

EDIT: This has given me a lot to think about. Thanks for all your input! I never expected this large of a response.

like image 626
tgai Avatar asked May 03 '10 17:05

tgai


3 Answers

Well considered comments illuminate when the code cannot. Well considered function and variable names eliminate the need for copious comments. If you find it necessary to comment everything, consider simplifying your code.

like image 117
Bob Kaufman Avatar answered Oct 03 '22 13:10

Bob Kaufman


If you ever look at code you wrote 6 months before, you will be wondering why you did not comment better.

like image 41
Romain Hippeau Avatar answered Oct 03 '22 13:10

Romain Hippeau


If the code is well written, with short methods (see Composed Method pattern) and meaningful names, then the code needs very little comments. Only comments which explain the "why" are useful - the comments explaining "what" should be replaced by improving the code so much that it's obvious what it does. Comments should not be used as an excuse for writing bad code.

Public APIs, especially in closed-source apps, are perhaps the only place where thorough javadocs are recommended - and then you need to take the effort to maintain them and keep them always accurate and up-to-date. A misleading or outdated comment is worse than no comment. It's better to document what the code does by writing tests for it and using good names for the tests.

The book Clean Code has a good chapter about comments.

like image 26
Esko Luontola Avatar answered Oct 03 '22 14:10

Esko Luontola