News for hackers. Stuff that matters.
Re:We Are Sorry to Inform You (2005) by andi999 on Saturday June 18, 2022 @07:40AM (#31788985)
I agree. Also if people really believe goto is bad, lets not forget the 'hidden gotos' like early return oder state machines.
Re:We Are Sorry to Inform You (2005) by dls2016 on Saturday June 18, 2022 @08:33AM (#31789264)
I’ve worked with people who stick to “only one return statement per function”. Super annoying.
Re:We Are Sorry to Inform You (2005) by dhzhzjsbevs on Saturday June 18, 2022 @11:39AM (#31790835)
The no else statement shit drives me up the wall. Sometimes code is more readable when it says what you mean. Sometimes I want to branch, other times I want to return early. There is a difference in what I mean and code should reflect that even if the opcode is the same.
Re:We Are Sorry to Inform You (2005) by zasdffaa on Saturday June 18, 2022 @01:00PM (#31791660)
I'm one of those and rather unsure of its value. The original reason is the modelling of the program as a reducible graph (if I'm not getting confused with other stuff).

I occasionally have a couple of returns in one func but only for very, very simple and short funcs where it's clearer.

I dunno. Perhaps the problem is we're missing some higher level procedural construct - any ideas?

Re:We Are Sorry to Inform You (2005) by cebert on Saturday June 18, 2022 @08:59AM (#31789439)
I think it depends on the situation, but a single return statement per method does make it a bit easier to read a method and understand flow control.
Re:We Are Sorry to Inform You (2005) by HWR_14 on Saturday June 18, 2022 @10:57AM (#31790359)
At the top of the function, "if [special input] return [special output]" is a great way of handling edge cases
Re:We Are Sorry to Inform You (2005) by Tyr42 on Saturday June 18, 2022 @10:21AM (#31790034)
Really exit can be pretty useful though.

Doing if(condition) return at the start, before it gets going often reduces indentation and how many branches you need to see at once.

But I can agree sneaky returns can bite you sometimes

Re:We Are Sorry to Inform You (2005) by StellarScience on Saturday June 18, 2022 @12:33PM (#31791390)
We've adopted single entry / single exit except for short functions (< 10-15 lines), where all the returns are clearly visible at once. It seemed to placate both sides, and it allows you to write that simple 'findFoo' function that returns 'foo' as soon as it finds it.
Re:We Are Sorry to Inform You (2005) by ketzu on Saturday June 18, 2022 @09:48AM (#31789787)
The 'hidden gotos', especially like an early return, for me have stricter guarantees and limitations than a goto used in the same way. Goto and labels can do too much for me to like them, i prefer more specific things with clearer boundaries and intent.
Re:We Are Sorry to Inform You (2005) by andi999 on Saturday June 18, 2022 @10:58AM (#31790372)
What are these guarantees? One is that they are easier to miss when reading the code.
Re:We Are Sorry to Inform You (2005) by ketzu on Saturday June 18, 2022 @07:55PM (#31794873)
Sorry, I don't fully understand what you mean by that. Are "early returns" easier to miss when reading code (easier to miss than goto statements achieving the same effect)? If that was your intended meaning (sorry I am missing some sleep today) I don't share that view, I think both statements are equally obvious in that regard.

Examples I was thinking about:

* an early return can only go back to the call-site

* all returns in a function or method return to the same place

* an early return terminates the context it is in

* an early return does not create an entry point for nearly arbitrary other code points as labels for gotos do