# Redundant code

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/Redundant_code
> Markdown URL: https://mediated.wiki/source/Redundant_code.md
> Source: https://en.wikipedia.org/wiki/Redundant_code
> Source revision: 1355270924
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

In [computer programming](/source/computer_programming), '''redundant code''' is [source code](/source/source_code) or compiled code that is unnecessary. Code that can be removed without affecting its desired behavior is redundant.

==Categories==
Notable categories of redundant code include:

; Recomputing: Calculating again 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.

; [Dead code](/source/Dead_code): Code that is executed but has no external effect (i.e., does not change the output produced by a program).

; [Unreachable code](/source/Unreachable_code): Code that is never executed (also called dead code).

; [NOP](/source/NOP_(code)) padding: A NOP instruction might be considered redundant if it is for padding. But if the NOP is required for proper functionality, then it is not redundant.

; Unused [identifier](/source/identifier): Something declared, but never referenced, is a redundant declaration.

==Examples==
In the following [C](/source/C_(computer_language)) 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 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 be 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}}
* {{Annotated link |Code reuse}}
* {{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==
{{Reflist}}

Category:Compiler optimizations
Category:Software anomalies
Category:Source code

---
Adapted from the Wikipedia article [Redundant code](https://en.wikipedia.org/wiki/Redundant_code) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/Redundant_code?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
