Jump to content
 







Main menu
   


Navigation  



Main page
Contents
Current events
Random article
About Wikipedia
Contact us
Donate
 




Contribute  



Help
Learn to edit
Community portal
Recent changes
Upload file
 








Search  

































Create account

Log in
 









Create account
 Log in
 




Pages for logged out editors learn more  



Contributions
Talk
 



















Contents

   



(Top)
 


1 Examples  





2 See also  





3 References  














Redundant code






Deutsch
Español
Italiano
Nederlands

Türkçe

 

Edit links
 









Article
Talk
 

















Read
Edit
View history
 








Tools
   


Actions  



Read
Edit
View history
 




General  



What links here
Related changes
Upload file
Special pages
Permanent link
Page information
Cite this page
Get shortened URL
Download QR code
Wikidata item
 




Print/export  



Download as PDF
Printable version
 
















Appearance
   

 






From Wikipedia, the free encyclopedia
 


Incomputer programming, redundant codeissource code or compiled code in a computer program that is unnecessary, such as:

  • recomputing a value that has previously been calculated[1] 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).
  • ANOP instruction might be considered to be redundant code that has been explicitly inserted to pad out the instruction stream or introduce a time delay, for example to create a timing loop by "wasting time". Identifiers that are declared, but never referenced, are termed redundant declarations.

    Examples[edit]

    The following examples are in C.

    int foo(int iX)
    {
        int iY = iX*2;
    
        return iX*2;
    }
    

    The second iX*2 expression is redundant code and can be replaced by a reference to the variable iY. Alternatively, the definition int iY = iX*2 can instead be removed.

    Consider:

    #define min(A,B) ((A)<(B)?(A):(B))
    
    int shorter_magnitude(int u1, int v1, int u2, int v2)
    {
        /* Returns the shorter magnitude of (u1,v1) and (u2,v2) */
        return sqrt(min(u1*u1 + v1*v1, u2*u2 + v2*v2));
    }
    

    As a consequence of using the C preprocessor, the compiler will only see the expanded form:

    int shorter_magnitude(int u1, int v1, int u2, int v2)
    {
        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);
    }
    

    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:

    #define max(A,B) ((A)>(B)?(A):(B))
    
    int random(int cutoff, int range)
    {
        return max(cutoff, rand()%range);
    }
    

    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[edit]

    References[edit]


    Retrieved from "https://en.wikipedia.org/w/index.php?title=Redundant_code&oldid=1143196663"

    Categories: 
    Compiler optimizations
    Software anomalies
    Source code
     



    This page was last edited on 6 March 2023, at 11:44 (UTC).

    Text is available under the Creative Commons Attribution-ShareAlike License 4.0; additional terms may apply. By using this site, you agree to the Terms of Use and Privacy Policy. Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc., a non-profit organization.



    Privacy policy

    About Wikipedia

    Disclaimers

    Contact Wikipedia

    Code of Conduct

    Developers

    Statistics

    Cookie statement

    Mobile view



    Wikimedia Foundation
    Powered by MediaWiki