Thursday, January 14, 2010

Comments... really?

Why in the heck do people feel the need to write a novel about their code in the comments? I understand comments are good programming practice. I use comments all the time to make notes to myself about things in the code which may not be apparent at first, such as the purpose of a variable, or why a line of code is commented.

But I see this kind of thing in code and it annoys me:
// increment the 'counter' variable
counter++;

If you don't know what counter++ does, you should not be programming, and therefore should not be reading those comments.

So stop it with the novels, already. Chances are most programming projects can boast a bazillion lines of code because most of them are comments:
/**
* incrementCounter
*
* this function is very important to the program
* flow. without this function, the program would
* not be able to operate correctly
*
* usually, this function is called by mainLoop
* once every frame, but exceptions are made. this
* function should never be used anywhere outside
* of mainLoop, or the program flow could potentially
* be thrown out of whack
*
* this function is a simple wrapper for the
* 'counter++' operation and nothing more. however,
* it will stick around because you never can know
* when you need this function to do something
* else
**/
void incrementCounter (game_t *game)
{
// increment the 'counter' variable of the supplied
// game argument
(game->counter)++;
}


Seriously, dude, couldn't you have done something a little less superfluous? Like this:
/**
* incrementCounter
* do NOT call this outside of
* mainLoop, or risk screwing up
* program flow
**/
void incrementCounter (game_t *game)
{
(game->counter)++;
}


Programmers are not morons, so stop treating them as such. We understand that functions are created to logically group special operations which may need to be repeatedly called or organized properly.

Of course, I'm making these examples up. I'm too lazy to try and find a real example, but this kind of thing is everywhere in Java.

What do I want you to walk away with? This is what I want you to get out of this post: comments are not for novels.

No comments:

Post a Comment