by teeray · 1y ago · 0 comments
Once you've been using the language for awhile, you begin to dislike the elaborate system of rugs other languages have to sweep errors under. Errors in Go are right there, in your face, and undeniable that the operation you are doing can be faulty somehow. With good error wrapping, you can trace down exactly which of these `if err != nil` blocks generated the error without a stack trace. If it bothers you that much, you can always make a snippet / macro for it in your editor.
I find it bizzare that go so strongly relies on this pattern, but lacks the features to make sure you actually check for errors.
The pattern we're talking about is returning errors and having to explicitly check for them, right? How does the lack of "algebraic data types/enum/unions" make this pattern un-ergonomic?
How would you forget, exactly? Your tests are going to blow in your face should you ever leave out an entire feature from the implementation.
If you forgot to document that feature in your tests, which is more likely, then you've created a situation of undefined behaviour, not a bug. You've made no claims as to what should happen. All possible behaviours are equally valid.
And no, pattern matching doesn't help you here as you still need to document what happens on those pattern matches. If you forget to document these cases you've still got the very same undefined behaviour. There is no escaping the need for the tests.
It's cool that there is nicer syntax, that your editor can warn you of mistakes while you are typing, and that can save time and all that don't get me wrong, but this idea that you are going straight up forget without any notice of your forgetfulness, as fun as a trope as it is, just isn't realistic.
This implies that the only people who dislike Go's error handling are newbies that "don't get it".
Go's error handling is objectively bad for two reasons:
1. You are never forced to check or handle errors. It's easy to accidentally miss an `if err != nil` check, I've seen sages and newbies alike make this mistake.
> Errors in Go are right there, in your face, and undeniable that the operation you are doing can be faulty somehow.
2. Repeating `if err != nil` ad nauseam is not handling errors. Knowing the operation can be faulty somehow is a good way of putting it, because in most cases it's difficult — if not impossible — to figure out what specific failures may occur. This is exacerbated by the historical reliance on strings. e.g., Is it a simple issue that can be easily recovered? Is it a fatal error?
While I also don't like Go's error handling approach I thought Go compiler gives an error if a variable is unused, in this case `err`. Is this not the case?
This isn't foolproof. If you're calling multiple methods and reusing `err` it won't give an error because it's technically not unused.
There are linters that do, and I am of the opinion they should be added to `go vet`.
> Knowing the operation can be faulty somehow is a good way of putting it, because in most cases it's difficult — if not impossible
Guru was once able to tell you exactly what errors could be generated from any given err. Now that the world is LSP, we have lost this superpower.
Instead we pack best practices in an external tool.
``` if (foo.bar?.baz?.[5]?.bazinga?.value) ```
Is so much nicer than
``` if foo.bar and foo.bar.baz and foo.bar.baz[5] and foo.bar.baz[5].bazinga and foo.bar.baz[5].bazinga.value ```
I honestly don't care which one of those is falsy, my logic is the same either way.
This enables you to ergonomically pass around meaningful domain-oriented objects, which is nice.
Edit: looks like optional chaining is a separate proposal – https://github.com/golang/go/issues/42847
Its funny because when I was a beginner in both I strongly preferred python syntax. I thought it was much simpler.
if dict.get('key', {}).get('key-nested',{}).get....: if dict.get('key', {}).get('key-nested',{}).get....:I tend to use logs with line numbers to point to where errors occur (but that only gets me so far if I’m returning the error from a child function in the call stack.)