mercredi 25 mars 2015

Will this code ever return false?

I'm reviewing some code where the logic looks flawed. I'm not sure if the following code will ever return false because of the if else return flow. My question is, will the following code ever return false, or even throw an error?



function performSearch(e) {
if(e.keyCode === RETURN_KEY_KEYCODE) {
var select = document.getElementById("selectmenusearch");
var selected = select.options[select.selectedIndex].value;
if(selected === 'organisation') {
submitSearchForm('<%= doOrganisationSearchURL %>');
} else {
submitSearchForm('<%= doIndividualSearchURL %>');
}
} else {
return false;
}
return true;
}


So the flow to me looks like



if (this condition is true) {
//execute some code
} else {
return false
}
else return true


NB: I know it would be better to refactor to have only one return statement but it looks to me like there are two else statements.


Aucun commentaire:

Enregistrer un commentaire