A more formal name for this approach is / could be Guard Clauses: https://refactoring.com/catalog/replaceNestedConditionalWith... . This pattern has been elevated to a language construct in Swift: https://thatthinginswift.com/guard-statement-swift/
Avoid Else, Return Early (2013)
51–60 of 601 posts
Re: Avoid Else, Return Early (2013)
#52As a further refinement, I will often take tricky conditional logic and put them into a method that consists of nothing but guard clauses and a return true or false at the bottom of the method. In Ruby you can use ? and ! at the end of method names, so I'll give the method an intention-revealing question-mark name. "Order.used_credit_card?"
I use this in Javascript whenever I can too, though of course it's not as semantically nice as Ruby.
Re: Avoid Else, Return Early (2013)
#53Re: Avoid Else, Return Early (2013)
#54I was made to have only one exit point for some projects and it drove me up the wall. It only really makes sense if you're coding in languages like C. I don't like functions written this way because they're longer, are harder to read, have complex nesting and most of all they introduce state (the variable that stores the result). In dynamic languages, you're not going to get warned if you forget to set the result var…
Did they give any rationale for this?
Re: Avoid Else, Return Early (2013)
#55Avoid rules, use your judgment.
"Unless he is certain of doing as well, he will probably do best to follow the rules." - Strunk, The Elements of Style Your advice is unfortunately common but misguided. Without a firm and intuitive grasp of "rules" and why they exist, one cannot build a framework for making decisions with which one can use their judgment. On top of that, most programmers aren't advanced enough to have more reliable judgment than "th…
A better way of saying it might be: If you don't understand why the rule exists and don't have time to learn, follow the rule. When you follow the rule you may discover why it exists, when you don't follow the rule you will almost certainly discover why it exists.
Re: Avoid Else, Return Early (2013)
#56Sort of agree, but I don't think if (err) { handleError(err) return } and if (err) return handleError(err) are equally good. The second one doesn't really make it clear wether handleError returns a value and that value is intended to be returned.
Re: Avoid Else, Return Early (2013)
#57That rule seems to have been so successful, that no modern language I know of allows (the original definition of) multiple entry or multiple return (except for exceptions).
Re: Avoid Else, Return Early (2013)
#58It depends! But it depends on the actual meaning of the code, so examples which use meaningless identifiers like `doStuff()` miss the distinction. If the conditions are semantically symmetrical, if/else is the right approach: fun max(a, b) { if a > b { a } else { b } } But is it a precondition which causes you to skip the primary logic of the function, then get it out of the way early: fun max(a, b) { // special case…
You could still write it as fun max(a, b) { if a > b { return a } return b } Not saying this is necessarily better or worse. The point I want to make is that your case isn’t special.
Re: Avoid Else, Return Early (2013)
#59In Ruby these are idiomatic enough to have a name and are built into the linter, so it'll yell at you if it sees that you're not using guard clauses. As a further refinement, I will often take tricky conditional logic and put them into a method that consists of nothing but guard clauses and a return true or false at the bottom of the method. In Ruby you can use ? and ! at the end of method names, so I'll give the met…
def foo
fail unless bar
return baz if foobar
foobaz
end
[1]: http://www.rubydoc.info/gems/rubocop/RuboCop/Cop/Style/Guard...Re: Avoid Else, Return Early (2013)
#60This refactoring is so simple and obvious I don't even understand why some people don't write like this by default. I've had colleagues comment my PR on my "failed validation, return early" code saying "we generally strive to maintain one single point of return". I mean, why would you want only one return even?
> I mean, why would you want only one return even? Some other comments here discuss resource cleanup issues that can creep into C code with early return. If you're not coding in C, then more likely it's pure cargo-cult.