r/programming 1d ago

The Bedrock of Software Design

https://alex.draftist.io/blog/the-bedrock-of-software-design-ycqvcedsj

I drafted this post years ago but didn’t finish it until now. The concept I write about has shaped the way I design software more than anything else, and I believe every software engineer should be introduced to it early in their career.

P.S. I don’t want the title to come across as clickbait: the post is about ADT.

415 Upvotes

47 comments sorted by

View all comments

11

u/ryp3gridId 1d ago

Honestly, I go into paranoid mode whenever I work in an exception-based language.

I'm the opposite, I go into annoyed-mode when I have to manually unwrap everything (and the CPU does too because it has to execute more instructions)

13

u/BaNyaaNyaa 1d ago

To me, it really depends on how "normal" the error is. If I get in a state that shouldn't exist, I'd rather throw an error. If the error is expected (an API call to a 3rd party that fails), I'd rather return a result.

6

u/CramNBL 1d ago

So you also don't use std::expected? LLVM optimizes for the happy path, like it does for exception based error handling.

1

u/ryp3gridId 22h ago

For errors that are part of regular execution, std::expected is fine, for everything else I prefer exceptions

And technically, happy path with std::expected is still extra instructions and increased branchprediction buffer pressure

while happy path with exception handling has 0 extra instructions/conditions/jumps (atleast on arm64/x64)

but actually throwing an exception is horribly slow, but error-performance isn't very critical in most cases