No Comments Left BehindDecember 01, 2012 -
I would like to explain my hatred for commented-out code. Some might call this hatred extreme. They’re probably right.
When I look at code interspersed with commented-out lines, I think of my own home which is often covered in dog hair. Now my dog is adorable, but he does in fact shed quite a bit.
Now for me, in my home, by myself, this dog hair bothers me maybe 2% of the time I am there. Most of the time I don’t even notice it. For everyone else who comes over, however, they notice it all of the time. It’s on the floor, it’s collecting in corners, it gets in your shoes. They notice it and most of the time it bothers them. I don’t vacuum for myself, I vacuum for them.
Now back to your code. Comments collect in files, sometimes in big ugly piles, and you don’t even notice. You don’t care, you know why those lines are commented out. “Big deal,” you might say. “I might need to use those lines later if someone changes their mind.” Meanwhile, everyone else who looks at that code sees those lines and it bothers them. “Is a feature broken because that line is commented out? Was that just to debug something?” We don’t vacuum for ourselves, we do it for them. We clean up our code and delete those lines so that the next person can read our code without having a panic attack.
Now code isn’t entirely the same as dog hair. Commented-out code could actually be useful in the future. Fortunately, for code, we have an easy solution: version control. I don’t care which one you use, just pick one and use it. You’re probably already using one. If that code you just deleted instead of commenting out really does need to be resurrected, version control will do it.
So take a deep breath, relax, and delete those lines. Trust your version control system, it’s there to make life easier. It’s going to be ok.