Common Excuses Used To Comment Code and What To Do About Them

Common Excuses Used To Comment Code and What To Do About Them

Posted by  Sam on Jun 18, 2008 at 12:00 AM UTC - 5 hrs
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  aprogram. 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 towhat 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 amethod 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 everwork? (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:
  1. In the styles of Javadoc, RubyDoc, et cetera for documenting APIs others will use.
  2. 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.
  3. TODOs, which should be the exception, not the rule
  4. 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 blurbRSS 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!
 
源网址:http://www.codeodor.com/index.cfm/2008/6/18/Common-Excuses-Used-To-Comment-Code-and-What-To-Do-About-Them/2293
 

posted on 2013-10-17 14:27  弘晓  阅读(153)  评论(0)    收藏  举报

导航