Code Bloat - Examples

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 = '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.latitude; strURL += ","; strURL += markers.longitude; strURL += strSize; strURL += strZoom; strURL += strSensor; for (var i = 0; i < markers.length; i++) { strURL += markers.addMarker; } strFinalImage = strHTMLStart + strURL + strHTMLEnd; return strFinalImage; };

The same logic can be stated more efficiently as follows:

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

Read more about this topic:  Code Bloat

Famous quotes containing the word examples:

    Histories are more full of examples of the fidelity of dogs than of friends.
    Alexander Pope (1688–1744)

    It is hardly to be believed how spiritual reflections when mixed with a little physics can hold people’s attention and give them a livelier idea of God than do the often ill-applied examples of his wrath.
    —G.C. (Georg Christoph)

    There are many examples of women that have excelled in learning, and even in war, but this is no reason we should bring ‘em all up to Latin and Greek or else military discipline, instead of needle-work and housewifry.
    Bernard Mandeville (1670–1733)