No 'oops I forgot to put in a try/catch and now my code died with no explanation'.
No 'I forgot a finally ( or didn't realize I needed one ) and now I'm leaking resources'.
No 'should I return Null or false or an error code?'.
No '20 log messages for the same error because every function is reporting it'.
The Go model
- Return err, always as the last parameter
- When you need more context, add it to the error before you return it
- There is a calling function that reports errors. Maybe main(), maybe a subscriber, maybe a goroutine. There can be other situations of course but the point is that there's a clear ownership. If someone is logging errors in a higher up method, they should be prepared to explain why in code reviews.
2. Defer is nice. As easy to forget as using in C#.
3. Always null/nil. Uncle Bob is plain wrong here.
4. Stack trace. But you should keep things wide and shallow. No matter what technology you use.
In Go it is about as easy to swallow errors as with try-catch. But my post was more about how all the if-statements generates more unnecessary if-statements.
This is literally the exact opposite of what I believe, and the bane of my existence at work. Wide and shallow code is spaghetti. Too many APIs = continuous confusion and relearning.
By default in Go uncaught panic go to stdout.
"That's the developers fault, not the languages"
Look, the old grey mares of the programming world are doing the best they can. I'm not going to blame C because its developers usually make it up as they go.
But we've learned things over the past 50 years, and one of the things we've learned is that company defined 'best practices' and code reviews can not catch all the mistakes that developers make and all of the things that can bring a service to its knees.
Go has the benefit of experience. It knows what mistakes developers make and it is going to prevent them from doing that. There is Right Way To Handle Errors in Go.
How many times have I seen uncaught exceptions? I saw one a week ago.
How many times have I lost resources because I didn't realize something I was calling could throw exceptions? Dozens.
Null/Nil exceptions. I couldn't count the null pointer errors I've had to fix in my time. Probably in the hundreds.
"should" keep things wide and shallow - somehow when Java 8 came out all the Lambda programmers lost this message.
None of these practices scale. We know that because we've seen it
Well unfortunately, not always.
There are too many ways too screw up error vars + nils + control flows.
This is so backwards. First, I want my program to immediately crash if I have a bug. Go is like shell in that it keeps going even if there's an error. Second, I'm used to getting stack traces, Go is the language that will give "no explanation" by comparison if there's a failure.
Re stack traces, they're fine I guess, but I prefer a well-crafted error message to 200 lines of irrelevant file locations/functions.
In practice it's impossible to exercise all the inputs your programs may get, and then they crash in production when users do things you don't expect.
Something had happened during the processing of this web request? Catch the exception and convert it to an appropriate status code.
You should only ever let exceptions fall through ”main” when the problem is really not something you can manage to handle.
In practice often people let the exception eater in main handle it, and it either logs somewhere nobody looks or returns this sort of generic error and exits:
https://www.google.com/search?q=Microsoft+Word+has+encounter...
An error being returned doesn't always mean there's a bug.
> Go is like shell in that it keeps going even if there's an error.
With both Go and shell scripts it's up to you if you want the program to keep going when there's an error.
The bug is not the error, the bug is the unhandled error.
I wish that one use case was handled better. It is just too easy to mess up.
If statements used for error checking are a bit verbose but basically fine.
I'm weakly pro-if, but no, it's not. We went a lot of years without it, and some rending and gnashing accompanied its introduction.
Should you avoid if-statements? It depends. What are you going to have to do instead? You're going to have to do something. Is that something going to be more understandable for your co-workers for the lifetime of the code? Maybe, depending on your co-workers. Maintainability over the lifetime of the code far outweighs and "should" that they tell you at school.
People seem mad that they have to plumb around `err`, and that they have to provide useful context with fmt.Errorf. I see that as letting good programmers make good systems. The default in other languages is useless -- line numbers and arguments is all you are allowed to have, and when something goes wrong it takes up your entire screen with noise. Not as good as everyone thinks it is.
I personally vastly prefer Rusts Result<T, Error> and Erlang/Elixirs {:ok, T} you pattern match on or crash the process and recover.
The code is cleaner while it also makes harder to make mistakes.
I prefer the current situation to exceptions but would like to see them try something more like Rust or Elixir at some point.
This.
Most people are used to thinking in terms of try/catch. Programs exiting on exceptions. This is just one of those things like Garbage Collection. Its one of those things a modern programming language should do.
The example is a bit like using automatic transmission cars. As much you can sing praises of a manual transmission cars(manual control, mileage etc) its just everyday programming is like driving in roads with heavy traffic, and whatever perceived poetic beauty a manual task could offer, the automated task works better if you have to do this for hours everyday.
In many ways a language without try/catch semantics these days is dead on arrival for most shops.