loading...
Cover image for The Risks of Dead Code

The Risks of Dead Code

codebyamir profile image Amir Boroumand Updated on ・2 min read

Originally published on my blog at www.codebyamir.com

Overview

Dead code is code that is never executed. This can be a method that's no longer called, a commented out block of code, or code appearing after a return statement that's unreachable.

In any case, it often reflects functionality that no longer exists in the software and offers no value.

In this article, we'll explore the risks presented by dead code in a software project.

Dead code adds complexity

Dead code makes understanding a codebase more challenging. A developer must figure out which parts of the code are no longer used which takes considerable time, effort, and investigation.

Imagine spending a few hours understanding how a feature works and then discovering that the code implementing the feature is no longer called because it was replaced by a better implementation.

Dead code is misleading

Blocks of commented code raise more questions than answers. Is this a future TODO? Was this code buggy or slow?

There is no reason for commented code to exist because source control can always take us back to a previous version.

Dead code can come back to life

There is a risk that someone may inadvertently call the dead code and bring it back to life. This can lead to bugs and potentially disastrous results.

I recently came across a method that implemented a global password reset feature on a production site. The front-end code had been removed years ago, but the back-end code was still there. If anyone called this method accidentally, it would cause a lot of headaches and customer complaints.

During the summer of 2012, Knight Capital Group caused a major stock market disruption and suffered a loss of over $400 million when a botched software deployment caused dead order handling code to be executed. The code had not been tested in many years and resulted in a deluge of orders hitting the market that could not be cancelled.

Posted on by:

codebyamir profile

Amir Boroumand

@codebyamir

Software Engineer specializing in backend Java development of web applications

Discussion

markdown guide
 

I'm always repeating on code reviews to remove unused code. No matter this is a few lines of commented code, unused function or whole component. Due to Git or another source control we can revert it when there is a need.

In my opinion, removing dead code is so essential like washing hands after the toilet.

washing

 

Due to Git or another source control we can revert it when there is a need.

So much this! One thing I've been wondering about is what's the best way to discover old code that can be useful again; for example, in a project I had a class I'd written to generate PDFs that I removed because it had become dead code. A year or so later, I found myself needing it again, so I pulled it out of Git history. Now, I knew it was there, but if someone new had taken over the project, they wouldn't neccessarily know that. I was thinking about maybe tagging commits that remove potentially useful chunks of code, but that's more work and isn't all-encompassing. Do you have any thoughts on the best ways to discover hidden gems in a project's history, Rafal?

 

In case of dead function or method, I do nothing. In my opinion, it would be easier and faster to rewrite the piece of code again instead of searching for it somewhere in the source control history.
If there is something bigger like complete functionality or library, I'm removing it in separate commit adding a suitable tag to tag to it (for searching purposes).
In most cases, someone from the team remembers where to find removed functionality, but I have to add that only twice I needed to revert deleted code.

 

Very nice and concise article! I'd like to add another drawback:

Dead code inflates reports, it alters how many lines of code there is, classes, %of comented code..

Static analysis tools may be altered too.

 

So true. Even more in this "convention over configuración" era where the code is more diffficult to trace

 

Just want to point out that although bad, commented code is not dead code as per MISRA and DO-178 standard.

 

This reminds me also of the Ariane 5 rocket.