4 ms·
In better languages, the compiler will write that code for you, and eliminate it whenever it can prove that the condition will never be hit. This is one of the
by jiggawatts 4y ago
In better languages, the compiler will write that code for you, and eliminate it whenever it can prove that the condition will never be hit.
This is one of the philosophical differences between C and other languages that makes everyone consider C unsafe. A legacy language that should no longer be used, because all of its users are perpetually incentivised to take shortcuts precisely like the one you've just described.
- zokier 4y agoMost programming languages are not expressive enough to reliably eliminate redundant overflow checks (or overflows); afaik doing so in general way requires something in the ballpark of refinement/dependent types
- jiggawatts 4y agoIn which case the compiler will emit the overflow or bounds checks, which is fine. If the developer is certain that a specific add operation should use unchecked add for performance, then they can override the behaviour as a spot micro-optimisation. That then becomes a "self documenting" thing, easily found by a simple keyword search. To give you an idea of just how rarely this is actually needed or done, there are less than a thousand uses of "unchecked_add()" in all of the Rust code on GitHub! https://github.com/search?q=.unchecked_add%28+language%3ARust&type=code&l=Rust https://github.com/search?q=.unchecked_add%28+language%3ARus...