News for hackers. Stuff that matters.
Re:Declined Proposal: A built-in Go error check function, “try” by stcredzero on Tuesday July 16, 2019 @09:04PM (#20455493)
So long as we can all agree that it feels super bad, I guess this is fine

Exceptions feel super good, even as you're taking too many shortcuts and glossing over things. Proper handling of the unhappy paths is often going to feel like a slog, because it often is complicated, and it's often a slog. Glossing over error handling in golang can feel bad exactly when it should.

Re:Declined Proposal: A built-in Go error check function, “try” by KirinDave on Tuesday July 16, 2019 @09:26PM (#20455591)
> Exceptions feel super good, even as you're taking too many shortcuts and glossing over things.

I'm not sure I follow this talk about "short cuts.". Exceptions provide stronger guarantees by default than multiple-value-bind and manual if statements. Your code fails closed as opposed to open.

> Proper handling of the unhappy paths is often going to feel like a slog, because it often is complicated, and it's often a slog.

I'm used to languages where this generally isn't the case, but sure. Why twist the knife in the wound though?

> Glossing over error handling in golang can feel bad exactly when it should.

When you capture but fail to check an errval and an untraceable default value starts rattling around inside your code causing unexpected behavior? It's like an NPE but worse because it won't immediately trap. It's more like a double free.

Re:Declined Proposal: A built-in Go error check function, “try” by schrodinger on Tuesday July 16, 2019 @09:43PM (#20455679)
go vet catches that scenario and won’t pass CI. You have to explicitly decide to ignore it by using an underscore for the err value and then you get just what you asked for.
Re:Declined Proposal: A built-in Go error check function, “try” by KirinDave on Tuesday July 16, 2019 @09:55PM (#20455740)
Sure, but it doesn't catch the case where you have multiple error variables in scope and you refer to the wrong one in one place but not the other or if you reuse an error.

I've seen that happen more than a few times. Some of it in new production code.

Re:Declined Proposal: A built-in Go error check function, “try” by apta on Tuesday July 16, 2019 @11:43PM (#20456221)
fmt.Println says otherwise. That and it's possible to overwrite previous error variables, something that is not possible with exceptions.
Re:Declined Proposal: A built-in Go error check function, “try” by philwelch on Wednesday July 17, 2019 @09:17AM (#20459077)
In some contexts, exceptions are just cleaner and easier. If I’m writing a web backend, my error handling is going to almost always be, “stop trying to do things and generate a 4xx/5xx response”. Throwing an exception from anywhere and then handling it at the top of the request handling does that without having to tediously carry errors all the way up the call stack.
Re:Declined Proposal: A built-in Go error check function, “try” by stcredzero on Wednesday July 17, 2019 @02:25PM (#20462549)
In some contexts, exceptions are just cleaner and easier.

Definitely. The question is, are the benefits in those contexts worth the cost of the potential abuses? As a codebase and company get larger and larger, the higher the probability that a problem will get into the codebase.

There is an analogy here with C++ templates and with method_missing meta-programming. There are some contexts where they make things a lot easier. However, in a large codebase, the probability that some programmer, somewhere is going to cause a costly problem with a bit of nifty overreach goes up and up.

Re:Declined Proposal: A built-in Go error check function, “try” by philwelch on Wednesday July 17, 2019 @10:43PM (#20466238)
While true, HTTP services in particular are a very, very common context.
Re:Declined Proposal: A built-in Go error check function, “try” by mikeschinkel on Friday July 19, 2019 @03:20PM (#20481654)
Then use panic() and recover() for that special type of use-case. Just as easy as exception handling.
Re:Declined Proposal: A built-in Go error check function, “try” by philwelch on Sunday July 21, 2019 @08:41PM (#20494639)
That's just exception handling with extra steps! ;)
Re:Declined Proposal: A built-in Go error check function, “try” by mikeschinkel on Friday July 19, 2019 @03:18PM (#20481648)
But then you have to spend lots of extra time trying to discern between the errors that were okay, and the errors that actually indicate flaws in your code.

As they say, an ounce of prevention...

Re:Declined Proposal: A built-in Go error check function, “try” by philwelch on Sunday July 21, 2019 @09:03PM (#20494697)
It never really seemed like that to me, to be honest.

Any error that I have forgotten to handle is a bug, in the sense that it was a bug to have forgotten to handle the error in the first place. So the standard behavior of "treat every unhandled exception as a 500 response" is technically correct there. Upon consideration, some errors are actually so rare and/or catastrophic that I don't even have to worry about handling them because I wouldn't do anything different anyway. For example, suppose I have a database-backed service that has no meaningful failover if the database is down, and it throws an exception whenever the database is down. There is no reason for me to have to handle that error.

There are other error cases where I have to stop handling the request but it isn't my fault. So if my service has an endpoint that expects a JSON POST body and the body doesn't even parse as JSON, maybe I need to handle that so it throws a 400 instead of a 500. But I could just wrap that exception with a custom class that my error handler generates a 400 response for, and then the rest of my code could just go on assuming that the JSON did parse and that it was valid.

Probably the closest equivalent would be if you had a forking web server where each request had its own thread, and you could effectively kill the thread with a response object at any time. It's not really a case of "using exceptions for flow control" or whatever; it's more a case of "this entire unit of execution is finished and can kill itself just as soon as it's done leaving a note".