In darker times, software management would measure productivity as a function of lines of code. More code means more done, right? Facepalm. When I work with IT management in my capacity as a consultant, I encourage them to view code differently. I encourage them to view code as a liability, like inventory. And when useful code is a liability, think of what a boat anchor dead code is.
I once wrote a fun post about the fate of dead code in your codebase. And while I enjoyed writing that, it had a serious underlying message. Dead code costs you time, money, and maintenance headaches. And it has absolutely no upside.
A Working Definition for Dead Code
Okay. If I’m going to make a blog post out of disparaging dead code, I should probably offer a definition. Let’s do that here.
Some people will draw a distinction between code that can’t be executed (unreachable) and executed code whose effects don’t matter (dead). I acknowledge this definition but won’t use it here. For the sake of simplicity and clarity of message, let’s create a single category of dead code: any code in your codebase that has no bearing on your application’s behavior is, for our purposes here, dead.
The Problems with Dead Code
Having defined it, what’s the problem? If it has no bearing on your application’s behavior, what’s the harm? How does it cost time and money, as I claimed a moment ago?
Well, simply put, your code does not live in a shrink-wrapped vacuum. As your application evolves, developers have to change the code. When you have only code that matters in your codebase, they can do this with the most efficiency. If, on the other hand, you have thousands of lines of useless code, these developers will spend hundreds of hours maintaining that useless code.
Think of having dead code as being reminiscent of running your heat in the winter while keeping all of your windows open. It’s self-defeating and wasteful.
But even worse, it’s a totally solvable problem. Let’s take a look at different types of dead code that you encounter and what you can do about it.