Live data from Hacker News

Declined Proposal: A built-in Go error check function, “try”

github.com

121–130 of 425 posts

Re: Declined Proposal: A built-in Go error check function, “try”

#121
post #61

Earlier quoted context omitted.

> Do I particularly like managing errors that way? No, but I do think that it improves the transparency and quality of a lot of Go projects. So long as we can all agree that it feels super bad, I guess this is fine. But it does sort of mean that Golang approaches the Java world back with checked exceptions where principle trumped ergonomics. That lead to a world where folks felt "forced" to use Java, and that's a sti…

>So long as we can all agree that it feels super bad, I guess this is fine. Actually, I don't think everyone agrees it feels super bad. I personally like having all of my error handling be explicit, painfully explicit even. >approaches the Java world back with checked exceptions where principle trumped ergonomics. I also have to disagree here. To me, checked exceptions are the worst of both worlds. Here you have addi…

> Your 'happy path' must be surrounded by try blocks.

Not if you're doing it right, which means bubbling up (read: adding the exceptions to the "throws" clause) exceptions that you can't handle _then and there_. You leave the "real" exception handling to the code that's closest to the end user and can actually handle the error in a meaningful way.

For example, in this pattern when you implement the business logic in a REST service, generally there's no catching at all, instead the exceptions are declared as rethrown and way up the stack you would have a global catch all that serializes the errors into JSON and sends them to the user. Simple. In Go you're forced to do "if err{}" checks everywhere, particularly in your endpoint's business logic. It's actually _way less_ verbose and burdensome in Java if you do exceptions the right way.

Re: Declined Proposal: A built-in Go error check function, “try”

#122

Earlier quoted context omitted.

> Do I particularly like managing errors that way? No, but I do think that it improves the transparency and quality of a lot of Go projects. So long as we can all agree that it feels super bad, I guess this is fine. But it does sort of mean that Golang approaches the Java world back with checked exceptions where principle trumped ergonomics. That lead to a world where folks felt "forced" to use Java, and that's a sti…

Literally the first non-trivial code I wrote in go (running a bunch of goroutines to download a ton of files from a website in parallel)... I knew exactly where and how things could fail and where things were failing just by looking at the code. Coming from C++ and C# and Python, there was no comparison. I had never been so confident in the code I'd written, even though I was a newbie at Go and a veteran of the other…

>I knew exactly where and how things could fail and where things were failing just by looking at the code.

Same could be said about assembly language.

Re: Declined Proposal: A built-in Go error check function, “try”

#123
post #84

I mean... If you don't like writing `if err!=nil {...}` throughout your code base surely you can just create some middleware function in its own package that has switch statements based on error cases. Checking for errors frequently in the running of the code is more or less a Good Thing.

I don't see how a switch statement helps you avoid checking for errors when methods you use return errors, and a lot of methods return errors.

You can pass functions to functions (https://play.golang.org/p/XNMtrDUDS0).

So you can do (sans syntax):

    ErrorCheckerFunc(fn myParams -> MyFunc(myParams), "message")

    ErrorCheckerFunc(fn myParams2 -> MyFunc2(myParams2), "message2")
etc...

and then the actual function:

    ErrorCheckerFunc(fn myFunc, message){
     returnVal, returnError = myFunc();
     switchError:
      case error is foo:
       print error
      case error is bar: 
       log error print "error logged"
      ...
      case n+1 etc

      // you can handle message in a similar way to get 
      // cases by function names, and if you have elixir
      // style quote unquote metaprogramming in Golang you might just be able to get function names based on passed function - I don't think this is in the language spec though it may be possible to do some other way.

      if error not nil do switchError(error)
       return RuhRoh
      else
       return returnVal
    }
It's not so bad, it's just a middleware.

Re: Declined Proposal: A built-in Go error check function, “try”

#124

I suspected this was coming, and it's unfortunate. If Go had implemented try, then in a year everyone would be happy using it and the controversy would have died down. I've seen this happen before. Unfortunately, the community that has sprung up around Go is more or less opposed to new language features on principle.

The controversy may have died down, but the impact on code written would have been permanent.

There's a lot of value to there being "one way to do things", even when it's not the best way from any particular point of view.

Go holds this principle higher than most other languages and I think that should be either embraced, or one should look elsewhere - and I'm saying that as someone who "looked elsewhere".

Re: Declined Proposal: A built-in Go error check function, “try”

#125

This is great news. I really like how errors are handled in Go and I hated this proposal. The current implementation forces you to constantly think about errors at each single point and it is extremely good at standing out. This is something very valuable, not something that requires or needs to be hidden behind syntactic sugar. It improves the readability of the code and the quality of the software. If it ain't brok…

Go does not force you to think about errors at every single point. If a function returns only an error (such as, for example, os.Mkdir), the language will happily let you drop the error on the floor.

And that's totally fine. I don't always want to be fighting with monads. If I did I'd write Haskell code. Go is the quick and dirty git-er-done tool that provides quite a bit more performance and type safety than Python, but maintains some of the development speed/ergonomics.

Here's the thing, some of us don't want Rust. It looks great! It's perfectly awesome for it's primary domain (i.e. re-implementing critical portions of a modern browser). But it's not what I want to use as my daily driver.

I'm writing stupid shit that's munging CSV files and transforming json documents. I want a language that mostly gets out of my way, but performs well and can scale beyond a couple thousand lines of code. Go fits that space nicely, even with its (or maybe because of) conceptually imperfect error handling.

Re: Declined Proposal: A built-in Go error check function, “try”

#126
post #81

Earlier quoted context omitted.

> My guess is that gofmt is a huge factor in this It really is. My code looks like your code and the next person's code. Go is opinionated and strict and that makes reading other people's code so much easier

I wish gofmt would let you set a desired line length and break it for you.. prettier has me spoiled in that regard

If a tool is going to enforce line length limits, I'd rather have it spit a warning/error than silently try to guess a good place to break the line automatically. Otherwise, an editor tool that soft-wraps long lines at the (often poorly) guessed location without touching the code would be better.

Re: Declined Proposal: A built-in Go error check function, “try”

#127
Thank you Go developers! Please keep the language minimal, opinionated, and never subject to the bikeshedding mobs!

It wouldn't have been the end of the world but I'm glad to see Go still has alive the spirit that made it so popular in the first place.

Re: Declined Proposal: A built-in Go error check function, “try”

#128
post #68

Earlier quoted context omitted.

I don't understand why anyone would be (strongly) against this proposal. If I understand it correctly, it's just a shortcut. That is, the old (existing) way would still work. If anyone didn't like "try", well, don't write your code that way. It would also appear to be amenable to an automated tool that would convert to or from try-style. Given that, why would people have strong feelings against? Because they think go…

This is the prime example of what makes Go different than most other languages. They don't put things in attached with the argument "if you don't like it, you don't have to use it." Go is a language designed to be written and read by teams. You don't like try. Bob does. Bob writes code that uses it. You still have to read it.

I kind feel that declining try/catch is aligned with the baleful eye cast on Bob. The language designers think Bob is going to use try/catch to foist error handling on someone else or for later which never comes. And so error handling will then end up slightly broken to totally broken. So they'd rather when Bob's writes code with error paths that he to deal with that then and there. Not 'elsewhere' 'later' or make it 'someone else's problem'

Re: Declined Proposal: A built-in Go error check function, “try”

#129
post #94

Earlier quoted context omitted.

>Go has exactly the same issue. But with exceptions at least you can group multiple statements together and handle them with one catch block. With Go you have to use multiple if blocks to get the same semantics. Go does not suffer from this issue at all. I am not talking about flattening from the call hierarchy, I am talking about flattening the try scope itself. (In case it isn’t obvious: in Go you’d be forced to se…

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

>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”

#130
post #73
post #33

This hits at something fundamental about Go, which is what I like the most about it... It's a language intended to have few primitives with an emphasis on code being transparent and errors being values, requiring you to think about what they might be at each point as you're forced to carry them up the chain. Do I particularly like managing errors that way? No, but I do think that it improves the transparency and qual…

> and you can see it in stark contrast to something like the JS community with its require('left-pad') NPM ecosystem. Is it fair to compare external packages to core language features? There is nothing preventing a go mod/dep left-pad

Language choices like lacking generics make the over-reliance on dependencies less.
Post reply on HN