Redundant code: Difference between revisions

Content deleted Content added
Added punctuation omission.
Examples: Simplify example; remove example that is _not_ about redundancy
 
(37 intermediate revisions by 28 users not shown)
Line 1:
In [[computer programming]], '''redundant code''' is [[source code]] or compiled code that is unnecessary. Code that can be removed without affecting its desired behavior is redundant.
{{TOC right}}
 
==Categories==
{{Unreferenced section|date=October 2008}}
Noteble categoreis of redundant code include:
 
'''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 asRecomputing: recomputingRecomputing a value that has previously been calculated<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> and is still available, code that is never executed (often called [[unreachable code]]), or code which is executed but has no external effect (e.g., does not change the output produced by a program), usually known as [[dead code]].
 
; [[Dead code]]: Code that is executed but has no external effect; e.g., does not change the output produced by a program.
A [[NOP]] instruction might be considered to be redundant code that has been explicitly inserted to pad out the [[instruction (computer science)|instruction]] stream or introduce a time delay. [[Identifier]]s that are declared but never referenced are usually termed as [[redundant declaration]]s.
 
; [[Unreachable code]]: Code that is never executed. Also, called dead code.
==Example==
<source lang="c">
int f (int x)
{
int y=x*2;
return x*2;
}
</source>
 
; [[NOP (code)|NOP]] padding: A NOP instruction might be considered redundant if it's for padding. But if the NOP is required for proper functionality then it's not redundant.
The second <tt>x*2</tt> expression is redundant code and can be replaced by a reference to the variable <tt>y</tt>. Alternatively, the definition <tt>int y=x*2</tt> can instead be removed.
 
; Unused [[identifier]]: Declared, but never referenced is a redundant declaration.
 
==Examples==
In the following [[C (computer language)|C]] code, the second <code>x * 2</code> expression is redundant code. Line 2 can be removed, or alternatively, line 3 can be changed to {{code| return y;}}.
 
<syntaxhighlight lang="C" line>
int f foo(int x) {
int y = x * 2;
return x * 2;
}
</syntaxhighlight>
 
A more subtle example involves the C preprocessor that inserts code before compilation. Consider:
 
<syntaxhighlight lang="C">
#define min(A,B) ((A)<(B)?(A):(B))
int shorter_magnitude(int a, int b, int c, int d) {
return sqrt(min(a*a + b*b, c*c + d*d));
}
</syntaxhighlight>
 
After preprocessing, the code expands to code that evaluates both {{code|a*a + b*b}} and {{code|c*c + d*d}} twice. To eliminate the duplicate code, the macro {{code|min}} could ge converted to a function.
 
<syntaxhighlight lang="C">
int shorter_magnitude(int a, int b, int c, int d) {
return sqrt(((a*a + b*b)<(c*c + d*d)?(a*a + b*b):(c*c + d*d)));
}
</syntaxhighlight>
 
==See also==
* {{Annotated link |Code bloat}}
{{Portal|Computer Science}}
* {{Annotated link |Code reuse}}
*[[Code bloat]]
* {{Annotated link |Common subexpression elimination}}
* {{Annotated link |Don't repeat yourself}}
* {{Annotated link |Duplicate code}}
* {{Annotated link |Redundancy (information theory)}}
* {{Annotated link |Code refactoring}}
* {{Annotated link |Code smell}}
 
==References==
Line 26 ⟶ 55:
 
[[Category:Compiler optimizations]]
[[Category:Software engineering terminologyanomalies]]
[[Category:Source code]]
 
[[it:Codice ridondante]]
[[lt:Perteklinis kodas]]
[[nl:Redundante code]]