Skip to content

Instantly share code, notes, and snippets.

@SeanMcMillan
Created October 29, 2010 18:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SeanMcMillan/654147 to your computer and use it in GitHub Desktop.
Save SeanMcMillan/654147 to your computer and use it in GitHub Desktop.
Examples of one return versus multiple returns
// 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;
}
}
}
// 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";
}
// 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