Code Bloat

In computer programming, code bloat is the production of program code (source code or machine code) that is perceived as unnecessarily long, slow, or otherwise wasteful of resources. Code bloat can be caused by inadequacies in the programming language in which the code is written, the compiler used to compile it, or the programmer writing it. Thus, while code bloat generally refers to source code size (as produced by the programmer), it can be used to refer instead to the generated code size or even the binary file size.

Examples

The following JavaScript algorithm has a large number of redundant variables, unnecessary logic and inefficient string concatenation.

// Complex 
function TK2getImageHTML(size, zoom, sensor, markers) {
    var strFinalImage = "";
    var strHTMLStart = '<img src="';
    var strHTMLEnd = '" alt="The map"/>';    
    var strURL = "http://maps.google.com/maps/api/staticmap?center=";
    var strSize = '&size='+ size;
    var strZoom = '&zoom='+ zoom;
    var strSensor = '&sensor='+ sensor;    
   
    strURL += markers[0].latitude;
    strURL += ",";
    strURL += markers[0].longitude;
    strURL += strSize;
    strURL += strZoom;
    strURL += strSensor;
    
    for (var i = 0; i < markers.length; i++) {
        strURL += markers[i].addMarker;
    }
    
    strFinalImage = strHTMLStart + strURL + strHTMLEnd;
    return strFinalImage;
};

The same logic can be stated more efficiently as follows:

// Simplified 
function TK2getImageHTML(size, zoom, sensor, markers) {
    var url = [ 'http://maps.google.com/maps/api/staticmap',
        '?center=', markers[0].latitude, ',', markers[0].longitude,
        '&size=', size,
        '&zoom=', zoom,
        '&sensor=', sensor ]; 
    for (var i = 0; i < markers.length; i++) {
        url.push(markers[i].addMarker);
    }
    return '<img src="' + url.join('') + '" alt="The map" />';
}

Code density of different languages

The difference in code density between various computer languages is so great that often less memory is needed to hold both a program written in a "compact" language (such as a domain-specific programming language, Microsoft P-Code, or threaded code), plus an interpreter for that compact language (written in native code), than to hold that program written directly in native code.

Reducing bloat

Some techniques for reducing code bloat include:[1]

  • Code refactoring commonly used code sequence into a subroutine, and calling that subroutine from several locations, rather than copy and pasting the code at each of those locations.
  • Re-using subroutines that have already been written (perhaps with additional parameters), rather than re-writing them again from scratch as a new routine.

See also

References

  1. ^ "Code bloat". DocForge. Retrieved 2009.

  This article uses material from the Wikipedia page available here. It is released under the Creative Commons Attribution-Share-Alike License 3.0.

Code_bloat
 



 

Connect with defaultLogic
What We've Done
Led Digital Marketing Efforts of Top 500 e-Retailers.
Worked with Top Brands at Leading Agencies.
Successfully Managed Over $50 million in Digital Ad Spend.
Developed Strategies and Processes that Enabled Brands to Grow During an Economic Downturn.
Taught Advanced Internet Marketing Strategies at the graduate level.


Manage research, learning and skills at defaultlogic.com. Create an account using LinkedIn to manage and organize your omni-channel knowledge. defaultlogic.com is like a shopping cart for information -- helping you to save, discuss and share.


  Contact Us