News for hackers. Stuff that matters.
Re:Errors are values by rtpg on Monday January 12, 2015 @08:50PM (#8877662)
>We recently scanned all the open source projects we could find and discovered that this snippet occurs only once per page or two, less often than some would have you believe.

once per page? that's once per 30 or so lines. 2 or 3 percent of the code is this same repetitive structure? Also, only takes one time to forget this for everything to fall apart.

We've spent so much time trying to deal with uninit'd pointers and things like that on the language level, and here we have something that's much simpler, yet super repetitive and should be easy to catch on compile time.

Maybe I'm just a bad programmer, but I really prefer the Java "force you to catch exception" model much more to this.

>It's worth stressing that whatever the design, it's critical that the program check the errors however they are exposed.

If it's critical to check the errors, I think that it's worth writing language rules in a way to force people to write a "has error " code path (think pattern matching).

I get go is trying to be as small of a core as possible, but I think there's a lot of value in extra compiler checks (with no runtime cost!) to catch this sort of thing.

Re:Errors are values by bsdetector on Monday January 12, 2015 @10:13PM (#8877910)
> that's once per 30 or so lines. 2 or 3 percent of the code ... I really prefer the Java "force you to catch exception" model much more to this.

And that 2%-3% doesn't include where errors should have been checked but were not. For instance run programs without stdout ("./program 1>&-") and most Go console programs will just happily write to nowhere whereas almost all Java ones error out immediately. Closing stdout doesn't often matter, but sometimes things like this are important.

I don't understand how in the 2010s it's okay to make a language with worse reliability. And worse "JavaDoc" equivalent. Go should be better than C and Java in every category and it's just not better in way too many.

Re:Errors are values by NateDad on Tuesday January 13, 2015 @09:07AM (#8879889)
My go code is way more reliable than my exception-language code, because I know exactly where it can fail and what happens when it does. It is much harder to see where exceptions can happen and what the code paths will be when errors happen.

Why is not having stdout automatically an error condition? Lots of programs should work fine with no stdout. If a program requires stdout and doesn't fall when it's missing, that's a bug (and pretty easy to fix).

Re:Errors are values by bsdetector on Tuesday January 13, 2015 @11:03AM (#8880623)
> Why is not having stdout automatically an error condition?

In UNIX the file handle an opened file gets is the next available, lowest number. Closing stdout means that the program opens a file and it gets file handle 1 instead of a higher number. Then it writes something to stdout, which is an alias for file handle 1, but instead it writes to the opened file.

I believe this caused some security bugs in passwd or maybe su where you could run it without stderr and "Password:" would be written to the passwd file instead of stderr. ...but these specifics are beside the point, which is that it is next to impossible to anticipate the problems an unhandled error can create.

Java's checked exceptions can be annoying when you have to add some extra "throws X" or handle every case, but my experience is that I never had the slightest doubt over how a method could fail or what needed to be done to handle errors. For unchecked exceptions maybe you have a point, although their failure mode for unhandled errors is still to abort the program rather than to continue on with undefined behavior as in Go.

Re:Errors are values by sangnoir on Tuesday January 13, 2015 @12:41AM (#8878350)
Language design is about trade-offs. Broadly speaking: being good in one category means being bad in another.
Re:Errors are values by TheDong on Tuesday January 13, 2015 @02:11AM (#8878553)
While your comment is sort of true, it's not applicable here really.

We know how to handle errors better than this. Go's error handling is C style. We've since found exceptions, Maybes, and friends which are very powerful.

What you said implies that when Rust chooses to use Maybes they're somehow being bad at something. That's simply false; computer science as a field has advanced and one of these advancements (not even that recent of one) is improved understandings of how strong type systems can be used to make code, including errors, safer. Go completely ignores this of course and they're not doing it for a tradeoff that I can see. They gain nothing in terms of performance for not having this type (everything is already a reflect/Value anyways, so much for not having the baggage that you'd need for generics) and everything in terms of having a worse language.

Being good at something doesn't instantly mean you're bad at something else. In fact, I wouldn't even claim that's the general case.

If you disagree, you're welcome to point out the tradeoffs at work here. I think your comment is just a vaguery that is completely untrue in this instance.

Re:Errors are values by enneff on Tuesday January 13, 2015 @05:52AM (#8879021)
The corresponding loss to Rust's gain is language complexity.
Re:Errors are values by kibwen on Tuesday January 13, 2015 @12:55PM (#8881422)
I think this argument is depressingly uncharitable.

One of Rust's goals was to remove the "feature" of null pointers. In its place rose the feature of tagged unions, to support the `Option` type. This is strictly trading one feature for another. Once this necessary task was completed, Rust was happily able to reap all the benefits of tagged unions elsewhere, such as with the `Result` type. It is not greater language complexity, it is merely the reuse of simple language features to empower developers to create more robust software.

Now, I am not among those saying that Go needs tagged unions, or any other feature for that matter (no, not even that one). I think Go is a good language, sometimes to the surprise of my peers. But what's disheartening to me is seeing a Go core developer imply that Go is literally the baseline for programming language simplicity, in effect papering over all the various tradeoffs that programming language design entails.

Re:Errors are values by enneff on Tuesday January 13, 2015 @03:00PM (#8882146)
You mischaracterised my point. Go is hardly the baseline for language simplicity. We just made a different set of trade offs to the Rust team, and I believe Go is the simpler (not simplest) language.
Re:Errors are values by marktangotango on Tuesday January 13, 2015 @08:49AM (#8879767)
>> We've since found exceptions, Maybes, and friends which are very powerful.

I think, and someone correct me if this is wrong, that implementing exceptions in Go is prohibited by go routines.

The stereotypical way to implement exceptions is using setjmp/longjmp which unwind the stack back to a known location. Since go routines are implemented as continuations, any particular executing go routine may or may not have it's stack in context, and I'm not sure what it means to unwind stacks across multiple continuations, ie is it even possible?

So that's one possible example.

Re:Errors are values by pcwalton on Tuesday January 13, 2015 @09:49AM (#8880144)
> I think, and someone correct me if this is wrong, that implementing exceptions in Go is prohibited by go routines.

No. Go already has all the runtime functionality necessary to support exceptions via panic/recover.

Re:Errors are values by NateDad on Tuesday January 13, 2015 @09:11AM (#8879913)
The tradeoff is complexity. You need to define a maybe for your return value, which needs generics. Whereas Go gets 80% of the functionality with multiple returns which are like 10% odd the complexity (and are useful in other ways).
Re:Errors are values by pcwalton on Tuesday January 13, 2015 @09:52AM (#8880158)
You don't have to have generics to use enums for error handling any more than you need generics to use tuples for error handling. The thing you need is enums/algebraic data types/sum types.

See for example the first answer to this SO question on error handling in Swift, which doesn't use generics at all: http://stackoverflow.com/questions/24010569/error-handling-i...

Re:Errors are values by frowaway001 on Tuesday January 13, 2015 @11:44AM (#8880890)
This is nonsense.
Re:Errors are values by curun1r on Tuesday January 13, 2015 @02:08AM (#8878547)
Go's error handling is only a problem because of other missing features...algebraic types and/or generics. With either of those features, you could implement two-track, functional composition with a single bind method. That would go a long way towards only having to write your "if err != nil" once rather than once per function invocation.

Alas, the lack of those other features leads to articles like this one that offer unnatural ways of avoiding something that is pretty much idiomatic in the language.

Re:Errors are values by twotwotwo on Monday January 12, 2015 @09:06PM (#8877713)
> I really prefer the Java "force you to catch exception" model much more to this.

https://github.com/kisielk/errcheck/ will tell you if you silently discard the `err` value (though you can still explicitly assign it to _ to ignore). Seems worth using for a lot of projects.

Re:Errors are values by burke on Monday January 12, 2015 @09:39PM (#8877796)
We use errcheck for all the Go code we write at Shopify. In many cases, we compile the project using a script that fails if errcheck isn't clean, effectively turning into a "you are forced to catch the exception" sort of idea.

It's nice because it's still easy to ignore the err in certain circumstances (e.g. the occasional `_ = f.Close()`), but there's always a visual marker to remind you.

Re:Errors are values by jroesch on Monday January 12, 2015 @11:38PM (#8878170)
The above comments are just pointing out that the compiler should do this for you. It is great that there are third party tools for this, but a simple lint like this should come for free, especially when this is the endorsed error handling policy.
Re:Errors are values by enneff on Tuesday January 13, 2015 @05:54AM (#8879024)
Not everything belongs in the compiler. Go's "vet" tool might be the place for something like this. But, honestly, I have been writing Go code for 5 years and I can count the number of times that "didn't check the error" was the cause of a bug on one hand.
Re:Errors are values by twotwotwo on Wednesday January 14, 2015 @02:45PM (#8888535)
Agree on not in the compiler. A ton of folks seem to dig errcheck; having that sort of check in vet or in the Go distro somehow seems worthwhile. (Of course, if I feel that way, maybe I should put some effort where my mouth is and try and start on a patch. :)
Re:Errors are values by Intermernet on Monday January 12, 2015 @11:51PM (#8878213)
I'm actually quite surprised this functionality hasn't made it into "go vet" yet. It would be useful to be able to do this without a 3rd party utility (although all respect to kisielk for producing such a useful tool).
Re:Errors are values by rtpg on Tuesday January 13, 2015 @12:39AM (#8878346)
pleasantly surprised that you could write go tooling to check this, I hope the team is willing to pull things like this in.
Re:Errors are values by cmelbye on Tuesday January 13, 2015 @12:43AM (#8878355)
> here we have something that's much simpler, yet super repetitive and should be easy to catch on compile time.

It is easy to catch on compile time. https://github.com/kisielk/errcheck