Monthly Archives: May 2016
I’ve not become a fan of Frozen I promise.
During the last few weeks, I’ve been reading a LOT of code. Mainly other people’s code, and something struck me. There was a LOT of commented out code. And this code was checked in to version control too.
I may be approaching this from a simplistic point of view here, but I can’t fathom why people comment out code, and then check that code in. I’ve heard a variety of reasons, some of them good, some of them, not so good.
But I think I’ve nailed it down to this. Fear!
There’s a fear that we may need the code again at some point in the future.
However I would counter that with the following. If it’s not needed right now, then why is it in the code base? And more importantly, why is it checked in?
Commented out code CAN be useful such as providing an example of using a complex API or such. However in this instance it was code that wasn’t going to be executed as the developer had found a better way of doing it.
So it begs the question, why is the code still there?
I can understand if we weren’t using version control, or if the server was flaky etc, but the servers are fairly robust these days, and besides there’s redundancies as well. So again, why is it there?
I would say that we should be merciless with commented out code. I’ll confess that I am in code reviews. It not only distracts the developer maintaining the code, but also disrupts the flow of the code when you’re reading it too.
We shouldn’t be afraid of removing commented out code from projects. That’s what version control is for. If we need that code again, we can easily get to where the code existed before being removed.
Also, we shouldn’t be afraid of removing code that’s no longer in use! I did exactly that a few weeks. I was working on a legacy product, and there was a section in the project that I didn’t think was being executed.
I grepped for the class names, and found they weren’t being used anywhere else. Once I’d done that, I removed their entries in the Maven pom files and tried to compile. It compiled fine without issue, and I tried to run the product, and that ran fine too. So after that I removed the directories and their contents. Repeated the build and execute steps and functionality wasn’t compromised.
Sometimes the best things we can do for our code, is to actually remove code. Whether it be commented out code, or code that’s never actually executed.