News for hackers. Stuff that matters.
Re:Declined Proposal: A built-in Go error check function, “try” by pcwalton on Tuesday July 16, 2019 @09:21PM (#20455564)
> But it results in still less cumbersome code, since you only need scoping for the error handling portions.)

There isn't a meaningful difference in cumbersomeness between having two try-catch blocks and two if-err blocks. There is a meaningful difference in cumbersomeness between what Go has today and "try foo(try bar())". Which is why it's so unfortunate that the community killed the try proposal.

> But forgetting that, because passing errors down in Go is explicit, it is actually customary to use error wrapping to add context as an error is propagated,

People say this, but in practice any code search reveals that "if err != nil { return err }" is everywhere. I believe that many Go projects aspire to annotate all errors, but much fewer actually do.

Ironically, the nice thing about exceptions, as well as Rust error chaining crates, is that they do this automatically, so in practice programs written in languages with those features tend to have better error diagnostics than Go programs do. Computers are better at doing things consistently than humans are.

> It is a simpler language than Java. You get most of the benefits of checked exceptions without all of the calories from exceptions.

Go is a more complex language than Java is overall, because of all the special cases the language adds to magic types like maps and errors that are just part of the library in Java.

Re:Declined Proposal: A built-in Go error check function, “try” by wybiral on Tuesday July 16, 2019 @09:29PM (#20455609)
> any code search reveals that "if err != nil { return err }" is everywhere

Code searches in languages with exceptions tend to wrap the tryblocks around massive portions of code instead of the individual function calls to the point that you have a top level doing:

  try:
      ...program here...
  except:
      print('¯\_(ツ)_/¯')
Re:Declined Proposal: A built-in Go error check function, “try” by tsimionescu on Wednesday July 17, 2019 @02:19AM (#20456978)
But that is exactly how error handling usually works, especially if cleanup is handled separately - you just need to propagate the errors, usually all the way up to the user, who is the only one who can take a meaningful decision.

Almost all actual error handling in code is either error translation and re-throw, resource cleanup, or automatic retries (sometimes you retry the same request, sometimes you try a fallback option, but it's still the same idea).

The user however may be able to check and actually fix their internet connection, they may fix the typo they did in the config file, they may call support to see what's happening to the database etc. - your program can't do any of these things.

That's why exceptions work so well in most languages, especially GC languages where you have dramatically fewer resources to cleanup: they bubble up automatically towards the initial caller, which is often the user. Threading messes with this, but if you use the more modern async style (async/await in most languages) you get proper exception propagation even then.

Re:Declined Proposal: A built-in Go error check function, “try” by mikeschinkel on Friday July 19, 2019 @02:46PM (#20481394)
> That's why exceptions work so well in most languages

But there are many people including some well-known and highly-respect people who believe that exception handling in fact does not actually work well:

- https://www.joelonsoftware.com/2003/10/13/13/ - https://blogs.msdn.microsoft.com/larryosterman/2004/09/10/st... - https://www.atlassian.com/blog/archives/exceptions_are_bad - https://stackoverflow.com/a/1736320/102699 - http://xahlee.info/comp/why_i_hate_exceptions.html - http://www.lighterra.com/papers/exceptionsharmful/

#justsaying

Re:Declined Proposal: A built-in Go error check function, “try” by KirinDave on Tuesday July 16, 2019 @10:12PM (#20455814)
... I could do this with Golang too, couldn't I?
Re:Declined Proposal: A built-in Go error check function, “try” by wybiral on Tuesday July 16, 2019 @10:21PM (#20455852)
On the opposite end of the spectrum, yes, as they pointed out you can just return all of the err's up the stack.

I wasn't saying that Go's approach solves this, just that it's not a problem unique to Go.

And in the case of Go it's painfully obvious that you're ignoring all of those errors whereas in other languages you can't always tell, visually, that they're being ignored because of the magic of exceptions.

Re:Declined Proposal: A built-in Go error check function, “try” by jchw on Tuesday July 16, 2019 @09:41PM (#20455667)
>There isn't a meaningful difference in cumbersomeness between having two try-catch blocks and two if-err blocks. There is a meaningful difference in cumbersomeness between what Go has today and "try foo(try bar())". Which is why it's so unfortunate that the community killed the try proposal.

    Object thing1;
    try {
        thing1 = doStuff();
    } catch(SameException e) {
        // handle error 1
    }

    try {
        return doOtherStuff(thing1);
    } catch(SameException e) {
        // handle error 2
    }
vs

    thing1, err := doStuff()
    if err != nil {
        // Handle error
    }

    thing2, err := doOtherStuff(thing1)
    if err != nil {
        // Handle error
    }

    return thing2
Happy path is flat. Control flow is obvious and simple. I have not much more to add.

>People say this, but in practice any code search reveals that "if err != nil { return err }" is everywhere. I believe that many Go projects aspire to annotate all errors, but much fewer actually do.

https://github.com/search?l=Go&q=%22errors.Wrap%22&type=Code

>Ironically, the nice thing about exceptions, as well as Rust error chaining crates, is that they do this automatically, so in practice programs written in languages with those features tend to have better error diagnostics than Go programs do.

Rust is a different ball game. Rust does not try to be simple. It comes at its own costs. (I like Rust too.)

>Computers are better at doing things consistently than humans are.

These empty platitudes come up frequently when debating language decisions online. But, it's so meaningless in so many dimensions. I mean, we could also 'use the computer' by adding C macros on top of Go and use them to reduce repetitiveness, but I don't think many people will applaud you for it. Simply applying computer code to solve a problem does not constitute good design.

Go's proof is in the pudding. It's been extremely reliable for me in real world applications.

Re:Declined Proposal: A built-in Go error check function, “try” by erik_seaberg on Wednesday July 17, 2019 @12:34AM (#20456481)

  doOtherStuff(doStuff())
is the happy path, but it's been hidden among lines of noise that do nothing more than return to the callers who know what to do. Generating this using cpp or m4 would suck, but it's still better than not generating it due to wasted effort (especially re-reading) and mistakes.
Re:Declined Proposal: A built-in Go error check function, “try” by tsimionescu on Wednesday July 17, 2019 @02:25AM (#20457016)
How often do you actually need to handle those errors differently? In my experience, it is vastly more likely that a function which can throw errors in Java looks like this:

  Stuff foo() throws SameException {
    return doOtherStuff(doStuff())
  }
Whereas in Go the exact same function must be written like this:

  func foo() (Stuff, SameException) {
    thing1, err := doStuff()
    if err != nil {
        return err
    }

    thing2, err := doOtherStuff(thing1)
    if err != nil {
        return err
    }
  }
Propagating is by far the most common "error handling" and Go constantly makes you break the flow of the code to do it.
Re:Declined Proposal: A built-in Go error check function, “try” by Crinus on Wednesday July 17, 2019 @06:48AM (#20458105)
FWIW when debugging i prefer the second style if for no other reason than that i can place a breakpoint in doOtherStuff while skipping doStuff. Also reading it, it is more obvious that the code calls both doStuff and doOtherStuff (though with just two calls it isn't a big different, imagine having a 2-3 more calls in there).

(also why debuggers still insist on line-based breakpoints is beyond me, why can't i right click at a call and put breakpoint at the call itself instead of the line where the call lies on?)

Re:Declined Proposal: A built-in Go error check function, “try” by tsimionescu on Wednesday July 17, 2019 @07:14AM (#20458217)
You could also write the first function body as

  Stuff thing1 = doStuff()
  return doOtherStuff(thung1)
It's still easier to read without the explicit error handling.
Re:Declined Proposal: A built-in Go error check function, “try” by mikeschinkel on Friday July 19, 2019 @02:54PM (#20481464)
After working with Go for a while I actually find code with error handling easier to read because it is more clear to me what it happening. I look for the error handling as an indicator that the code can actually fail vs. code that cannot fail.

And maybe it's because I've developed a very consistent pattern, and when I find that there are too many errors to handle in a function it helps me realize the function is probably doing too much and needs to be split into multiple functions.

IMO it is all about training your brain and being accepting enough to work in the dialect of the land, as opposed to demanding to speak English when living in France. :-)

Re:Declined Proposal: A built-in Go error check function, “try” by wybiral on Tuesday July 16, 2019 @09:56PM (#20455745)
The fact that it allows for more nesting on a single line is a bug, not a feature, IMO.
Re:Declined Proposal: A built-in Go error check function, “try” by weberc2 on Tuesday July 16, 2019 @10:32PM (#20455903)
I like your points about diagnostics. I definitely feel this—it seems like one of Go’s weakest points. That said, Go is still one of the best tools available for building software today.
[dead]
Re:Declined Proposal: A built-in Go error check function, “try” by pcwalton on Tuesday July 16, 2019 @10:22PM (#20455863)
I'm not saying you should use Rust! I'm saying that Go should add try.
Re:Declined Proposal: A built-in Go error check function, “try” by mikeschinkel on Friday July 19, 2019 @03:04PM (#20481548)
I was one of the many who argued strenuously that they not add try(). I spent an entire day writing up my reasons who I posted to the ticket on GitHub.

Bottom line, try() had too many flaws — especially related to consistency with the rest of Go — to be an appropriate addition to the Go language.

Adding try() to Go would be like adding a disco ball to the Sistine Chapel. Sure, disco balls have their place, but some places are just not appropriate for disco balls.

Or as Yoda says:

"Do or do not. There is no try." (Fortunately!)