4 ms·
I've been writing some gocode recently and huge chunk of code is if err != nil ... I know you can do if ; err!=nil but that not that much better and you end u
by dominotw 11y ago
I've been writing some gocode recently and huge chunk of code is
if err != nil ...
I know you can do if ; err!=nil but that not that much better and you end up in deeply nested if blocks.
i have to mentally block out err !=nil to read any gocode linearly. How is this acceptable, I don't get it.
https://blog.golang.org/errors-are-values https://blog.golang.org/errors-are-values
We recently scanned all the open source projects we could find and discovered that this snippet occurs only once per page or two
This seems false from my experience, def way more than 1 or 2 instances per page.
- sergiotapia 11y agoI agree, it's exactly why I stopped using the language. err != nil checks all over the place gave me a headache. I since moved on to Elixir and it's really great. I just wish that it would allow me to have just a single deployable binary. That's something I really miss about Go.
- ocean3 11y agoexrm provides a deployable tar file. Granted its not single binary.
- jimjimjim 11y agoNo point in raising an error if you aren't going to do something about it. If you don't care about an error then don't check it. If you do care then handle it. and you don't need massive if blocks, you can use more early returns as part of the err checks.
- dominotw 11y agoI don't exactly understand what you are suggesting. Ofcourse, I care about error and want them to be handled. Problem often is that method I am in doesn't know how to handle the error and have to bubble it up to a place where there is knowledge about how to handle it. Here is Erik Meijer talking about golang exception handling https://www.youtube.com/watch?v=a2ihmMmSfwk&t=577 https://www.youtube.com/watch?v=a2ihmMmSfwk&t=577 The golang guy doesn't answer the question properly and repeats "exceptions are not for control flow". Really disappointing answer and cringeworthy interview.
- dilap 11y agoHard to say without specifics, but my experience writing server code in, say, Python, was, I ended up wanting to handle errors in the best possible way, so my code turned into: try: x := blah() except Foo: yadadad which is worse (IMO) than x, err := blah() if err != nil { yadada } Besides the extra syntactic clumsiness, it was really hard to know what was going to throw exceptions, and which exceptions, and when. Frequently I would get bugs from unexpectedly thrown exceptions. You said "I have to mentally block out err != nil to read...code" -- for me, the error handling code is code, code I want to pay just as much attention to as the non-error path. So for that reason I really like Go error handling. For a client-side script or something, maybe this isn't true; feel free to use log.Fatal or panic if that makes sense for your use case.
- dominotw 11y agoBut, You don't immediately handle an exception right there in the same method majority of cases. If I have function for copying files, that function doest know how to handle filecorruptionexception it just bubbles it up.
- randallsquared 11y ago> for me, the error handling code is code, code I want to pay just as much attention to as the non-error path. You likely want to pay as much attention as required to make things work, but the purpose of your code isn't to generate errors -- errors are what get in the way of the actual purpose of your code. For people who want to get an overview of what some code does, the error code is interesting only after you understand the actual purpose, and sprinkling the error code throughout the code that describe the purpose of the program causes distraction.
- dilap 11y agoA reasonable attitude for client-side code, perhaps, ('oh it blew up, I'll reopen the app and try again/rerun the script'), but I think not so reasonable for server-side code.. There's element of personal preference here. The longer I program the more I favor systems and styles that minimize unexpected problems; explicit error handling is very much in that vein. Another complication is that the word "error" actually encompasses two very different kinds of situations: (1) expected-but-non optimal external conditions (there's no file at that path, the tcp connection got closed) and (2) unexpected conditions caused by a bug (array index out of bounds, the regular expression typed into the code is invalid). Handling (1) gracefully is very much part of the actual purpose of your code: files WILL be missing sometimes, network connections WILL die, so it behooves you and your program to think about these situations as first class. (2) is not part of the purpose of your program, it's an error IN your program. These are best handled by crashing/exceptions/panic. A lot of languages mix the handling of (1) and (2), e.g. Python and some styles of C++, which use exceptions for everything. Go uses explicit error returns for (1) and panics for (2), which feels right to me.
- omginternets 11y agoI really like Go, but I have to admit this still irks me. I suppose every language has it's warts, and this is one of them.