Hacker News new | ask | show | jobs
by JoeAltmaier 5581 days ago
Perhaps the worst problem with checking-vs-exceptions is, either solution dominates your code structure, obscuring the algorithm logic.

The holy grail would be some method of ensuring the code cannot fail e.g. weirdly constrained argument semantics. Thus separating algorithm from constraints instead of shuffling them together on the page like a deck of cards.

2 comments

If only c++ had some sort of static type system which could be leveraged to provide compile-time checks...

But seriously, this is a large part of the power of c++'s type system. Taking the article's example, if the argument types were of (user class) 'non_zero_float', there's no possibility for error.

You still have to check that your input is non-zero at some point, but you've now focused it into one place (the 'non_zero_float' class ctor), and other chunks of your program depending on those type semantics no longer need to worry about it.

You can really make that type do a compile-time check on runtime values?

It would be better to have some way of getting the compiler to optimize constraints, perhaps by proving at compile time that the error is impossible.

You can't prevent exceptions when you do IO or dynamically allocate memory.