Code bloat
This article needs additional citations for verification. (June 2014) |
In computer programming, code bloat is the production of executable code (source code or machine code) that is 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
[edit]The following algorithm is written in JavaScript, and can generate an HTML ‹img› tag based on user input. It contains a large number of redundant code: unnecessary logic and variables, and inefficient string concatenation.
// Complex
function TK2getImageHTML(size, zoom, sensor, markers) {
const strHTMLStart = '<img src="';
const strHTMLEnd = '" alt="The map"/>';
var strFinalImage = "";
var strURL = "https://example.com/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 algorithm above can be rewritten to be less redundant and more efficient as follows (although not as readable):
// Simplified
function TK2getImageHTML(sz,zm,sens,mks){
let url=""
mks.forEach(mk => url += mk.addMarker())
return `<img src="https://example.com/staticmap?center=${mks.latitude},${mks.longitude}&size=${sz}&zoom=${zm}&sensor=${sens+url}" alt="The map" />`
}
Code density of different languages
[edit]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
[edit]Some techniques for reducing code bloat include:[1]
- Code refactoring a 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 (copy-and-paste programming).
- Re-using subroutines that have already been written (perhaps with additional parameters), rather than re-writing them again from scratch as a new routine.
- Combine program analysis to detect bloated code, with program transformation to remove bloated code.
- Disabling/removing certain program optimizations, as they may actually hurt performance.
- Using code minifiers/obfuscators that strip unneeded whitespace/indentation and other things unimportant to a program running.
See also
[edit]- Dead code elimination
- Minimalism (computing)
- Muntzing
- Polymorphism (computer science)
- Software optimization
- Software bloat
- Lightweight software
References
[edit]- ^ "Code bloat". DocForge. Archived from the original on 5 March 2016. Retrieved 30 December 2009.