|
|
|
|
|
by timo614
5050 days ago
|
|
I'd say build your code such that you don't need to comment but comment wherever there is some unclear reason for why something is done. Instead of x+= n;
running_total += total; But comments are helpful for understanding the reasoning behind why something was done. For example:
$.resize.delay = 16; Was placed in one file of some code I collaborate on with the comment "// this is probably a horrible idea" I had to ask the developer what exactly the code does (yeah I could have looked it up but since he said it was a horrible idea I wanted to know why). Turns out it just configures the jquery resize event to fire at about 60 frames per second. Having a comment like:
// Configures jquery resize to fire the resize event approx 60 frames per second Would have made the code a bit easier to understand. |
|
I'd argue that the following line is a better solution in every way than adding a comment as you suggest:
Self-commenting code is always better: less duplication, less maintenance, greater refactoring agility, and most of all, it encourages you to design a cleaner system in the first place.Granted, in real-world scenarios, there will always be many cases where you are forced to add comments, sometimes extensively (hand-optimized code, inherently complex systems, etc).If anything though, this only confirms how important it is to aim for self-documenting code.
Real-world production code is rarely a pretty thing, and the fact that you have to start adding comments everywhere is a reflection of this. You'd be surprised how clean and elegant code becomes when your only goal is to make it as self-descriptive as possible.