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.
Noteble categoreis of redundant code include:
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;
.
intfoo(intx){inty=x*2;returnx*2;}
A more subtle example involves the C preprocessor that inserts code before compilation. Consider:
#define min(A,B) ((A)<(B)?(A):(B))intshorter_magnitude(inta,intb,intc,intd){returnsqrt(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 ge converted to a function.
intshorter_magnitude(inta,intb,intc,intd){returnsqrt(((a*a+b*b)<(c*c+d*d)?(a*a+b*b):(c*c+d*d)));}