Fun Infused Games  |   Smooth Operator  |   Evil Scale  |   Nasty: The Game   |   Abduction: The Game   |   Starcraft Live  |   AngryEvilRobot.com   |   Wellplayed.net RSS Feed Available RSS 

  Home   |    Archive   |    Subscribe   |    Search   |    About

Don't Be Afraid to Comment Thy Code
Date 7/25/2008    Tags Rant, Development, Programming    (10)

Comments are my friend. I like to leave comments before functions, at the begining of classes, and before any complex block of logic. If a variable name isn't straight-forward enough to tell persisely what it's for (which is a good thing to have, but not always possible unless you're okay with things named PercentageOfUsersWhoOverUseStaticsDividedByUsersWhoLoveYogurt), I comment that too. I'd probably comment a comment if there was a way to do so.

Some might feel that's borderline too many comments, but I'd rather have too many comments than not enough of them. I've picked up projects by users that didn't document their code at all. You can pick up what the code does, but not always the reason why it does something nor the programmer's intentions for doing something some way.

Good code should be self-documenenting they say. Mostly the reason for this rant is because Jeff Atwood wrote about just this fact on his blog Coding Horror (very good blog FYI). Certainly he made some good points about self-documenting, but I think he went too far in his hatred of comment use (hatred is a bit harsh, but then again so am I).

So the idea for self-documenting code is that you should be able to take a look at a piece of code and know what it's intended to do. If you name your function GetAllContacts(), it's pretty obvious that it will get all contacts. If you do x = 2, you don't need a comment saying "Set x = 2". That seems pretty logical.

Conversly though, if you've got a block of code that does something all mathematical like in your code, I'd much rather have a comment beforehand that says what it SHOULD do rather than having to walk through the code to determine what it does do. The reason for that is two-fold.

First, it's a lot quicker to just read a sentence to determine what a piece of code does. Sure I can walk through it, but I'd rather not. I still have nightmares from college when they forced us to walk through recursive code. When I close my eyes, I can still see a function calling itself.

Second, by writing what code should do in the comments, you document what code should do. Imagine if you will that you write some complex piece of code and then it is used for several months. Now there is some bug in your application and you have to go back over the code. You walk through this code and figure out what it does. Unfortunately, what it acctually does doesn't match what it should do. If you had a comment saying what the code should do, you'd realize that the result you're getting isn't the one you wanted, you'd find the bug right away, and get that promotion and corner office you wanted. But since you didn't comment your code and the code doesn't error out on you (it returns an answer, just not the right one), you won't find the bug, you'll be replaced by someone that knows how to document their code, and you'll loath your new job at the Wendy's drive-thru (but enjoy the free Frosty's until you're reprimanded for stealing food). All because you wanted to save a few lines that the compiler just discards anyways.

Share: Wellplayed.net
This article has been view 1221 times.

Advertisement:

Comments:


Dan

Avatar

7/25/2008 5:10:16 PM

I agree completely. I think that Jeff made a good point about making the code more self-explanatory but went too far with his avoidance of comments.

I also wrote a response to Jeff's post:

http://blog.uncommons.org/2008/07/25/no-your-code-is-not-so-great-that-it-doesnt-need-comments/

Dan.
Disagree

Avatar

7/26/2008 2:01:08 PM

I completely disagree. In my opinion comments become lies much more frequently than not, and they are rarely updated. All it takes is one automated refactoring to destroy an entire series of comments on a method/class/variable, etc...

Code should document itself.
Dan

Avatar

7/26/2008 3:12:44 PM

"Disagree", comments are part of your code. You have to maintain them just like the code itself. If you change the code to break the comments, then fix/remove the comments just as you would if your change broke some other part of the code.

In my experience of maintaining other people's code, the absence of necessary comments is a much bigger problem than the presence of unnecessary comments.
Kris

Avatar

7/26/2008 10:24:02 PM

The more complex the code, the bigger that problem becomes too. Self-documenting or not, I don't want to look through hundreds of lines of code to figure out what something does when i could just read a line or two and know. The programmer does need to take it upon themselves to keep comments up to date because otherwise they are worse than not having them in the first place.
Saggett

Avatar

7/27/2008 1:31:56 PM

I completely agree. I think a lot of programmers disagree because they are either too lazy to write and maintain the comments or because they have too high an opinion of their own code.

A couple of weeks back I was working with a team that said they didn't comment because they all knew the code (through pair-programming) and because is was self-documenting. To prove it one of the developers opened a random class. When I read it and couldn't see _immediately_ what it was supposed to do, he took a look too and it took a good five minutes of reading code and arguing amongst themselves to agree what the code was for. The reason they gave for this delay? We haven't worked on that class for about two months.

Good luck with your two year old uncommented code guys.
Disagree 2

Avatar

7/27/2008 1:38:56 PM

I think commenting code is a sign of badly-written code. Plus, I've never run across any comments that were maintained. If the function is complex then completely refactor the function into multiple well-named methods and variables.

I think Jeff was right.
Kris

Avatar

7/27/2008 2:37:14 PM

Self documenting code will tell you what the code does, it does not tell you the why the code does what it does. Often times the why is very important to why the code is written the way it is. Leaving out the why can be very dangerious when it comes time to refactor your code.
Dustin

Avatar

7/27/2008 4:29:40 PM

I appreciate arguments on both sides and personally find it an "art" to determine what the appropriate level of comments is in my code. I certainly would prefer clean, highly readable code when possible. However, I generally like Javadoc comments for describing classes, packages, and methods. In fact, when learning and using a new Java library, I ALWAYS go to the Javadoc API documentation (http://java.sun.com/javase/6/docs/api/) rather than through the source code. For my project's own internal code, I'm more likely to look at the code itself rather than the Javadoc.
Dan

Avatar

7/27/2008 9:06:27 PM

I agree, and believe that effective, thoughtfully placed comments are valuable.

Although I haven't maintained too much code that isn't my own, one problem I have encountered is that many programmers write comments that are verbose, inarticulate, obscure, or redundant. Especially painful memories involve programmers who spoke English as a second language.

I think that the English-language aspect of commenting should be given more stress. (Well, obviously I mean in articles and books aimed at English-speaking programmers.)

@Dustin: I can relate. Sun's documentation rocks my socks.
Dan 2

Avatar

7/27/2008 9:07:29 PM

I'd also like to point out that I, who just commented, am not the original Dan. Sorry about that :X


Add Comments:


Name
Website
Comment
Insert Cancel
top
top


top
Tags
YouTube (1)  ASP.net (16)  Annoyances (7)   (2)  Sage-Like Advice (12)  Video Games (6)  NFL (3)  Writing (2)  Abduction Action (1)  Particle Effects (1)  Google (3)  Visual Studio (1)  Site News (2)  Advise (1)  Interview (1)  C# (14)  Education (2)  Politics (1)  MSSQL (1)  Certifications (1)  Conspiracy (1)  Abduction Action! (12)  Nasty (30)  Get Rich Quick (1)  Technology (1)  Economy (1)  Cool (3)  Sports (11)  Web Tools (3)  Abduction! (1)  Rant (33)  XBLIG (11)  Tutorial (7)  Web Hosting (1)  Books (1)  Realty (1)  Programming (4)  Tech Support (1)  Vista (2)  Development (11)  Design (3)  Absurd (1)  Links (1)  Legal (2)  Security (4)  JavaScript (7)  CSS (1)  Video (5)  Weird (1)  Computing (2)  Content (1)  TFS (1)  Xbox (1)  XNA (24)  Popularity (6)  Abudction Action! (1)  Domain Name (2)  Game Design (4)  Wii (3)  .Net (3)  AJAX (4)  Game Development (6)  
top

top
top


Twitter

    Follow me on Twitter



    Enjoy Nasty!
    I'm with Coco.