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.

Categories

edit

Notable categories of redundant code include:

Recomputing
Calculating again a value that has previously been calculated[1] and is still available.
Dead code
Code that is executed but has no external effect (i.e., does not change the output produced by a program).
Unreachable code
Code that is never executed (also called dead code).
NOP 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
Something declared, but never referenced, is a redundant declaration.

Examples

edit

In the following C code, the second x * 2 expression is redundant code. Line 2 can be removed, or alternatively, line 3 can be changed to return y;.

int foo(int x) {
    int y = x * 2;
    return x * 2;
}

A more subtle example involves the C preprocessor that inserts code before compilation. Consider:

#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));
}

After preprocessing, the code expands to code that evaluates both a*a + b*b and c*c + d*d twice. To eliminate the duplicate code, the macro min could be converted to a function.

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)));
}

See also

edit

References

edit

📚 Artikel Terkait di Wikipedia

Peephole optimization

register values, removing all of the redundant code in the example above would eventually leave the following code: PUSH AF PUSH BC PUSH DE PUSH HL CALL

Dead code

introduction of side-effects caused by the dead code. Dead-code elimination Redundant code Unreachable code Oxbow code 0xDEADC0DE is a magic number written in

Dead-code elimination

dead-code elimination (DCE, dead-code removal, dead-code stripping, or dead-code strip) is a compiler optimization to remove dead code (code that does

Postal Index Number

Postal Index Number (PIN; sometimes redundantly a PIN Code) refers to a six-digit code in the Indian postal code system used by India Post. The PIN system

Unreachable code

code analysis tool, or even analysis by hand, could be used to decide whether the code is truly unreachable. Code coverage Redundant code Dead code Oxbow

Color code

recommends categorical color codes in seven colors: red, orange, yellow, green, blue, white and black. Adding redundant coding of luminosity and colorfulness

RAS syndrome

RAS syndrome is the redundant use of one or more of the words that make up an acronym in conjunction with the abbreviated form. This means, in effect

Source lines of code

Source lines of code (SLOC), also known as lines of code (LOC), is a software metric used to measure the size of a computer program by counting the number