3 ms·
> If you don't know what ??? should be, your code's going to misbehave in production Not necessarily. The programmer may know the ??? code will be dead, but it
by Someone 4y ago
> If you don't know what ??? should be, your code's going to misbehave in production
Not necessarily. The programmer may know the ??? code will be dead, but it’s not realistic to expect the compiler to deduce that.
For example, the programmer may know that, in this program, baz has been checked to be in the range [0,999] ten layers up the call stack, and bar was computed from it five layers up by calling quux, and thus will be in the range [31,98355], which is fine, given that this program tests that int can hold that range at startup.
Not all functions in a program have to be free of undefined behavior in isolation to make a program free of it.
- codebje 4y agoThat's a great example of knowing exactly what ??? means: the program is misbehaving and should terminate. It's dead in the current call stack, but over time programs change and that is how errors creep in. If it's a precondition that _baz_ is in a particular range for the function to behave, why not check that in an assert and save yourself or a future programmer a ton of stress trying to work out why some unrelated part of the code blew up when they made what seemed like a simple change nine layers up the call stack? There's no runtime cost to an assert in production code, but it makes your intent clear to both the compiler and other humans.