News for hackers. Stuff that matters.
Re:We Are Sorry to Inform You (2005) by dale_glass on Saturday June 18, 2022 @06:45AM (#31788723)
The "Goto Statement Considered Harmful" paper kinda overshot its mark, IMO.

It made perfect sense back when people were writing BASIC full of "GOTO 110" and "GOTO 520" that resulted in a tangled mess. Unfortunately it caught on so well that it resulted in people being really obsessive about it, and rejecting every single instance of it in C code, even in places where it makes perfect sense and makes the code cleaner.

Re:We Are Sorry to Inform You (2005) by tialaramex on Saturday June 18, 2022 @07:29AM (#31788911)
Goto in C (and worse, setjmp) is almost always a workaround for a deficiency in C. Examples:

The "goto cleanup" idiom to do deferred work (I don't like defer or finally in languages which have them, but at least these are language constructs which say what we wanted, the goto cleanup idiom only achieves that in some very easy cases, in more complicated cases your actual intent will be unclear)

The "iterator escape" idiom to get out of some larger iterative operation we're doing because we just realised we want to give up / found the overall answer. This is lack of imagination in the language's / library's / eco-system's control flow semantics and is one reason I really like Rust's core::ops::ControlFlow.

Re:We Are Sorry to Inform You (2005) by bruce343434 on Saturday June 18, 2022 @09:19AM (#31789575)
What is your proposed alternative for `defer`? (suppose hidden "RAII" control flow doesn't count)
Re:We Are Sorry to Inform You (2005) by ncmncm on Saturday June 18, 2022 @09:58AM (#31789873)
It doesn't. Or, rather, RAII flow is strictly, explicitly better.
Re:We Are Sorry to Inform You (2005) by munchenphile on Saturday June 18, 2022 @11:16AM (#31790568)
Kotlin‘s return/break@<implicit or explicit loop label> handles the iterator escape idiom quite elegantly.
Re:We Are Sorry to Inform You (2005) by dhzhzjsbevs on Saturday June 18, 2022 @11:36AM (#31790807)
> This is lack of imagination

Code shouldn't require imagination. Sometimes the simple and readable solution is just better.

Re:We Are Sorry to Inform You (2005) by saagarjha on Saturday June 18, 2022 @12:10PM (#31791158)
How about the "threaded interpreter" idiom?
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

Re:We Are Sorry to Inform You (2005) by ncmncm on Saturday June 18, 2022 @09:57AM (#31789868)
It has been, literally, many decades since I last found a place where C++ code would benefit from use of a goto statement.

Those few places where they appear in (what would have been) defensible C code are better served by reliance on destructors. I.e., it is much better to start compiling the C code with a C++ compiler, and then clean it up.

Re:We Are Sorry to Inform You (2005) by dataflow on Saturday June 18, 2022 @02:25PM (#31792424)
I've come across a couple of (admittedly niche) cases where C++ code benefits from a goto:

- In some very performance sensitive cases. It helps you skip a bunch of extra checks or variables.

- When you need to convert recursion to iteration, but want to keep the code structure mostly intact so you don't mess up the source control history.

Re:We Are Sorry to Inform You (2005) by ncmncm on Sunday June 19, 2022 @11:13PM (#31805923)
Agree, minimizing diffs (even indentation!) is a necessary evil.

Nowadays I will use lambdas that return early in preference to goto. Not from fastidiousness so much as to avoid confusing the optimizer.

Poster child for reprehensible use of goto is procmail.

Re:We Are Sorry to Inform You (2005) by andi999 on Tuesday June 21, 2022 @03:43PM (#31828250)
Destructor are not allowed to throw exceptions, so you cam only do stuff that guarantees not to throw.
Re:We Are Sorry to Inform You (2005) by hyperman1 on Saturday June 18, 2022 @07:18AM (#31788837)
These BASIC goto statements are pretty tame. Some basic interpreters did not require the line number in the statement to be constant. You could do things like a switch:

  switch n{case 1: case 2: case 3:} 
would become e.g.

  goto n*100+1000
with lines 1100,1200,1300 handling the cases.

And then there was the hidden advanced troubleshooting console:

  input N: GOTO N
Re:We Are Sorry to Inform You (2005) by dan-robertson on Saturday June 18, 2022 @08:31AM (#31789255)
This is how you do a SWITCH statement in Microsoft’s command language (for .bat files) I think.
Re:We Are Sorry to Inform You (2005) by 4cao on Saturday June 18, 2022 @10:02AM (#31789912)
It does come in handy in some scenarios:

    choice /c abc
    goto choice%errorlevel%

    :choice1
    :choice2
    :choice3
Or, for command-line parameters:

    set ops=a b c
    set op=%1
    for %%p in (%ops%) do if "%op%"=="%%p" goto %op%
    goto usage
Re:We Are Sorry to Inform You (2005) by tinus_hn on Saturday June 18, 2022 @07:02AM (#31788772)
Structured programming makes perfect sense in lieu of the rather silly rejection in this article that holds no more water than ‘Fortran has goto and no structured programming and that is what I’m used to’.
Re:We Are Sorry to Inform You (2005) by munchenphile on Saturday June 18, 2022 @07:23AM (#31788858)
goto makes sense in C for cleanup of local resources. Passing a bunch of pointers to pointers to another function for cleanup makes code far less readable (jumping around in a source file instead of just reading code linearly and freeing local resources in the scope in which they were allocated). Basically, forcing structured programming over using goto is the anti-lambda — requiring a lot of jumping around in the SOURCE file (not necessarily the binary) instead of linearly scanning through the code.

Dijkstra was wrong with his “goto” paper. It’s fine to admit that. He was right about most other things.

Re:We Are Sorry to Inform You (2005) by tinus_hn on Saturday June 18, 2022 @07:38AM (#31788972)
Sure, there are uses. But they are not in this article. The article argues that without goto, an if statement can only conditionally execute one statement.

> The paper doesn't explain to us what would be the use of the "if" statement without a "goto" to redirect the flow of execution: Should all our postconditions consist of a single statement[…]?

Re:We Are Sorry to Inform You (2005) by jstanley on Saturday June 18, 2022 @08:02AM (#31789100)
Yes, the article is a joke.
Re:We Are Sorry to Inform You (2005) by pstuart on Saturday June 18, 2022 @02:34PM (#31792496)
His name is Robert Paulson.