Just two years ago, I was beyond skeptical towards the forces telling me that comments are
worse-than-useless, self-injuring blocks
of unexecutable text in
a program. I thought the idea was downright ludicrous. But as I've made an effort towards reaching this nirvana called "self-documenting code," I've noticed it's far more than
a pipe dream.
The first thing you have to do is throw out this notion
of gratuitously commenting for the sake
of commenting that they teach you in school. There's no reason every line needs to be commented with some text that simply reiterates what the line does.
After that, we can examine some seemingly rational excuses people often use to comment their code:
-
The code is not readable without comments. Or, when someone (possibly myself) revisits the code, the comments will make it clear as to what the code does. The code makes it clear what the code does. In almost all cases, you can choose better variable names and keep all code in a method at the same level of abstraction to make is easy to read without comments.
-
We want to keep track of who changed what and when it was changed. Version control does this quite well (along with a ton of other benefits), and it only takes a few minutes to set up. Besides, does this ever work? (And how would you know?)
-
I wanted to keep a commented-out section of code there in case I need it again. Again, version control systems will keep the code in a prior revision for you - just go back and find it if you ever need it again. Unless you're commenting out the code temporarily to verify some behavior (or debug), I don't buy into this either. If it stays commented out, just remove it.
-
The code too complex to understand without comments. I used to think this case was a lot more common than it really is. But truthfully, it is extremely rare. Your code is probably just bad, and hard to understand. Re-write it so that's no longer the case.
-
Markers to easily find sections of code. I'll admit that sometimes I still do this. But I'm not proud of it. What's keeping us from making our files, classes, and functions more cohesive (and thus, likely to be smaller)? IDEs normally provide easy navigation to classes and methods, so there's really no need to scan for comments to identify an area you want to work in. Just keep the logical sections of your code small and cohesive, and you won't need these clutterful comments.
-
Natural language is easier to read than code. But it's not as precise. Besides, you're a programmer, you ought not have trouble reading programs. If you do, it's likely you haven't made it simple enough, and what you really think is that the code is too complex to understand without comments.
There are only four situations I can think
of at the moment where I need to comment code:
- In the styles of Javadoc, RubyDoc, et cetera for documenting APIs others will use.
- In the off chance it really is that complex: For example, on a bioinformatics DNA search function that took 5 weeks to formulate and write out. That's how rare it is to have something complex enough to warrant comments.
- TODOs, which should be the exception, not the rule
- Explaining why the most obvious code wasn't written. (Design decisions)
In what other ways can you reduce
clutter comments in your code? Or, if you prefer, feel free to tell me how I'm wrong. I often am, and I have
a feeling this is one
of those situations.
What are some other reasons you comment your code?
Update: I forgot to mention that discussions on relatively recent blog posts by
Brian Kotek (Don't Comment Your Code) and
Ben Nadel (Not Commenting and The Tipping Point of Poor Programming) got the creative juices flowing in
my brain for this post. Thanks for bringing it up, gentlemen.
Hey! Why don't you make your life easier and subscribe to the full post
or short blurb RSS feed? I'm so confident you'll love my smelly pasta plate
wisdom that I'm offering a no-strings-attached, lifetime money back guarantee!
Leave a comment
Back in the mid 1990's when I worked in the Avionics Defence industry, we used to have to design our code in Pseudo-code comment form. This would then be reviewed(sometimes many times) in meetings before turning it into real ADA/CORAL 66 code. The comments were always left in. But this was a long time ago :-)
Posted by Neil Cooper
on Feb 26, 2013 at 08:09 AM UTC - 5 hrs
There was a chapter about this in the book Code Complete, and I was a fan at one point, even doing it myself:
http://www.codeodor.com/index.cfm/2007/2/10/The-Ps...Nowadays I really only do it if the problem I'm working on is particularly hard for me to grasp, and I'm ashamed to say I don't remember the last time that was. I guess I should work on harder problems!
Posted by
Sammy Larbi
on Feb 26, 2013 at 08:30 AM UTC - 5 hrs
I only did it because it was company/Government/Military standard. Naval Sonar, F-16 and F-22 Helmet and Head Up Displays and Eurofighter Flight control work required it.
I'm in the UK but a lot of the work was for the U.S.A - I was a grunt, a drone software engineer just doing what they were told. It was actually intensely dull :-)
I'm glad I now work for a TV company.
Posted by Neil Cooper
on Feb 26, 2013 at 08:40 AM UTC - 5 hrs
Cool - what kind of work gets done at a TV company? Websites, I'd imagine, but what else?
Posted by
Sammy Larbi
on Feb 26, 2013 at 08:58 AM UTC - 5 hrs
I work for BSkyB (
http://www.sky.com/) 100's of TV channels + over 50 HD and 3D channels.
My work constists mostly of Web and Interactive TV at the moment but also we have the Sky+ app that allows you to look at the TV listings and put your set top box onto record from anywhere in the world as long as you have an internet connection. There is also the Sky GO app that allows you to watch live TV channels, on demand movies etc. Sky Sports allows you to watch multi screen Sports including the 9 feed Formula one racing. There are sky news apps .. actually the list is endless.
http://www.sky.com/products/ways-to-watch/sky-apps/index.html" target="_blank">
http://www.sky.com/products/ways-to-watch/sky-apps...
NOW TV (
http://www.nowtv.com/) internet TV service.
There is also games dev for Sky Games or different channels such as MTV. I'm doing some work with Disney and FX channel at the moment.
Special effects work, graphics work for TV Shows and films, Internal applications for different departments..
Needless to say, the S/W Engineer Dept. is pretty big.
Posted by Neil Cooper
on Feb 26, 2013 at 10:11 AM UTC - 5 hrs
oops. I'm not sure how some HTML got into that post ?!?!?
Posted by Neil Cooper
on Feb 26, 2013 at 10:12 AM UTC - 5 hrs
Wow, that sounds amazing! What's the interactive TV stuff like? Like for DVR, channel guides, and whatnot, or something else?
Re: the HTML in the url ... looks like my regex could use some work in IDing URLs as well. =)
Posted by
Sammy Larbi
on Feb 26, 2013 at 10:34 AM UTC - 5 hrs
The interactive TV stuff ranges from sports/news multiscreen video/info/stats tv applications (like F1 racing which looks like this
http://e1.365dm.com/13/02/496x259/redbutton_289579...) to games and booking facilities. In the past you could do banking and shopping too but there isn't so much of that any more.
Voting apps for TV shows (X-Factor, Talent shows, other rubbish) is also big in the interactive TV world.
Some channels have a permanent link to interactive apps via the red-button on the remote control.
Posted by Neil Cooper
on Feb 26, 2013 at 11:26 AM UTC - 5 hrs
Wow, that stuff looks really cool. I cancelled my cable TV, in part because of how terrible the interface was to do anything. I think they've come a long way since then, but I'd be surprised if it was as nice as the stuff you're showing there.
Thanks for enlightening me! =)
Posted by
Sammy Larbi
on Feb 26, 2013 at 12:23 PM UTC - 5 hrs
Only "bioinformatics DNA search function that took 5 weeks" deserve comments? OK, ok, ok. I get the point... just anything that not understand by NOW ...
Posted by 0zkr
on Mar 04, 2013 at 10:09 PM UTC - 5 hrs
Yeah, I'd expand it a bit and make it anything fairly complex. But a lot of code is not very complex, and you end up seeing comments like // loop over the collection, for a contrived example.
Posted by
Sammy Larbi
on Mar 05, 2013 at 06:33 AM UTC - 5 hrs
I think it's nice to see a comment above some regular expressions. If I stare at one on a Monday morning I would really like a bit of a pointer to what it's doing so I don't have to work it all out.
But then again I'm not very good at them ;-)
Posted by Neil Cooper
on Mar 05, 2013 at 06:36 AM UTC - 5 hrs
I could see that.
Posted by
Sammy Larbi
on Mar 05, 2013 at 07:41 AM UTC - 5 hrs
I usually put a one-line comment above sections out of pure habit. I probably don't need to do it because no-one else is actually going to be editing my source. However...
"Code can only tell you how the program works; comments can tell you why it works. Try not to shortchange your fellow developers in either area."
http://www.codinghorror.com/blog/2006/12/code-tell...
Posted by Ali
on Mar 20, 2013 at 02:22 PM UTC - 5 hrs
Leave a comment