Geeks With Blogs
Dave Chestnutt: SparklingCode and CodeGaffes Writing better code; for fun and profit

We all have old code snippets in our code base. Whether it’s a method that’s no longer used, or a few lines that we’ve replaced - our code has sections commented out. When should we remove them? How should we comment them out? If you’re not careful, commented out code can cause future problems. Read on.

Code Gaffe #1: Sneaky Commented out code

Commented out code should be, well, really commented out.

 

Really comment it out

Don't put a /* at the top and */ at the bottom of code you want to remove:

 

/*

code block line 1

code block line 2

*/

 

Why? If the code block is of any size, someone reading the file may not realize that the code is commented out. And you're simply causing them to waste time. Instead, comment out each line:

 

//

// code block line 1

// code block line 2

//

 

Of course, in modern editors with syntax coloring, this should not be an issue because the commented out code will be colorized as a comment. But we don't all have color printers, and sometimes we still use printouts. And we use other tools besides editors (like file differencing tools, change reports, etc.) that don't have syntax smarts.

 

Besides, if you have a modern editor (and even with some ancient ones), you can select the code block to comment out, press a key, and it'll add "//" to every line - so there's really no excuse to not do it this way.

 

I knew a guy who was very puzzled while trying to fix code in an overloaded method that he didn't realize was commented out. Due to the nature of the code, he couldn't use a debugger, he had to dump info to a log file for debugging (and he didn't have syntax coloring). After wasting a day doing this, he finally saw the comment markers on an early page, and with appropriate *&^*% language, found the correct overload.

 

CodeGaffe #2: Old Commented out Code

The real reason we can't bear to delete old code is that we spent so much time writing the code in the first place

Of course, the best tack by far is to delete the unused code. The code will rot when it's unused, and after a while, you wouldn't be able to simply uncomment it anyway. If you really can't bear to delete it, make it obvious that it's commented out.

 

What do I mean by "rot"? Once the code is commented out, it's never getting executed. As the rest of the code base evolves (adding new features, fixing bugs, refactoring), the code will be out of date—perhaps very subtly.

 

Delete the old code (I know, it’s hard to part with it )

The rule of thumb I use is to comment out code if I think I might need it again very soon. Then, the very next time I'm in the same module, I delete it. Thanks to the wonders of source control systems, it's now very easy to see that I intended to remove the code, since it'll be commented out in one version and gone in the next. That's a little more obvious than simply removing it.

 

This suggestion (comment then delete) is really for those of us who hate to delete the code outright, "because I might want it back someday". Usually though, the real reason we can't bear to part with it is that we spent a lot of time writing the code in the first place. But my advice is to delete it once it's no longer used.

 

So clean up that old code. Get to it!

 

Technorati tags: ,

Posted on Sunday, August 20, 2006 4:16 PM CodeGaffes | Back to top


Comments on this post: CodeGaffe: Commented Out Code

# re: CodeGaffe: Commented Out Code
Requesting Gravatar...
Very interesting topic, can you post some further information on this subject.
Thats great.i chose the right one for the right situation.thanks for sharing.
Left by Writing Essays Services on Jul 10, 2010 2:03 AM

# re: CodeGaffe: Commented Out Code
Requesting Gravatar...
Thanks for the information you shared that's so useful and quite informative and i have taken those into consideration....
Left by Buy University Essays UK on Jul 10, 2010 2:05 AM

# re: CodeGaffe: Commented Out Code
Requesting Gravatar...
Your experience is great and learning for other people.
Left by Email Marketing California USA on Jul 10, 2010 2:12 AM

# re: CodeGaffe: Commented Out Code
Requesting Gravatar...
what a great style, it not easy job well don.
Left by Real Estate, Plots, Apartments on Jul 10, 2010 2:13 AM

# re: CodeGaffe: Commented Out Code
Requesting Gravatar...
your post is so informative. i got few new thing , i had no knowledge about.
Left by Brisbane Property Investment on Oct 16, 2010 4:12 AM

# re: CodeGaffe: Commented Out Code
Requesting Gravatar...
Fantastic i am very glad for this article. A Exceptional page of knowledge.
Left by Every Other Day Diet on Oct 16, 2010 4:43 AM

# re: CodeGaffe: Commented Out Code
Requesting Gravatar...
your article really help me a lot.thank very much.
Left by ffxiv gil on Oct 21, 2010 1:29 AM

# re: CodeGaffe: Commented Out Code
Requesting Gravatar...
very nice work done, thank you for sharing it..===========
buy electronics online
Left by buy electronics online on Oct 05, 2011 6:51 AM

Your comment:
 (will show your gravatar)


Copyright © Dave Chestnutt | Powered by: GeeksWithBlogs.net