9 ms·
Agree to disagree here. And yes, this article is a follow-up to the linked one. The last section of the blog has a discussion of aesthetic and philosophical co
by zachmu 2y ago
Agree to disagree here. And yes, this article is a follow-up to the linked one.
The last section of the blog has a discussion of aesthetic and philosophical concerns around using errors for control flow. I'm against it, for the reasons I provide. YMMV.
- sethammons 2y agoYour take away in the "ok is harmful" article is incorrect. There is nothing wrong with "ok" - your problem is with shadowing variables. The better organized code couldn't use shadowing. Your example of moving the ok-checks to the end makes as much sense as moving all your err checks to the bottom. If you overwrite a shadow variable before using the value, of course that's bad. The real takeaway should be that one must always be careful with shadow variables and generally use them as immediately as possible.
- zachmu 2y agoMy whole point is that following the convention basically guarantees shadowing in many contexts. That's bad.
- sethammons 2y agoDo you also do err, err2, someOtherErr? Shadowing is the exact same situation. Errors are just values
- zachmu 2y agoYes, it is the same situation. No, we don't tend to use unique names for errors. The idiom is just too strong. This is somewhat mitigated by consistent consecutive error handling, meaning you always immediately dispatch an error on the line after assigning it. Every time we have had bugs because of this, it's because someone tried to be clever and delay handling an error until slightly later (like at the end of an if else chain where each branch might have had an error).
- kiitos 2y agoSure, and every `x, err := f()` shadows any previous `err`. This kind of shadowing is totally normal and idiomatic, in no way bad.
- zachmu 2y agoerr has the same problem as ok, it's true. It's a messy, kind of embarrassing spot in the language and idioms, not something to celebrate.
- kiitos 2y agoI'm not sure how useful it is to adjudicate what is ultimately just a property of the language. It's not something to celebrate, or mourn, or (importantly) try to work around, it's just the way things work. It seems like you're just fighting the language. This is not the path to success :)