When programming we all use comments. Some times we use comments more extensively then other times. In general we accept comments as a good thing, which of course they are. Comments can help you e.g. to annotate the difficult state logic of your fully animated menu system. Or it can be used to add full documentation of your library or framework API as you can do with something like JavaDoc and from which your actual documentation can be generated. Pretty cool!
I use comments myself in both situations. When I create libraries I always use a JavaDoc style of commenting for the interface of my API and when I do application development I tend to use more inline comments that explain some of the more difficult logic or that explains the structure of certain parts of the application. With inline comments I try as much as possible to make a brain dump of my short term memory about a certain aspect of the logic. This way, when I come back to this particular piece of code, be it weeks or even months later, I can more easily pick up of what was going on or why I did things in a certain way. This is especially necessary for the gotchas!
Over the years however I’ve seen quite a lot of programmers taking a weird approach to comments. Often when browsing through source files, of mostly unexperienced programmers, I often see comments like this:
function foo(x : int, y : int) : int
{
var q;
// Add x & y together and store the result in q.
q = x + y;
return( q );
}
You see that comment? What it does, is just echoing what the code does. It a completely pointless comment. I’m a programmer and I can read code! I know it adds y to x and then stores the result in q. What I would like to know is why… Instead of actually taking the time to describe what the function does and why it exists, the programmer added a pointless comment to the code because commenting your code is good, isn’t it? Well, not if you comment your code this way.
So, next time you write that function that has some obscure algorithm going on, just add comments that describe in plain English what that algorithm does, how it works and what you should use the function for. Don’t just add useless comments out of the notion that commenting your code is good.