Skip to content

Instantly share code, notes, and snippets.

@phillipj
Created August 29, 2012 11:34
Show Gist options
  • Save phillipj/3511219 to your computer and use it in GitHub Desktop.
Save phillipj/3511219 to your computer and use it in GitHub Desktop.
Whether or not to use multiple return statements
private boolean isReadable(String myString) {
boolean isValid = true;
// as I expect my string to be valid by default, I only need to check when the string is NOT valid
// which also means I can get rid of the second if-statement in notReadable.java
if (myString.length() == 0) {
isValid = false;
}
// ..
// this bit of code should only be invoked if my string still is valid
if (isValid) {
myString = myString.concat("useless");
isValid = myString.length() < 10;
}
return isValid;
}
private boolean isNotReadable(String myString) {
if (myString.length() == 0) {
return false;
}
if (myString.equals("something")) {
return true;
}
// ..
// this bit of code should NEVER be reached when previous if-statements evaluated to true
// ..that assumtion might be clear at that given moment, what in 1 year when your colleague needs to fix a bug?
// I could easily read this as "always concat useless to my string", but be suprised when looking further up the method
myString = myString.concat("useless");
return myString.length() < 10;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment