Created
October 21, 2010 18:04
-
-
Save bentruyman/638983 to your computer and use it in GitHub Desktop.
Don't abuse `return`
This file contains 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
// BAD: `return` shouldn't be used to control flow/execution | |
function loadProductData(query) { | |
if ($('body').hasClass('busy')) | |
return; | |
$.ajax( /*...*/ ); | |
} | |
// GREAT SUCCESS: Just do this instead (OMG duh!) | |
function loadProductData(query) { | |
if (!$('body').hasClass('busy')) { | |
$.ajax( /*...*/ ); | |
} | |
} | |
// BAD: Always return if you're going to return | |
function performScientificMaths(x, y) { | |
if (x + y > 0) { | |
return x + y; | |
} | |
} | |
// GREATE SUCCESS: Never a null (I mean, undefined) moment | |
function performScientificMaths(x, y) { | |
if (x + y > 0) { | |
return x + y; | |
} else { | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
And I guess in the, albeit rare, case a function is refactored to start actually returning useful values, you're not unexpectedly returning
undefined
.Am I grasping at straws yet?