Hacker News new | ask | show | jobs
by divan 641 days ago
"Go’s Error Handling Is Perfect, Actually" [1]

https://blog.verygoodsoftwarenotvirus.ru/posts/errors-in-go/

3 comments

> Spend any amount of time in programming circles, and just as the sun rises and falls, you are certain to hear someone complain about error handling in Go. These complaints are, anecdotally, rarely well thought out suggestions on what error handling could or should be like in a language like Go, but often merely boil down to “I don’t like having to look at it”.

I read it. The first paragraph dismisses preferences in a matter that boils down to preference.

> Note that for any sufficiently complex program that invokes many dependencies, this stack trace will be so far down the chain that you may not even see where you’re making the call that causes it.

I don't understand this part. Why would my code not appear in the stack trace? Did this author know how to read stack traces?

Personally I find it much faster to pinpoint errors in the Go style.

With a stack trace, I have to cross-reference with code (ensuring versions match) and filter out a bunch of irrelevant calls in the stack. It’s not uncommon for the stack trace to end deep in library code with the root cause being many calls removed, making me check through a bunch of call sites to figure out what happened.

In Go if good context is added to errors, an error log is generally enough on its own to make it obvious exactly what went wrong.

Adding good context is also possible with exceptions.

I think people often compare good Go code with, say, bad Python code where every good practice has been ignored. Go is new and people who write Go are more likely to be skilled and enthusiastic about its design philosophy, so it is somewhat true that Go code is more likely to be high quality.

There's nothing Go error handling does that cannot be done with exceptions. When it comes to bad code, bad Go code is likely to ignore errors or have no context, whereas bad Python code is less likely to ignore errors and at least a raw stacktrace has some context.

This is an argument as old as time though.

This is a very long post to say "Go's Error Handling would be better if it was like Rust's, but the language designers made a mistake early-on and now we don't want to fix it"
I understand that writer's position that they can't go back and fix it now, but in my mind "We can't very well fix it now" is quite different from perfect. I think such a rationale justifies every choice as equally "perfect" and is thus useless.