Dead Code

Fazlan Nazeem
3 min readJul 10, 2021

--

What is dead code?

As programmers, you may have come across

  • code that exists within a codebase, but never executes in the runtime.
  • code that executes, but the result is never used.

Nobody in your team knows why it exists, and nobody has the courage to remove it. Most likely this can be a piece of code that was used for some functionality in the past which is not used anymore. With time, as your code grows with new features and bug fixes, the possibility of pieces of dead code lying around in various modules of your system becomes exponentially higher.

How to identify them?

Dead code can exist in the form of entire functions/methods. Sometimes entire source files. It could also be entire packages/modules. It could also be a one-line statement or an if condition that can never happen.

Modern IDEs do a wonderful job of identifying dead code. It is able to highlight code sections that are never used and makes the job much easier for the programmer to find them. Some compilers issue a warning during compilation if such code exists. Some compilers force to remove unused code and deny compilation otherwise. However, these tools have their limitations. Think about a feature that has been deprecated and removed from the UI. Most likely, the IDE will not help you in these situations to identify the backend code related to this feature. Therefore being mindful about such occurrences is also paramount rather than relying completely on automated tools.

What happens when you have dead code lying around?

When you are working with a team, contributing to a common code base as the rest, there is one task that you do more than anything else: reading other people’s code. It is by reading code that other programmers wrote, and understanding it, we can decide on how to integrate the change we intend to do in the most effective manner. Think about the last time you had to do a change in some strange piece of code that you did not understand properly and wishing that it would do what you intended.

When people read through the dead code,

  • Brain cycles are wasted.
  • Confuses them.
  • Wrong assumptions are made
  • Unnecessary complexity is added on top of them

It can also affect build time and startup time depending on the scale of it.

Therefore it is essential to remove dead code as soon as you see them. This is not a one-time task nor a task that a single person can perform. The team has to understand the implications of letting unnecessary code lie around for no reason and take this as a collective responsibility.

Summary

Experienced programmers try their best to write code that others can understand with less effort. One task which could help achieve this goal is to remove unnecessary code as soon as you encounter it. In case of a mistake, you can always roll back: everybody uses version control nowadays.

--

--