Commented out code is dead code

“I’m uncommenting and fixing this code that I commented out a few days ago”, said no programmer ever.

Commenting out code is a nasty habit. I try to avoid it as much as I can. I try to not even use it for comments. On numerous occasions, I’ve seen /*commented out code*/ that has been sitting there for months, judging from the annotation on source control.

What’s the number one reason for committing commented out code? “To keep it as reference in case we want to add it back in the future”. Ok, this is an argument I can rationalize and tolerate. But how about these two thoughts?

  1. If you’re adding back that logic/configuration within a few days, then it’s going to be fresh in your mind, no need to make your code your personal post-it; if after a couple of weeks, those lines are never added back, it probably means they’ll never be used.
  2. What is a VCS for if not for keeping history? You can safely delete any lines you want and be rest assured that you can recover them at any point in the future. In fact I would take this a step further and delete all unused code, whether commented out or not. Don’t keep anything you think you might use in the future. If you do use it in the future, you can dig it out.

But the reason that should not be acceptable for commenting out code is: “I’m commenting out these test cases, so the build is green; I’ll get back to them later”. You’re defeating the purpose of automated testing and continuous integration.

If the code is dead, bury it, if your tests are broken, fix them.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: