Member Sign In
Not a member?

A Wired.com user account lets you create, edit and comment on Webmonkey articles. You will also be able to contribute to the Wired How-To Wiki and comment on news stories at Wired.com.


It's fast and free.

Webmonkey is a property of Wired Digital.
processing...
Join Webmonkey

Please send me occasional e-mail updates about new features and special offers from Wired/Webmonkey.
Yes No

Please send occasional e-mail offers from Wired/Webmonkey affiliated web sites and publications, and carefully selected companies.
Yes No

I understand and agree that registration on or use of this site constitutes agreement to Webmonkey's User Agreement and Privacy Policy.
Webmonkey is a property of Wired Digital.
processing...

Retrieve Sign In

Please enter your e-mail address or username below. Your username and password will be sent to the e-mail address you provided us.

or
Webmonkey is a property of Wired Digital.
processing...

Welcome to Webmonkey

A private profile page has been created for you.
As a member of Webmonkey, you can now:
  • edit articles
  • add to the code library
  • design and write a tutorial
  • comment on any Webmonkey article
Close
Webmonkey is a property of Wired Digital.

Sign In Information Sent

An e-mail has been sent to the e-mail address registered in this account.
If you cannot find it in your in-box, please check your bulk or junk folders.
Sign In
Webmonkey is a property of Wired Digital.

Commenting Your Code — What’s Too Much, Too Little?

HeadtattooDo you often forget to comment your code and find yourself scratching your head years later, trying to figure out what’s going on? After a few experiences like that you might be tempted to start leaving comments all over the place, but that can be just as bad of an idea.

Blogger Jeff Atwood recently posted an interesting look at what makes good comments and how some simple refactoring can make your code self-documenting. If you stick to best practices like giving functions and variables logical names, it shouldn’t be too hard for you or others to figure out how your code works.

That helps eliminates the need to strew comments throughout your code. All that remains to comment is a quick explanation of why your code works.

As Atwood writes, “I’m constantly running across comments from developers who don’t seem to understand that the code already tells us how it works; we need the comments to tell us why it works.”

Atwood walks through a couple of examples of how refactoring some totally uncommented code makes it infinitely more readable and doesn’t add extraneous comments.

So where’s the balance? What’s constitutes over-commented code and what is under-commented? Atwood compares it to writing a book:

Junior developers rely on comments to tell the story when they should be relying on the code to tell the story. Comments are narrative asides; important in their own way, but in no way meant to replace plot, characterization, and setting.

I encourage you to have a thorough read through the article since there’s a lot of good practical advice in it (and a hilarious example of some ridiculously over-commented code). In the end, how many comments your code contains is up to you, but remember the more self-documenting your code is the more readable it becomes.

[photo via MethodShop on Flickr]

See Also:

Post Comment Comments Permalink Print
Reddit Digg

 
Subscribe now

Special Offer For Webmonkey Users

WIRED magazine:
The first word on how technology is changing our world.

Subscribe for just $10 a year