by csense · 10y ago · 0 comments
This is one of the problems I have with C. You basically have to remember to manually check for an error on every function call, which means (1) an algorithm with short and simple pseudocode becomes lengthy and hard to read real code, because you need to add a ton of error handling, (2) it is easy to forget to handle an error in some place. Exception-based error handling lets you delegate errors to a top-level handler without cluttering up intermediate code (i.e. an exception anywhere in webpage rendering logic should be handled by 500'ing the entire page, which exception-based error handling lets you do without cluttering intermediate functions between the function producing the error and the function handling the error with error-handling logic).
I've been thinking about learning Go, but if its only error handling mechanisms are "pack it into a return value" or "panic() which is like exit(1) in C" then it's a black mark against the language. It's still a black mark if the language has exception-like error handling but it's not considered "idiomatic Go" and the stdlib and a good chunk of third-party libs report errors in return values.
Explicit control flow is more verbose, but it is also easy to spot when the control flow passes over something important by mistake. And as pointed out elsewhere, forgetting to check a return value is a solved problem via static analysis.
Go doesn't force you to deal with errors either. I see plenty of code ignoring them with
retval , _ := MightYieldAnError();
And some languages that have exceptions actually force the client code to handle them, like Java AFAIK .Finally, Go "explicit control flow" didn't have to be verbose like it is, that's a design choice its authors made.
Go is primitive in the sense that it uses C style code patterns , like the famous :
retval = MightYieldAnError( &outError);
Edit: I forgot to mention the panic/recover which ultimately means Go designers implemented some form of exceptions. That basically voids the usefulness of Go error system, and an admission that it's broken.> And some languages that have exceptions actually force the client code to handle them, like Java AFAIK.
But this misses the point. Any error which you have to explicitly ignore is fine, because then you were aware of it at some point and explicitly ignored it, and anyone reading the code can plainly see you explicitly ignoring them.
And errors which can only happen at specific points (like function return values) are also ok, because then you only have to consider error handling and cleanup at those points.
But if you have neither of the above - if you have the situation where errors can spontaneously result from any evaluation of any expression with no explicit notation required - then you have a poor situation indeed.
This means there's no implicit control flow. You can always see where a function might throw, or know that a function will never throw. It also means, performance-wise, the implementation is much more efficient than C++-style stack unwinding.
I've not used this in practice, because I've not tried Swift, but I think it's a good approach.
https://developer.apple.com/library/prerelease/ios/documenta...
So in Go you have the worst of both worlds. Constant error handling code everywhere, AND you have to treat every single expression as if it may suddenly throw panics.
Oh and to make matters even worse, Go has typed nil, which is not equal to untyped nil. So you cannot correctly check interfaces against nil, the most common cause of panics in my experience.
// t is some kind of interface if (t == nil) { // t is untyped nil } else { // t is non-nil OR a typed nil }
I'm not even sure what the correct fix is. Should every interface come with a isValid() method to check if it's usable ?
Go's error model doesn't differ fundamentally from that of C. The built-in unused value error helps a lot by catching a sizable subset of cases, though; Go's designers made the right call there.
It uses error values like in Go, but you must prefix any function which can throw with a try statement or it fails with a compile time error. That way you never forget to handle it, but it's not really an exception either.
For example, you get a nice operator for appending to a string, but not a slice. A zero-valued slice is empty, a zero-valued map is nil.
This isn't to say that Go is bad, it's just that its tradeoffs aren't in service of any big unifying vision. When range can only iterate over a slice, map, string, or channel, there's no forest. It's just four trees.
Ret, ok = Something(things),
There's no need for an "if" here, you've told it to handle a failure.