Live data from Hacker News

Avoid Else, Return Early (2013)

blog.timoxley.com

51–60 of 601 posts

Re: Avoid Else, Return Early (2013)

#51
post #6

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/

JavaScript has the guard operator. It can really clean up your control flow when you know it's a guard operator and not just logical AND.

Re: Avoid Else, Return Early (2013)

#52
In 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 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)

#53
1. This is common in Go; 2. It is not about saving lines or curly braces. It is about the flow of code, and branching away when expectations are violated. It is about helping readers of code establish a mental model of the intentions of code.

Re: Avoid Else, Return Early (2013)

#54

I 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…

> I was made to have only one exit point for some projects

Did they give any rationale for this?

Re: Avoid Else, Return Early (2013)

#55
post #26
post #7

Avoid 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…

"Unless he is certain of doing as well, he will probably do best to follow the rules." - Guy who wrote the rule book

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)

#56

Sort 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.

I had to double check the function call occurred. It's not intuitive and removes braces in favor of possible side effects.

Re: Avoid Else, Return Early (2013)

#57
I have read somewhere else (https://softwareengineering.stackexchange.com/questions/1187...) that the "single exit" rule was not actually about having only one place where the function returns, but actually about having a single place where the function returns to.

That 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)

#58
post #41

It 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.

I am arguing this is worse, since it expresses the logic in a more convoluted way.

Re: Avoid Else, Return Early (2013)

#59

In 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…

Love this in Ruby. Rubocop's Guard Clause cop[1] always flag this for me.

  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)

#60

This 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.

Right, I should've mentioned except arcane things (for us pleb web devs) happening in low-level languages.
Post reply on HN