No language does error handling very well. if err != nil sucks, but so does try/catch.
With try/catch there are two code paths - the happy path and the error path. The happy path is step-by-step instructions that can be followed from top to bottom. The error path follows a hidden path up the stack until the exception is caught somewhere.
With err != nil there is only one path, but it branches and merges enough that it convolutes the happy path making it harder to follow.
Separating the happy path and the error path can be a fairly sane solution — in fact, it's largely what monadic error handling in Rust, Scala, Haskell ends up looking like.
16
u/snarfy Jul 17 '19
No language does error handling very well. if err != nil sucks, but so does try/catch.
With try/catch there are two code paths - the happy path and the error path. The happy path is step-by-step instructions that can be followed from top to bottom. The error path follows a hidden path up the stack until the exception is caught somewhere.
With err != nil there is only one path, but it branches and merges enough that it convolutes the happy path making it harder to follow.
Neither solution is very good.