Dead code: Difference between revisions

Content deleted Content added
Dorchard (talk | contribs)
No edit summary
Reverting edit(s) by 201.162.227.111 (talk) to rev. 1239272187 by Es20490446e: Vandalism (RW 16.1)
 
(87 intermediate revisions by 62 users not shown)
Line 1:
{{Short description|Computer code that is never executed}}
'''Dead code''' is a [[computer programming]] term for code in the [[source code]] of a program which is executed but whose results is never used in any other computation. <ref>[http://doi.acm.org/10.1145/349214.349233 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.]</ref> The execution of dead code wastes time as its results are never used. Dead code is not equivalent to [[unreachable code]], which is code that can never be executed.
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.<ref>{{Cite web |url=https://www.cs.bu.edu/~hwxi/academic/papers/padl99.pdf |title=Hongwei Xi, Dead Code Elimination through Dependent Types |access-date=2020-05-06 |archive-date=2012-03-10 |archive-url=https://web.archive.org/web/20120310002543/http://www.cs.bu.edu/~hwxi/academic/papers/padl99.pdf |url-status=dead }}</ref><ref>[http://www.do178site.com/do178b_questions.php] {{Webarchive|url=https://web.archive.org/web/20200520200130/http://www.do178site.com/do178b_questions.php |date=2020-05-20 }} DO-178B</ref><ref>[[DO-178B]] Wikipedia/DO-178B.</ref>
'''DeadIn code'''some isareas aof [[computer programming]], term for'''dead code''' is a section in the [[source code]] of a program which is executed but whose resultsresult is never used in any other computation. <ref>[http://doi.acm.org/10.1145/349214.349233 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-415378–415.]</ref><ref>[[Andrew TheAppel|Appel, executionA. ofW.]] dead1998 codeModern wastesCompiler timeImplementation asin itsJava. resultsCambridge areUniversity never usedPress.</ref> DeadThe codeexecution isof not equivalent to [[unreachabledead code]], whichwastes iscomputation codetime thatand can never be executedmemory.
 
While the result of a dead computation may never be used, the dead codeit may raise [[Exception handling|exceptions]] or affect some global state, thus removal of such code may change the output of the program and shouldintroduce notunintended be[[software performedbug|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 [[Method (computer programming)#Static methods|static]] and/or [[Inline function|inline]] functions and enabling the use of [[link-time optimization]].
Dead code elimination is a form of [[compiler optimization]] in which dead code is removed from a program. The dead code elimination technique is in the same class of optimizations as [[unreachable code]] elimination and [[redundant code]] elimination.
 
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 should not be performed. 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 ==
 
<sourcesyntaxhighlight lang='cC'>
int ffoo (int xiX, int yiY)
{
int ziZ = x + yiX/iY;
 
return x iX* yiY;
}
</syntaxhighlight>
</source>
 
In the above example, although the division of {{mono|iX}} by {{mono|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 program analysis|static-code analysis]] and [[data-flow analysis]]. This is in contrast to unreachable code analysis which is based on [[control flow analysis|control-flow analysis]].
 
Dead code elimination is a form of [[compiler optimization]] in which dead code is removed from a program. 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.<ref>[[Douglas W. Jones]] [http://catless.com/Risks/8.19.html#subj2 Dead Code Maintenance, Risks 8.19 (Feb. 1, 1989)] {{Webarchive|url=https://web.archive.org/web/20110708124114/http://catless.com/Risks/8.19.html#subj2 |date=2011-07-08 }}</ref>
 
Some [[Integrated development environment|IDE]]s (such as Xcode, Visual Studio 2010<ref>[http://blogs.msdn.com/b/habibh/archive/2009/07/31/discover-dead-code-in-your-application-using-code-analysis.aspx Habib Heydarian, Microsoft Corp]</ref> and Eclipse Galileo<ref>[http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse.jdt.doc.isv/guide/jdt_api_compile.htm 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.
In the above example the definition of <tt>z</tt> is never used and is thus dead code and can be removed.
 
== See also ==
* [[Redundant Dead-code elimination]]
* [[UnreachableRedundant code]]
* [[Unreachable code]]
* [[Oxbow code]]
* ''0xDEADC0DE'' is a [[Magic number (programming)#Debug values|magic number]] written in [[Hexspeak]] used as a marker in [[OpenWrt|OpenWRT]] firmware
 
== References ==
{{reflistReflist}}
* <cite>Appel, A. W. 1998 Modern Compiler Implementation in Java. Cambridge University Press. </cite>
 
== External links ==
* [https://web.archive.org/web/20110722110715/http://java.net/projects/dcd/pages/Home Dead Code Detector (DCD) simply finds never used code in your Java/JEE applications]
* [https://web.archive.org/web/20110722110615/http://java.net/projects/dcd/pages/Alternatives Comparisons of some Java Dead Code Detector]
* [http://www.ucdetector.org/ UCDetector] Eclipse PlugIn to find dead java code
 
{{DEFAULTSORT:Dead Code}}
[[Category:Software engineering terminology]]
[[Category:SoftwareCompiler metricsconstruction]]
[[Category:Source code]]
[[Category:Software engineering terminologyanomalies]]