4 ms·
Don't forget this absolute banger: > Obviously Go chose a different path. Go programmers believe that robust programs are composed from pieces that handle the
by masklinn 7y ago
Don't forget this absolute banger:
> Obviously Go chose a different path. Go programmers believe that robust programs are composed from pieces that handle the failure cases before they handle the happy path.
A function which only returns an error can have its result ignored without any warning.
- throwaway894345 7y agoI don’t think that paragraph was referencing compiler guarantees.
- coldtea 7y agoIt should refer to them or address them. Since, given this, Go is no better than a language with unchecked expressions nobody handles...
- stouset 7y agoThere’s other reasons too! I’ve written the following: if v, err = func(); err != nil { nil, err } … and then went on to use `v`. Thanks to `v`s zero value being legitimate (and not nil like a pointer would be), the program continues on as if everything is okay. In case you didn’t catch it, I forgot the `return`. Rust takes a much better approach with Result, where the return value is either `Ok(v)` or `Err(e)`, and there’s no way to access a meaningless value for the other possibility.
- coldtea 7y agoYeah, and this is so simple a change (compiler wise) and far more stronger a guarantee I don't see why Go didn't implement it... At least then errors as return values would be solid. Of course now they make programmers do all the extra error wrapping thing in 1.14 to pass "richer" errors...
- pm90 7y agoEh. It’s verbose but I like it. It makes me think about the code a bit when I have to write a descriptive error wrap. Kind of annoying I guess... I haven’t written ultra large go codebases though so ymmv.
- coldtea 7y ago>It’s verbose but I like it. It makes me think about the code a bit when I have to write a descriptive error wrap. Having the compiler force you, as is my suggestion, would make you think even more -- or not be able not to think and skip the error check or miss it.
- deleted 7y ago[deleted]
- earthboundkid 7y agoYour example doesn't compile: https://play.golang.org/p/KQwXqTZHSPF https://play.golang.org/p/KQwXqTZHSPF Go doesn't allow values to just be referenced without having some use, e.g., JavaScript's `"use strict";` hack could not be done. In general, I have never seen a bug caused by accidentally ignoring an error. It's a theoretical concern, but not a real world problem.
- stouset 7y agoDon’t know what to tell you, I have personally made this mistake and not had this caught by the compiler. I haven’t used go in several years at this point, so it’s entirely possible this is a newly-caught scenario by the compiler. Regardless, the fundamental point stands. Using tuples to return “meaningless” values alongside errors allows developers to mistakenly use those meaningless values.
- throwaway894345 7y agoI do wish Go would adopt sum types, but in practice errors like you describe are vanishingly rare. It’s mostly a theoretical problem.
- tomlu 7y ago> A function which only returns an error can have its result ignored without any warning. It should perhaps be an error to not assign an error to a variable. Internally, Google has linters that enforce this.
- deleted 7y ago[deleted]
- closeparen 7y agoAny serious Go shop would have errcheck as one of the linters in CI.
- masklinn 7y agoApparently linters are bad and that’s why go literally refuses to compile if you have unused imports. But crap like this? No problem, flies right through.
- Vendan 7y agoLinters aren't bad (Go basically has one as `go vet` that checks for all kinds of "this is probably wrong", like the common "closing over a loop variable") Warnings are bad, specifically when the warning is unambiguous (importing a package you aren't using is always wrong, though it makes debugging frustrating at times) The idea is that warnings that don't "stop" the build generally get ignored. Build most non-trivial C++ projects and count how many warnings flow past the top of your screen for an example of what they were trying to prevent.
- earthboundkid 7y agoWhat drives people crazy about Go is the laser-like focus of the designers on real world problems over theoretical problems. Theoretical problem: Someone might mutate a variable intend to be constant. Go designers: Then put a comment saying not to do that. Real problem: People ignore compiler warnings. Go designers: Then eliminate warnings. Real problem: Exceptions can happen anywhere and often go unchecked. Go designers: Then call exceptions "panics" and encourage people not to use them. Theoretical problem: Someone might ignore an error return value. Go designers: Let paranoid people write linters. Etc. etc.
- Vendan 7y agoInterestingly, the one time I introduced someone to Go without really "realizing" it (during a coding interview, got to pick the language I used), his first comment was actually that he liked how explict the error return was. (strconv.Atoi, to be specific). That pretty much sums it all up to me: `if err != nil` seems like annoying boilerplate, but then when you see stuff that's not just doing `return err` inside that conditional, you realize that it can actually be a benefit.