News for hackers. Stuff that matters.
Re:Avoid Else, Return Early (2013) by psyc on Monday March 26, 2018 @10:09AM (#16678989)
For whatever it's worth, I insisted on single return for a long time when I was an intermediate programmer. 30 years in, I strongly prefer return early (as well as continue early in loops) and I cannot go back. The immensely reduced indentation is marvelous. Having that 'happy path' consolidated is more readable.
Re:Avoid Else, Return Early (2013) by RobertRoberts on Monday March 26, 2018 @11:09AM (#16679465)
I find it interesting how most of the comments about return early are based on experience.

I had the same thing happen about 10 years in. I just got sick of the extra coding and long indented blocks. And just switched over one day.

Why is this not taught from day one?

Re:Avoid Else, Return Early (2013) by macspoofing on Monday March 26, 2018 @12:33PM (#16680121)
The problem is that for any rule you come up with there are cases where that rule is just too rigid and too inflexible.

There are times where early return makes a ton of sense. Usually that's the case when you can derive the return value from a shallow interpretation of some input values (i.e. check preconditions) but still require more complex processing for other input values. In that case, return early, but constrain the rest of the method to one return point.

On the flip-side, there are cases where an early return makes it incredibly hard to reason about the function. This usually occurs in dense, nested conditional logic. In those cases, I found sticking to rule that a return value should be initialized once and only once, and return from a single point, beneficial in even reasoning about the problem.

Re:Avoid Else, Return Early (2013) by RobertRoberts on Monday March 26, 2018 @01:12PM (#16680531)
Can't that be taught as well? (both rules, and exceptions to rules?)
Re:Avoid Else, Return Early (2013) by macspoofing on Monday March 26, 2018 @01:24PM (#16680667)
Sure.

But as a novice youd should adhere to rules. As you gain experience you learn when to break them.

Re:Avoid Else, Return Early (2013) by RobertRoberts on Monday March 26, 2018 @03:30PM (#16681706)
I think you read my comment backwards.

I think the "rule" should be "exit early", and the exception "multi-indent exit later".

Re:Avoid Else, Return Early (2013) by Wildgoose on Monday March 26, 2018 @05:00PM (#16682547)
I sold my first program aged 15 and I'm 53 now. My experience is that the benefit of early return is to keep indentation to a minimum. To me, every time code indents it is a "context switch".

Having said that, early return should be for error/ignore situations - there should only be one return for a result and that should be at the very end of the function.

Re:Avoid Else, Return Early (2013) by rukuu001 on Tuesday March 27, 2018 @02:06AM (#16685714)
We were taught "one entry, one exit".

I now think of the early part of a function as a filter for bad params, invalid state & whatnot, and return as early as possible.

Code just feels less complex that way.

Re:Avoid Else, Return Early (2013) by Apatheticdino on Monday March 26, 2018 @11:30PM (#16684969)
I'd like to believe that this is a result of the concept of functions being first taught in the context of mathematics. Since there's no concept of flow control in mathematical functions, their entire purpose boils down to "Given parameters, do processing, return value".
Re:Avoid Else, Return Early (2013) by egypturnash on Monday March 26, 2018 @05:59PM (#16683026)
Habits based on what was fastest on slow machines where every cycle mattered and compilers weren’t as good at optimizing, I’d bet.
Re:Avoid Else, Return Early (2013) by maxxxxx on Monday March 26, 2018 @10:19AM (#16679063)
I am on board with returning at the beginning while checking arguments. But I hate it when code returns somewhere in the middle for some reason. I have spent countless hours debugging a problem with unexpected behavior only to find a return statement in the middle of a huge block to code.
Re:Avoid Else, Return Early (2013) by augustk on Monday March 26, 2018 @12:19PM (#16679999)
That's because the return statement is a kind of goto statement. Goto-like statements reduce the value of structured programming. In a pure structured program, each statement sequence is either completely executed or not executed. There are also pure structured languages, like Oberon, which doesn't allow impurely structured programs.
Re:Avoid Else, Return Early (2013) by maxxxxx on Monday March 26, 2018 @01:20PM (#16680613)
When return is used in the middle of a complex code sequence I much prefer a goto so at least I know where all paths exit.