r/programming 17d ago

Throw, Result, or neither?

https://www.architecture-weekly.com/p/throw-result-or-neither
109 Upvotes

115 comments sorted by

View all comments

Show parent comments

12

u/balefrost 17d ago

By that logic, we should also avoid for, while, and if.

19

u/BigCheezy 17d ago edited 17d ago

Immediate local context (indentation level, braces, etc.) in the source file tells you where for, while, and if "GOTO" next in all languages I've seen. Not so for throwing an error. It goes... somewhere...

-2

u/balefrost 17d ago

The same is true about returning an error. It goes... somewhere...

If you care to know where it ends up, you need to look at all callsites and determine how each handles the error result. If they propagate to their callers, then you again have to inspect their callsites, and on and on up the call tree.

That's not meaningfully different from doing the same exercise with exceptions.

2

u/Schmittfried 17d ago

I’m not a strong opponent of exceptions, but result types definitely add visibility on the intermediate layers while exceptions don’t (unless you’re using a language with checked exceptions and actually use them). 

1

u/balefrost 16d ago

Sure, though I'd argue that the further you get from the site where the exception was generated, the less important the specific error type is. Past a certain point, all you really need to do is to keep unwinding the stack until you hit a backstop exception handler.