What is dead code?


What is dead code?

Looks like it depends on who you ask. Some people think it’s unreachable code than never executes. Other people think that it’s redundant code that does execute, but has no influence on the final outcome.

def square(x):
   y = x*10                    # redundant code
   return x*x
   print("We never get here")  # unreachable code

We just had this confusion play out at work: I asked my colleague to delete the “dead code” meaning unreachable code, but he ended up deleting not only that code, but also the code he thought was redundant.

When you google “dead code”, Google AI overview say:

Dead code is a segment of code unnecessary for the successful operation of a particular software program. These are sections of code that are executed, but not used, accessed, or referenced during software operation.

On the other hand, many web sites define “dead code” as “code that is never executed”. E.g. Python deadcode package says it “removes dead code”, and defines it as “variables/functions/classes/files which are not used in a whole code base.

Wikipedia article for dead code has both definitions, and cites the sources accordingly:

The term dead code has multiple definitions. Some use the term to refer to code (i.e. instructions in memory) which can never be executed at run-time. In some areas of computer programming, dead code is a section in the source code of a program which is executed but whose result is never used in any other computation.

Thus, in order to avoid confusion, it is advisable to use terms “unreachable code” when we are talking about something that is never executed, and “redundant code” when we are talking about something that may or may not be executed, but has no influence on the final outcome.

PS. It can sometimes get rather murky what is redundant or even what is unreachable. Suppose we have this check:

if square(x) < 0:
   raise ValueError("The square is negative!")  # is it unreachable?

Is the raise statement unreachable? If square function works as expected, then it is. But if the function is complex (pun intended), it may not work as we expect, and negative squares may happen. One’s man unreachable code is another’s defensive programming.

2 Comments


  1. Most “interesting” things start to happen when people use reflection.
    It happened to me couple of times – I removed couple of unused properties in C# class and the system would crash.
    Turns out some external caller used those two via reflection to keep some retry counting 🙂

    Reply

    1. Yep. Happened to me as well. The “good” thing in Python/Javascript is that you must assume people may use “reflection” (so to speak), and before removing, you search the codebase for the property name. Of course, if they did something like getattr(“foo”+”bar”) to find property “foobar”, or if they read the property name from an environment variable, then you’re out of luck.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *