The use of CommentsThere have been several prominent intelligent programmers that have postulated over the years that if you name your variables and functions well enough, and architect your application well, that comments become unnecessary. While I agree with the premise - making your code readable enough that comments are unnecessary - I still feel that Comments are necessary... (Wait, what?!) Or rather, I think they still play an important role and should still be included, though perhaps to a lesser extent. For one thing, even if your code is completely self-evident, comments help reinforce what's supposed to be happening in the code. Let's face it, most of the time when you're debugging someone else's code (or even your own), you eventually find the offending snippet, and stop to wonder, "Was this done intentionally for some reason, or was it an accidental omission?" Code Commenting helps make it clear. If the code agrees with the comments, it was intentional, otherwise, it was probably just a brain-fart. This can save a lot of time and mental anquish. The second thing I consider comments for is to summarize sections of code. Almost every IDE out there will color your comments differently than your code. By summarizing what the next 10 lines of code are doing, I only have to scan 1 out of 10 lines to get the idea of what's going on. No matter how well you name your variables/functions, you can't beat that level of efficieny The other thing that these proponents seem to forget is that sometimes you just do something clever. Sometimes you just find a way to save 100 lines of code with a single line that's a little cryptic. I suppose some would argue that you should write the 100 lines of code anyway, just so it's clear, but for me, there's a limit there. If I can write a piece of code clearly in two lines or cryptically in 1, I'll write the second line. But once you get up to about a 6 or 7 line savings, sorry, but I'll just add a comment explaining what I'm doing, and you can write me an email telling me I'm the devil. |
There are no comments for this entry.
[Add Comment]