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:

    In the examples that I here bring in of what I have [read], heard, done or said, I have refrained from daring to alter even the smallest and most indifferent circumstances. My conscience falsifies not an iota; for my knowledge I cannot answer.
    Michel de Montaigne (1533–1592)

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

    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)