Dead code
![]() | It has been suggested that Dead code elimination be merged into this article. (Discuss) Proposed since November 2010. |
In computer programming, dead code is a 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 iX, int iY)
{
int iZ = iX/iY;
return iX*iY;
}
In the above example, although the division of iX by iY is computed and never used, it will throw an exception when a division by zero occurs. Therefore the removal of the dead code may change the output of the program.
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]
Some IDEs (such as Visual Studio 2010 [4] and Eclipse Galileo [5]) have the ability to locate dead code during the compiling stage.
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)
- ^ Habib Heydarian, Microsoft Corp.[1]
- ^ Eclipse Developer Guide[2]
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