37 points · by bheni · 6y ago · 73 comments
Super sorry about the shitty code in the free project you built your business on ;-).
Anyway, this was a point of huge debate early on in the development of Noms. Some team members just could not accept the constant litter of `if err != nil` no matter how many times I repeated that Rob Pike says it's OK. It goes against deeply held beliefs that all programmers internalize early on, for good reason.
It’s probably why some parts of Go internally have also used panics for flow control (the JSON decoder, famously), and just convert them to errors at the boundaries. This was our plan, but we were not careful about protecting all the goroutine launches. As I said elsewhere in the thread I think we would have welcomed working together on this if the Dolt folks had wanted to.
I should say, Rob Pike didn't put in the language the features that would have helped alleviate the pain of writing 50,000 times the exact same statement littering Go code bases like a plague. Go errors are purely a convention that is no better than C style error code returns. Not good enough in whatever year Go was designed. There are solutions to that issue that are much more composable than that (options, sum types...).
People come to Go for performance and concurrency (although context is another one of these tacked on horrors) and they leave the language because of the crazy ceremonial boilerplate it incurs.
This is like an option type but manually built.
Go is painful if you don't want to write high-quality heavily exercised production code. If you do, then you should be considering each error condition. Most code doesn't need to worry about error handling as it will run under close supervision on at most one machine over its lifetime. Go is a nuisance for that.
If you want to know that you have handled every error path, Go is awful because there is no language or compiler support for it at all. Completeness has to be ensured manually, by inspection.
Check out Rust's error handling approach sometime for an example of a language that truly values complete and correct error handling. It is a joy.
===
The reality is that Go doesn't have better error handling because the language designers value the simplicity of Go's implementation more than the simplicity of its interface - a trait that directly flows from its unix roots [1].
This is a fine tradeoff, and the right one for many problems. Go has been extremely successful in large part because of this philosophy. But you should not kid yourself that this is somehow the Right Way to handle errors, just because it's what Go does. Because even Go's designers are actively trying to fix it [2].
That's the excuse that the golang authors give. The fact of the matter, golang is also extremely annoying when writing "high quality production code". golang errors are error-prone, and on more than one occasion we've had instances where errors are mistakingly ignored. This is much worse than exceptions where they have to be explicitly swallowed, otherwise they'd automatically bubble up.
I've been writing Go almost daily for 5 years and have never thought writing if err != nil is remotely a plague. It's an opportunity to handle the situation gracefully, annotate the error or log with details and propagate or swallow. Each invocation becomes deliberate, routine, and meaningful.
If you're just trying to hack together a tire fire, then sure, maybe this is painful, but also maybe consider why you find this to be painful? What do you value, and if it's not what Go provides, why on earth are you using it?
The mess of thousands of lines of `if err` and the lack of any language feature to actually deal with tuple returns as first class citizens was a mistake, full stop. It makes it significantly more difficult to actually read go code, since the actual logic is hidden amongst dozens of lines of visually-indistinguishable error handling logic.
Sure, the panics sucked but it's over. Happy to figure out a way to get those changes back into Noms eventually. When we changed it, it was unclear anyone would ever work on Noms formally again.
explicit error returns is a little clumsy, but still nicer than exceptions.
(zig error handling seems basically perfect to me.)
e.g. what does binary search look like in elm?
in general i always thought out-of-bounds controlled-crashing seemed the program was a reasonable thing to do, since it's arguably a bug in the program, not an error in input.
or what about integer oveflow? or divide by zero, it would seem very impracticle indeed to wrap all those operations in maybe...
The main difference is cultural: you're encouraged to use return codes for expected error conditions and only panic for program bugs. But there's the temptation to not do that because panic(/exceptions) are more ergonomic.
I think exceptions tend to be OK in areas where you don't really care about handling errors: eg command-line tools, GUIs. But for long-lived proccesses, like servers, where it's important to handle errors in a precise way, they are painful, in my experience.
In almost all business processing, thanks to the magic of transactions, you never end up in a bad state.
I remember the first time I wrote some error-handling code in Elixir and nearly cried
For one, you can make few initial changes and then zone out fixing type errors, whereas this is entirely manual.
Except for when it’s harder, as the blog post describes. I would guess it’s not all zoning out in a functional language either?
Aside from that, I think that's just your opinion. Many people and large-scale projects have been happy with Go error handling. I personally like errgroup package and it solves a complicated problem cleanly. Be careful for what you wish for your enemy. ;)
As soon as I realized that Noms could panic when calculating the hash of its types I knew I was way off. In the end I think I spent closer to 6 weeks on this. It was time-consuming, and boring, but it couldn't be automated and every one of the changes was an opportunity to introduce bugs.
You are right though, I didn't spend too much of the post talking about how much I hated that 6 weeks and how I regretted my life choices.
Sensationalization has started slowly creeping into HN unfortunately.
https://github.com/dre1080/recovr
That's what we do. Well we always use errors, but we can't be sure the packages we use won't panic.
Granted its sometimes unavoidable and some package maintainers just didn't get the memo, nevertheless when deciding on a package to use it's much easer to find dubious goroutines, than assessing if every potential panic has been handled correctly. Usually easer to fix too if warranted.
So I spent a couple hours getting rid of panics, and afterwards I was able to use the tools.
This had been solved, fifteen years before go. I understand why java doesn't have this (it's legacy, from the era of erlang). I can't understand why Rob pike didn't steal that idea from erlang.
It's getting it in Project Loom as far as I understand (hierarchical/supervisor processes).
It goes to show how badly thought out golang is when it comes to language design.
I was about to say UTF-8 is an exception but they themselves brought us that in 1992.
handling all the cases sucks but it does make it really clear what choices your code is making
That's allowed in open source and part of the beauty. It does kind of make me sad though.
In the case you describe it sounds like it was particularly painful due to the multi-threaded nature of the software.
You didn't call it out directly but I feel like the inability of Golang to do polymorphism makes it more difficult.
If the result of something is simply a message, and that message can be either a normal data type or an error, then you don't need to always additionally return an optional error. This is why I prefer the idea of message passing languages over direct procedural languages.
It is still possible to do this somewhat in Golang using channels and/or queues, channels can tend to get clogged and freeze up in my experience.
I've had good experiences using Mangos ( Golang nanomsg implementation ). If you use inproc queues, you can do multi-threading in a language portable way. Then, if you decide to replace a processing component with something other than Golang, it is relatively easy to do so.