News for hackers. Stuff that matters.
Re:Twelve Years of Go by AtNightWeCode on Wednesday November 10, 2021 @01:14PM (#29178108)
12 years and still no proper error handling. World stars. ;) Seriously, the error handling is a big problem with Go. Not the way it works, the way it effects how people do control flow in general.
Re:Twelve Years of Go by nineplay on Wednesday November 10, 2021 @01:53PM (#29178761)
Error handling is easily one of my favorite things about Go.

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.

Re:Twelve Years of Go by kbd on Wednesday November 10, 2021 @03:00PM (#29179835)
> No 'oops I forgot to put in a try/catch and now my code died with no explanation'.

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:Twelve Years of Go by grey-area on Wednesday November 10, 2021 @03:18PM (#29180105)
Your users typically don't want the program to crash, spare a thought for them.

Re stack traces, they're fine I guess, but I prefer a well-crafted error message to 200 lines of irrelevant file locations/functions.

Re:Twelve Years of Go by JoeyJoJoJr on Wednesday November 10, 2021 @03:53PM (#29180609)
I want my program to crash obviously during development so that I know of the existence of bugs, which can be ironed out before getting to production.
Re:Twelve Years of Go by grey-area on Wednesday November 10, 2021 @04:01PM (#29180724)
That's the theory.

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.

Re:Twelve Years of Go by kaba0 on Wednesday November 10, 2021 @04:50PM (#29181405)
Which is better than having a bug silently be in production for years to come due to a silent error.
Re:Twelve Years of Go by grey-area on Thursday November 11, 2021 @01:59AM (#29185223)
Which is worse than just handling the error where it occurs.
Re:Twelve Years of Go by kaba0 on Wednesday November 10, 2021 @04:49PM (#29181400)
That’s the point of exceptions. They bubble up! I can manage them at a place where it is appropriate.

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.

Re:Twelve Years of Go by grey-area on Wednesday November 10, 2021 @05:02PM (#29181531)
That's the theory.

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...

Re:Twelve Years of Go by vlunkr on Wednesday November 10, 2021 @03:40PM (#29180425)
> First, I want my program to immediately crash if I have a bug.

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.

Re:Twelve Years of Go by kbd on Wednesday November 10, 2021 @08:01PM (#29183181)
> An error being returned doesn't always mean there's a bug.

The bug is not the error, the bug is the unhandled error.

Re:Twelve Years of Go by nineplay on Wednesday November 10, 2021 @03:36PM (#29180374)
I don't know what scale of projects you've worked on. When you're dealing 100K requests/second you very much do not want to immediately crash. It is absolutely the worst think your code can do.
Re:Twelve Years of Go by kaba0 on Wednesday November 10, 2021 @04:51PM (#29181415)
What framework, language crashes due to an exception, regardless of scale? That exception gets mapped to a http status code and that gets returned.
Re:Twelve Years of Go by the_gipsy on Wednesday November 10, 2021 @02:57PM (#29179784)
Go errors are better than exceptions, yes, but way worse than Result<T,E>.

There are too many ways too screw up error vars + nils + control flows.

Re:Twelve Years of Go by AtNightWeCode on Wednesday November 10, 2021 @02:15PM (#29179134)
1. You have something that catches and logs all uncaught exceptions.

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.

Re:Twelve Years of Go by ssteel on Wednesday November 10, 2021 @02:53PM (#29179716)
The if-statements are not unnecessary if you want to handle errors in place and make the code highly readable. Just because you don't like it doesn't make it wrong.
Re:Twelve Years of Go by nineplay on Wednesday November 10, 2021 @03:51PM (#29180576)
These are great designs that have been completely ignored by developers in every large scale codebase I've ever worked on.

"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

Re:Twelve Years of Go by philosopher1234 on Wednesday November 10, 2021 @02:29PM (#29179362)
>But you should keep things wide and shallow. No matter what technology you use.

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.

Re:Twelve Years of Go by Thaxll on Wednesday November 10, 2021 @02:40PM (#29179506)
1. In most web server you have a panic middleware that does exactly that.

By default in Go uncaught panic go to stdout.

Re:Twelve Years of Go by latchkey on Wednesday November 10, 2021 @04:55PM (#29181450)
There is no catch for array (or slice) index out of bounds. You either code it in yourself or the best you can do is watch for `recover()` and log it.

I wish that one use case was handled better. It is just too easy to mess up.

Re:Twelve Years of Go by nkozyra on Wednesday November 10, 2021 @02:20PM (#29179231)
> Return err, always as the last parameter

Well unfortunately, not always.

Re:Twelve Years of Go by nineplay on Wednesday November 10, 2021 @03:33PM (#29180323)
If you have a good linter it will flag code where err is not last
Re:Twelve Years of Go by fourseventy on Wednesday November 10, 2021 @01:36PM (#29178482)
Thats your opinion. I love the error handling in go. I think its superior to the traditional try/catch approach most other languages use.
Re:Twelve Years of Go by mountainriver on Wednesday November 10, 2021 @01:26PM (#29178323)
It’s never actually been a problem for me, I prefer Go errors over most languages
Re:Twelve Years of Go by jrockway on Wednesday November 10, 2021 @01:47PM (#29178657)
I agree with this. What I like about the Go method is that all errors are equal -- they look the same if it's a stack of microservices running at different companies, a bunch of goroutines communicating state with each other, or if you're just calling a local function that fails. It's the same every time, and the programmer can cut through the noise and make a complicated error from a complicated system simple, concise, and easy to understand.

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.

Re:Twelve Years of Go by AtNightWeCode on Wednesday November 10, 2021 @01:32PM (#29178408)
I can live with the error handling. But as I wrote. The problem is that it encourages if-programming. Something that is a problem in the Go community.
Re:Twelve Years of Go by handrous on Wednesday November 10, 2021 @01:36PM (#29178487)
If it's helping keep the "'if' considered harmful" crowd away, I say keep it. There are plenty of other languages.
Re:Twelve Years of Go by mountainriver on Wednesday November 10, 2021 @03:26PM (#29180218)
Yeah exactly, conditionals around errors are fine. Nothing like being pedantic for no reason
Re:Twelve Years of Go by mseepgood on Wednesday November 10, 2021 @01:37PM (#29178493)
since when is if-programming a bad thing
Re:Twelve Years of Go by skybrian on Wednesday November 10, 2021 @01:46PM (#29178642)
It depends on what kind. I didn’t find maintaining codebases littered with platform-specific ifdefs all that fun.

If statements used for error checking are a bit verbose but basically fine.

Re:Twelve Years of Go by mseepgood on Wednesday November 10, 2021 @01:56PM (#29178815)
Go doesn't have platform-specific ifdefs sprinkled throughout the source. Platform-specific code is separated into files guarded by build tags as recommended in 'The Practice of Programming'. Compile-time control flow is not mixed with runtime control-flow.
Re:Twelve Years of Go by skybrian on Wednesday November 10, 2021 @04:14PM (#29180905)
Sure, that was in C. Go is an improvement.
Re:Twelve Years of Go by AtNightWeCode on Wednesday November 10, 2021 @01:55PM (#29178797)
What you learn at school when it comes to programming is to avoid if-statements for control flow. It is error prone. If-statements are mainly used for various types of guards.
Re:Twelve Years of Go by AnimalMuppet on Wednesday November 10, 2021 @03:46PM (#29180517)
And what you learn after a few years in the real world is that, when school tells you "this is the right way", they're almost always wrong. Or, there at least almost always wrong in many circumstances. The real world is a lot more complicated than they teach you in school, and the correct answer is almost always "it depends".

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.

Re:Twelve Years of Go by mseepgood on Wednesday November 10, 2021 @02:17PM (#29179173)
'if' is the epitome of control flow (next to looping). It's fundamental to computer programming. It's honest. Don't be ashamed of control flow, don't hide your control flow as if it didn't exist.
Re:Twelve Years of Go by morelisp on Wednesday November 10, 2021 @02:37PM (#29179479)
> It's fundamental to computer programming.

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.

Re:Twelve Years of Go by Thaxll on Wednesday November 10, 2021 @02:02PM (#29178935)
vs exception based control flow which is way worse.
Re:Twelve Years of Go by xh-dude on Wednesday November 10, 2021 @04:03PM (#29180743)
I really think Go’s opinion here arises from a sense that some abstractions of control flow just should be verboten for concurrency. For example exceptions dumping a stack trace in a concurrent program is often OK and occasionally extremely cursed.
Re:Twelve Years of Go by nobleach on Wednesday November 10, 2021 @06:07PM (#29182211)
Spectre I think? The mitigation patches make branch-heavy code slower.
Re:Twelve Years of Go by lawn on Wednesday November 10, 2021 @04:10PM (#29180863)
I find it fascinating that some hate it and others love it.

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.

Re:Twelve Years of Go by grey-area on Thursday November 11, 2021 @02:01AM (#29185233)
I think a lot of Go programmers would agree with you.

I prefer the current situation to exceptions but would like to see them try something more like Rust or Elixir at some point.

Re:Twelve Years of Go by kamaal on Wednesday November 10, 2021 @10:56PM (#29184273)
>>the way it effects how people do control flow in general.

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.