Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

More comments in code or just simple, readable, maintainable code suffices? [closed]

Tags:

comments

Sometimes its really difficult to decide on when exactly you have written enough comments for someone to understand your intentions.

I think one needs to just focus more on writing readable, easy to understand code than on including a large number of lines of comments explaining every detail of whats happening.

What are your views about this?

like image 667
Lonzo Avatar asked Feb 13 '09 13:02

Lonzo


4 Answers

Comments aren't there to explain what you're doing. They're there to explain why you're doing it.

like image 200
Paul Tomblin Avatar answered Oct 10 '22 04:10

Paul Tomblin


The argument is based on a false dilemma: Either your code is a horrible abomination and you write tons of comments to explain every statement and expression, or your code is beautiful poetry that can be understood by your grandmother with no documentation at all.

In reality, you should strive for the latter (well, maybe not your grandmother but other developers), but realize that there are times when a couple of comments will clear up an ambiguity or make the next ten lines of code so much more plain. People who advocate no comments at all are extremists.

Of course, gratuitous comments should be avoided. No amount of comments will help bad code be more understandable. They probably just make it worse. But unless you're only coding trivial systems, there will be times when comments will clarify the design decisions being made.

This can be helpful when catching bugs. Literate code can look perfectly legitimate while being completely wrong. Without the comments, others (or you six months later) have to guess about your intent: Did you mean to do that, or was it an accident? Is this the bug, or is it somewhere else? Maybe I should refer to the design documentation... Comments are inline documentation, visible right where you need it.

Properly deciding when the need for comments actually exists is the key.

like image 37
Adam Bellaire Avatar answered Oct 10 '22 03:10

Adam Bellaire


Try to make the code self-explaining. One of the most important things is to use meaningful names for classes, functions, variables etc.

Comment the sections that aren't self-explaining. Trivial commenting (e.g. i++; // Add 1 to i) makes the code harder to read.

By the way - the closer to pseudocode you can work, the more self-explaining your code can become. This is a privilege of high-level languages; it's hard to make self-explaining assembly code.

like image 36
Joonas Pulakka Avatar answered Oct 10 '22 04:10

Joonas Pulakka


Not all code is self-documenting.

I'm in the process of troubleshooting a performance issue now. The developer thought he discovered the source of the bottleneck; a block of code that was going to sleep for some reason. There were no comments around this code, no context as to why it was there. We removed the block and re-tested. Now, the app is failing under load where it wasn't before.

My guess is someone had previously run into a performance issue and put this code in to mitigate the problem. Whether or not that was the right solution is one thing, but a few comments about why this code is there would now be saving us a world of pain and a whole lot of time...

like image 33
Patrick Cuff Avatar answered Oct 10 '22 04:10

Patrick Cuff