Redundant code: Difference between revisions

Content deleted Content added
change source to syntaxhighlight
Examples: Simplify example; remove example that is _not_ about redundancy
 
(11 intermediate revisions by 5 users not shown)
Line 1:
In [[computer programming]], '''redundant code''' is [[source code]] or compiled code inthat ais [[computerunnecessary. program]]Code that iscan unnecessary,be suchremoved without affecting its desired behavior is as:redundant.
* 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]]),
* 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==
A [[NOP (code)|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, for example to create a [[timing loop]] by "wasting time". [[Identifier]]s that are declared, but never referenced, are termed '''redundant declarations'''.
Noteble categoreis of redundant code include:
 
*; recomputingRecomputing: 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,.
==Examples==
The following examples are in [[C (computer language)|C]].
 
*; [[Dead code]]: Code whichthat is executed but has no external effect; (e.g., does not change the output produced by a program;. known as [[dead code]]).
<syntaxhighlight lang="C">
int foo(int iX)
{
int iY = iX*2;
 
*; [[Unreachable code]]: Code that is never executed. (knownAlso, ascalled [[unreachabledead code]]),.
return iX*2;
}
</syntaxhighlight>
 
; [[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 <code>iX*2</code> expression is redundant code and can be replaced by a reference to the variable <code>iY</code>. Alternatively, the definition <code>int iY = iX*2</code> can instead be removed.
 
; Unused [[identifier]]: Declared, but never referenced is a redundant declaration.
Consider:
 
==Examples==
<syntaxhighlight lang="C">
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;}}.
#define min(A,B) ((A)<(B)?(A):(B))
 
<syntaxhighlight lang="C" line>
int shorter_magnitude(int u1, int v1, int u2, int v2)
int foo(int iXx) {
{
int iYy = iXx * 2;
/* Returns the shorter magnitude of (u1,v1) and (u2,v2) */
return sqrt(min(u1*u1x + v1*v1, u2*u2 + v2*v2))2;
}
</syntaxhighlight>
 
AsA amore consequencesubtle ofexample usinginvolves the [[C preprocessor]], thethat compilerinserts willcode only see thebefore expandedcompilation. formConsider:
 
<syntaxhighlight lang="C">
#define min(A,B) ((A)<(B)?(A):(B))
int shorter_magnitude(int u1, int v1, int u2, int v2)
int shorter_magnitude(int u1a, int v1b, int u2c, int v2d) {
{
return sqrt(tempmin(a*a + b*b, c*c + d*d));
int temp;
if (u1*u1 + v1*v1 < u2*u2 + v2*v2)
temp = u1*u1 + v1*v1; /* Redundant already calculated for comparison */
else
temp = u2*u2 + v2*v2; /* Redundant already calculated for comparison */
return sqrt(temp);
}
</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.
Because the use of min/max macros is very common, modern compilers are programmed to recognize and eliminate redundancy caused by their use.
 
There is no redundancy, however, in the following code:
<syntaxhighlight lang="C">
int shorter_magnitude(int u1a, int v1b, int u2c, int v2d) {
#define max(A,B) ((A)>(B)?(A):(B))
return sqrt(((a*a + b*b)<(c*c + d*d)?(a*a + b*b):(c*c + d*d)));
 
int random(int cutoff, int range)
{
return max(cutoff, rand()%range);
}
</syntaxhighlight>
 
If the initial call to rand(), modulo range, is greater than or equal to cutoff, rand() will be called a second time for a second computation of rand()%range, which may result in a value that is actually lower than the cutoff. The max macro thus may not produce the intended behavior for this function.
 
==See also==
* [[{{Annotated link |Code bloat]]}}
* {{Annotated link |Code reuse}}
* [[Duplicate code]]
* {{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==