Created
October 29, 2010 18:55
-
-
Save SeanMcMillan/654147 to your computer and use it in GitHub Desktop.
Examples of one return versus multiple returns
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Lookup Table | |
var waldoChecks = { | |
kitchen: isInKitchen, | |
bedroom: isInBedroom, | |
garage: isInGarage | |
}; | |
//add the dom elements he could be hiding in | |
for(var i = 0; i < 100; i++) { | |
(function(i) { | |
var elem = "#hiding" + i.toString(); | |
waldoChecks[elem] = function() { | |
return $(elem).hasWaldo(); | |
}; | |
})(i) | |
} | |
function findWaldo() { | |
for (var loc in waldoChecks) { | |
if (waldoChecks[loc]()) { | |
return loc; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Multiple returns -- totally unreadable. | |
function findWaldo() { | |
var ret; | |
if (isInKitchen()) return "kitchen"; | |
if (isInBedroom()) return "bedroom"; | |
if (isInGarage()) return "garage"; | |
//check the dom elements he could be hiding in | |
for(var i = 0; i < 100; i++) { | |
var elem = "#hiding" + i.toString(); | |
if($(elem).hasWaldo()) { | |
return elem; | |
} | |
} | |
return "couldn't find him"; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Only one return, so It's clear. | |
function findWaldo() { | |
var ret; | |
ret = isInKitchen() ? "kitchen" : isInBedroom() ? "bedroom" : isInGarage() ? "garage" : undefined; | |
if(!ret) { | |
//check the dom elements he could be hiding in | |
for(var i = 0; i < 100; i++) { | |
if (!ret) { | |
var elem = "#hiding" + i.toString(); | |
if($(elem).hasWaldo()) { | |
ret = elem; | |
} | |
} | |
} | |
} | |
return ret ? ret : "couldn't find him"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment