The term dead code has multiple definitions. Some use the term to refer to code (i.e. instructions in memory) which can never be executed at run-time.
In some areas of
computer programming
Computer programming is the process of performing a particular computation (or more generally, accomplishing a specific computing result), usually by designing and building an executable computer program. Programming involves tasks such as ana ...
, dead code is a section in the
source code
In computing, source code, or simply code, is any collection of code, with or without comments, written using a human-readable programming language, usually as plain text. The source code of a program is specially designed to facilitate the wo ...
of a program which is executed but whose result is never used in any other computation. The execution of dead code wastes computation time and memory.
While the result of a dead computation may never be used, it 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. The programmer may aid the compiler in this matter by making additional use of
static
Static may refer to:
Places
*Static Nunatak, a nunatak in Antarctica
United States
* Static, Kentucky and Tennessee
*Static Peak, a mountain in Wyoming
**Static Peak Divide, a mountain pass near the peak
Science and technology Physics
*Static el ...
and/or
inline functions and enabling the use of
link-time optimization
Interprocedural optimization (IPO) is a collection of compiler techniques used in computer programming to improve performance in programs containing many frequently used functions of small or medium length. IPO differs from other compiler optimiz ...
.
Example
int foo (int iX, int iY)
In the above example, although the division of by 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
In compiler theory, dead-code elimination (also known as DCE, dead-code removal, dead-code stripping, or dead-code strip) is a compiler optimization to remove code which does not affect the program results. Removing such code has several benefit ...
is a form of
compiler optimization
In computing, an optimizing compiler is a compiler that tries to minimize or maximize some attributes of an executable computer program. Common requirements are to minimize a program's execution time, memory footprint, storage size, and power con ...
in which dead code is removed from a program. Dead code analysis can be performed using
live-variable analysis In compilers, live variable analysis (or simply liveness analysis) is a classic data-flow analysis to calculate the variables that are ''live'' at each point in the program. A variable is ''live'' at some point if it holds a value that may be neede ...
, a form of
static-code analysis and
data-flow analysis
In computing, dataflow is a broad concept, which has various meanings depending on the application and context. In the context of software architecture, data flow relates to stream processing or reactive programming.
Software architecture
Dataf ...
. This is in contrast to unreachable code analysis which is based on
control-flow analysis
In computer science, control-flow analysis (CFA) is a static-code-analysis technique for determining the control flow of a program. The control flow is expressed as a control-flow graph (CFG). For both functional programming languages and object- ...
.
The dead-code elimination technique is in the same class of optimizations as
unreachable code In computer programming, unreachable code is part of 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.
Unreachable code is sometimes also called ''dead code' ...
elimination and
redundant code In computer programming, redundant code is source code or compiled code in a computer program that is unnecessary, such as:
* recomputing a value that has previously been calculated and is still available,
* code that is never executed (known as unr ...
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.
Some
IDEs (such as Xcode, Visual Studio 2010 and Eclipse Galileo
Eclipse Developer Guide
/ref>) have the ability to locate dead code during the compiling stage.
While most optimization techniques seek to remove dead code in an implementation, in extreme forms of optimization for size it may sometimes be desirable to deliberately introduce and carefully shape seemingly dead code, when it allows to fold otherwise unrelated code sections together (and thereby reduce their combined size) so that the extra code will effectively not harm the first path of execution through the code but is used to carry out the actions necessary for the alternative paths of execution, for which other sections of the code may become dead code. On a more functional level, this can be seen as both, artificially introduction of harmless/useful side-effects and reduction of the redundancy of the code, but it can also be used down to opcode level in order to allow the usage of shorter instructions, which would not be possible when folding code sequences without the concerted introduction of side-effects caused by the dead code.
See also
* Dead-code elimination
In compiler theory, dead-code elimination (also known as DCE, dead-code removal, dead-code stripping, or dead-code strip) is a compiler optimization to remove code which does not affect the program results. Removing such code has several benefit ...
* Redundant code In computer programming, redundant code is source code or compiled code in a computer program that is unnecessary, such as:
* recomputing a value that has previously been calculated and is still available,
* code that is never executed (known as unr ...
* Unreachable code In computer programming, unreachable code is part of 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.
Unreachable code is sometimes also called ''dead code' ...
* Oxbow code
* Dead store
* ''0xDEADC0DE'' is a magic number written in Hexspeak Hexspeak, like leetspeak, is a novelty form of variant English spelling using the hexadecimal digits. Created by programmers as memorable magic numbers, hexspeak words can serve as a clear and unique identifier with which to mark memory or data.
H ...
used as a marker in OpenWRT
OpenWrt (from ''open wireless router'') is an open-source project for embedded operating systems based on Linux, primarily used on embedded devices to route network traffic. The main components are Linux, util-linux, musl, and BusyBox. All com ...
firmware
References
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
{{DEFAULTSORT:Dead Code
Compiler construction
Source code
Software anomalies