Comment by ansc

Comment by ansc 21 hours ago

5 replies

I think they are actually pretty different in approach. rust sprinkle ”?” everywhere and wants to avoid dealing with the error, and golang is more explicit and robust handling. sure, most is similar if it is just ”if err return err” but I have definitely seen more ”extreme” and correct error handling in golang, whereas in rust the convenience of just bubbling it up wins. I still prefer rust, but I am not sure the comparison is as close as people claim

pyrale 20 hours ago

> rust sprinkle ”?” everywhere and wants to avoid dealing with the error

You certainly must handle the error if you want your computation to continue.

> golang is more explicit

Not sure how golang is more explicit. In functor-based style, an error-prone computation stops if it yields an error and that error isn't handled explicitly. That makes sure that any successful computation is based on expected behaviour from beginning to end.

> and robust

Likewise, that's a claim based on nothing. Forcing developers to write a little snippet of code everywhere lest their code has a bug does not make code more robust.

> but I have definitely seen more ”extreme” and correct error handling in golang, whereas in rust the convenience of just bubbling it up wins.

You also have the option to match a result for lower-level error handling in rust.

Claiming that "convenience" makes rustaceans not use that option is like claiming that gophers don't check the error content because it's faster to panic.

  • ansc 14 hours ago

    My bad, I should have written in my experience working on larger codebases. It was a bit too general. It was never about the semantics about the language, but about allowing developers to make the correct choice. Kind of why Golang was built for Google I guess. In practice I've found Golang developers to handle errors where appropriate, where in (binary) Rust applications I've usually found `anyhow` everywhere with `?` to be a bit of a silent issue.

    You're right that the languages have the same capability. The terseness can IMO both be a strength and a liability.

tcfhgj 19 hours ago

Sprinkling "?" everywhere and the properties you mentioned are not really a different approach by the language but by the (some) users.

I am not even sure if Rust is that more convenient in this regard generally. To actually be able to use a ?, you have to actually define a conversion for the error types (which again you define yourself) or explicitly opt into a solution like anyhow, which would allow to use them almost blindly.

In Go, you can just blindly put your boilerplate (potentially using IDE shortcuts/autocomplete as some suggested to me or told me about).

> whereas in rust the convenience of just bubbling it up wins

Not necessarily, I have seen so many ways of error handling at this point, and what is best probably may depend on the individual situation.

In my personal experience, neither using anyhow nor my current default approach (custom error struct and `map_err(Error::EnumVariant)?` have prevented me from acknowledging that a potential error and thinking about if I should handle it in the current function or bubble up, although I agree that the perceived cost of handling an error may be increased, because you add comparatively more code in Rust.

Further, I feel like what Rust does is already the upper limit of boiler plate I can tolerate, although I am not sure there is a better way of this type of error handling.

  • Mawr 4 hours ago

    The difference between just "?" and "?" + <good error message> is much greater than the difference between "if err != nil { return err }" and "if err != nil { return fmt.Errorf("<good error message") }".

    Part of it is the syntactical easiness of "?", how can any other solution compete with the ease of typing a single character?

    But mostly, it's that "fmt.Errorf" is built-in and widely used, as opposed to whatever library you need to go out of your way to choose to be able to easily annotate errors in Rust.

    • tcfhgj an hour ago

      But to be able to actually use just "?" you need a specific library as well. So according to your wording you go out of your way to to be able to just use "?".