Dead code
Encyclopedia
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. The execution of dead code wastes computation time as its results are never used.
While the result of a dead computation may never be used the dead code may raise exceptions or affect some global state, thus removal of such code may change the output of the program and introduce unintended bugs
. Compiler optimizations are typically conservative in their approach to dead code removal if there is any ambiguity as to whether removal of the dead code will affect the program output.
In the above example the sum of x and y is computed but never used. It is thus dead code and can be removed.
In the above example "do something" is never executed, and so it is dead code.
in which dead code is removed from a program. Dead code analysis can be performed using live variable analysis
, a form of static code analysis
and data flow analysis. This is in contrast to unreachable code
analysis which is based on control flow
analysis.
The dead code elimination technique is in the same class of optimizations as unreachable code
elimination and redundant code
elimination.
In large programming projects, it is sometimes difficult to recognize and eliminate dead code, particularly when entire modules become dead. Test scaffolding can make it appear that the code is still live, and at times, contract language can require delivery of the code even when the code is no longer relevant.
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 is executed but whose result is never used in any other computation. The execution of dead code wastes computation time as its results are never used.
While the result of a dead computation may never be used the dead code may raise exceptions or affect some global state, thus removal of such code may change the output of the program and introduce unintended bugs
Software bug
A software bug is the common term used to describe an error, flaw, mistake, failure, or fault in a computer program or system that produces an incorrect or unexpected result, or causes it to behave in unintended ways. Most bugs arise from mistakes and errors made by people in either a program's...
. Compiler optimizations are typically conservative in their approach to dead code removal if there is any ambiguity as to whether removal of the dead code will affect the program output.
Example
In the above example the sum of x and y is computed but never used. It is thus dead code and can be removed.
In the above example "do something" is never executed, and so it is dead code.
Analysis
Dead code elimination is a form of compiler optimizationCompiler optimization
Compiler optimization is the process of tuning the output of a compiler to minimize or maximize some attributes of an executable computer program. The most common requirement is to minimize the time taken to execute a program; a less common one is to minimize the amount of memory occupied...
in which dead code is removed from a program. Dead code analysis can be performed using live variable analysis
Live variable analysis
In compiler theory, live variable analysis is a classic data flow analysis performed by compilers to calculate for each program point the variables that may be potentially read before their next write, that is, the variables that are live at the exit from each program point.Stated simply: a...
, a form of static code 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 data flow analysis. This is in contrast to unreachable code
Unreachable code
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....
analysis which is based on 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.
The dead code elimination technique is in the same class of optimizations as unreachable code
Unreachable code
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....
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.
In large programming projects, it is sometimes difficult to recognize and eliminate dead code, particularly when entire modules become dead. Test scaffolding can make it appear that the code is still live, and at times, contract language can require delivery of the code even when the code is no longer relevant.
External links
- Dead Code Detector (DCD) simply finds never used code in your Java/JEE applications
- Comparisons of some Java Dead Code Detector
- UCDetector Eclipse PlugIn to find dead java code