In my humble opinion the worst code smell you can have is Dead Code. I know that other smells such as large methods, large classes, overly complex logic, interwoven dependencies, etc are all smells, but nothing, I MEAN NOTHING smells worse than dead code. Let me explain.
**** BTW, I am not talking about commented out code (which is another smell), I am talking about code that is actually executed, but not used *****
In most systems today (even the good ones) code is not accurately commented or documented, if it is commented or documented at all. As you are looking though code and you run across logic that 'looks needed' but is not being used, what do you do. I assume you have to stop and ask yourself 'Should this be used'?
Here is a classic example of why dead code smells.
Lets say you are on the hunt for a pesky bug and you stumble into a method that you think is the cause. As you are stepping though and reading the code you notice some dead code. At first you pass it by thinking it is not needed, then you stop and take a double take. At this point you realize that code may be useful, but since it is not being used, you have to start making assumptions. Is the dead code an accident? Was it done with intent?
Now what do you do?
We all know that when you start making assumptions you are destined for trouble. A good developer has to think that the previous author had good intent when they made their code changes, but since the code is left over, you are not sure what to do. Because the dead code is left over you are at a cross roads, should I delete it, or should I try to determine how it was meant to be used. It is here you get into trouble. You are forced to try to put together the puzzle without enough information to make a educated guess. If the original author would have just removed the dead code, the new author would be free to make his/her own decision on how to solve the problem at had.
One other thing about dead code, it wastes cycle time. You are slowing down your application by executing code that is not used.
Moral of the story, if you are not using the code delete it. Or, if you must, comment it out and tell WHY you are not using it so the next guy does not have to make assumptions.
Till next time,
***** Note, if you are using Resharper, it will flag dead code for you. This will make removing it much easier *****
Posted
02-14-2008 5:53 AM
by
Derik Whittaker