Content deleted Content added
m Bot: Migrating 6 interwiki links, now provided by Wikidata on d:q1251793 (Report Errors) |
Stevebroshar (talk | contribs) →Examples: Simplify example; remove example that is _not_ about redundancy |
||
(25 intermediate revisions by 16 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.
In [[computer programming]], '''redundant code''' is [[source code]] or compiled code in a [[computer program]] that has any form of redundancy, such as recomputing 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 (known as [[unreachable code]]), or code which is executed but has no external effect (e.g., does not change the output produced by a program; known as [[dead code]]).▼
==Categories==
Noteble categoreis of redundant code include:
▲
==Examples==▼
<source lang="C">▼
int foo(int iX)▼
int iY = iX*2;▼
; [[Dead code]]: Code that is executed but has no external effect; e.g., does not change the output produced by a program.
return iX*2;▼
; [[Unreachable code]]: Code that is never executed. Also, called dead code.
; [[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.
; Unused [[identifier]]: Declared, but never referenced is a redundant declaration.
#define min(A,B) ((A)<(B)?(A):(B))▼
▲==Examples==
int shorter_magnitude(int u1, int v1, int u2, int v2)▼
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>
return sqrt(min(u1*u1 + v1*v1, u2*u2 + v2*v2));▼
}
</syntaxhighlight>
<
▲#define min(A,B) ((A)<(B)?(A):(B))
int shorter_magnitude(int u1, int v1, int u2, int v2)▼
}
</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.
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}}
*
* {{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==
|