Dead code
![]() | It has been suggested that Dead code elimination be merged into this article. (Discuss) Proposed since November 2010. |
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.[1][2] 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.
Example
int foo (int x, int y)
{
int z = x + y;
return x*y;
}
In the above example the sum of x and y is computed but never used. It is thus dead code and can be removed.
Analysis
Dead code elimination is a form of compiler optimization 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.[3]
See also
References
- ^ Debray, S. K., Evans, W., Muth, R., and De Sutter, B. 2000. Compiler techniques for code compaction. ACM Trans. Program. Lang. Syst. 22, 2 (Mar. 2000), 378-415.
- ^ Appel, A. W. 1998 Modern Compiler Implementation in Java. Cambridge University Press.
- ^ Douglas W. Jones Dead Code Maintenance, Risks 8.19 (Feb. 1, 1989)
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