Unreachable code
Encyclopedia
Unreachable code is a computer programming
Computer programming
Computer programming is the process of designing, writing, testing, debugging, and maintaining the source code of computer programs. This source code is written in one or more programming languages. The purpose of programming is to create a program that performs specific operations or exhibits a...

 term for code in the source code
Source code
In computer science, source code is text written using the format and syntax of the programming language that it is being written in. Such a language is specially designed to facilitate the work of computer programmers, who specify the actions to be performed by a computer mostly by writing source...

 of a program which can never be executed because there exists no control flow
Control flow
In computer science, control flow refers to the order in which the individual statements, instructions, or function calls of an imperative or a declarative program are executed or evaluated....

 path to the code from the rest of the program.

Unreachable code is sometimes also called dead code, although dead code
Dead code
Dead code is a computer programming term for code in the source code of a program which is executed but whose result is never used in any other computation...

 may also refer to code that is executed but has no effect on the output of a program.

Unreachable code is generally considered undesirable for a number of reasons, including:
  • Occupies unnecessary memory
  • Causes unnecessary caching
    Cache
    In computer engineering, a cache is a component that transparently stores data so that future requests for that data can be served faster. The data that is stored within a cache might be values that have been computed earlier or duplicates of original values that are stored elsewhere...

     of instructions into the CPU instruction cache - which also decreases data locality.
  • 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:
  • programming errors in complex conditional branches;
  • a consequence of the internal transformations performed by an optimizing compiler;
  • incomplete testing of a new or modified program that failed to test the unreachable code;
  • while fixing one bug, a programmer created a second bug that bypassed the unreachable code and was not discovered during testing;
  • obsolete code that a programmer decided not to delete because it was intermingled with functional code;
  • obsolete code that a programmer forgot to delete;
  • previously useful code that will never be reached, because future input data will never cause that code to be executed;
  • complex obsolete code that was intentionally retained but made unreachable so that it could be revived later if needed;
  • debugging constructs and vestigial development code which have yet to be removed from a program.


In the latter five cases, code which is currently unreachable is there as part of a legacy, i.e. code that 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.

Analysis

Detecting unreachable code is a form of static analysis
Static code analysis
Static program analysis is the analysis of computer software that is performed without actually executing programs built from that software In most cases the analysis is performed on some version of the source code and in the other cases some form of the object code...

 and involves performing control flow
Control flow
In computer science, control flow refers to the order in which the individual statements, instructions, or function calls of an imperative or a declarative program are executed or evaluated....

 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
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...

) some forms of unreachable code are explicitly disallowed. The optimization that removes unreachable code is known as dead code elimination
Dead code elimination
In compiler theory, dead code elimination is a compiler optimization to remove code which does not affect the program results. Removing such code has two benefits: it shrinks program size, an important...

.

Code may become unreachable as a consequence of the internal transformations performed by an optimizing compiler (e.g., common subexpression elimination
Common subexpression elimination
In computer science, common subexpression elimination is a compiler optimization that searches for instances of identical expressions , and analyses whether it is worthwhile replacing them with a single variable holding the computed value.- Example :In the following code: a = b * c + g; d = b * c...

).

In practice the sophistication of the analysis performed has a significant impact on the amount of unreachable code that is detected. For example, constant folding and simple flow analysis shows that the statement xyz in the following code is unreachable:


int n = 2 + 1;
if (n 4)
{
xyz
}


However, a great deal more sophistication is needed to work out that the statement xyz is unreachable in the following code:


double x = sqrt(2);
if (x > 5)
{
xyz
}


The unreachable code elimination technique is in the same class of optimizations as dead code
Dead code
Dead code is a computer programming term for code in the source code of a program which is executed but whose result is never used in any other computation...

 elimination and redundant code
Redundant code
Redundant code is a computer programming term for code, which may be source code or compiled code in a computer program, that has any form of redundancy, such as recomputing a value that has previously been calculated and is still available, code that is never executed , or code which is executed...

 elimination.

Unreachability vs. profiling

In some cases, a practical approach may be a combination of simple unreachability criteria and use of a profiler to handle the more complex cases. Profiling in general can not prove anything about the unreachability of a piece of code, but may be a good heuristic
Heuristic
Heuristic refers to experience-based techniques for problem solving, learning, and discovery. Heuristic methods are used to speed up the process of finding a satisfactory solution, where an exhaustive search is impractical...

for finding potentially unreachable code. Once a suspect piece of code is found, other methods, such as a more powerful code analysis tool, or even analysis by hand, could be used to decide whether the code is truly unreachable.
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK