Java Developer Tools

Dead Code Analysis

Dead code or unreachable code is a computer programming term for code in the source code of a program which can never be executed because there exists no control flow path to the code from the rest of the program.[1] or code that is executed but has no effect on the output of a program.

Dead code is generally considered undesirable for a number of reasons, including:

  • Occupies unnecessary memory
  • From the perspective of program maintenance; time and effort may be spent maintaining and documenting a piece of code which is in fact unreachable, hence never executed.

Causes

The existence of unreachable code can be due to various factors, such as:

  • complex conditional branches in which a case is never reachable;
  • as a consequence of the internal transformations performed by an optimizing compiler
  • improper maintenance of a program or from debugging constructs and vestigial development code which have yet to be removed from a program.

In the latter case, code which is currently unreachable is there as part of a legacy. The distinguishing point in that case is that this part of code was once useful but is no longer used or required.

Examples

Consider the following fragment of C code:

 int f (int x, int y)
 {
  return x+y;
  int z=x*y;
 }
The definition int z=x*y; is never reached as the function returns before the definition is reached. Therefore the definition of z can be discarded.

Conclusion

Detecting unreachable code is a form of static analysis and involves performing control flow analysis to find any code that will never be executed regardless of the values of variables and other conditions at run time. In some languages (e.g. Java) some forms of unreachable code are explicitly disallowed. The optimization that removes unreachable code is known as dead code elimination.

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.