logoalt Hacker News

zffryesterday at 6:49 PM2 repliesview on HN

At least on iOS, asserts become no-ops on release builds


Replies

josephgyesterday at 8:25 PM

It really depends on the language you use. Personally I like the way rust does this:

- assert!() (always checked),

- debug_assert!() (only run in debug builds)

- unreachable!() (panics)

- unsafe unreachable_unchecked() (tells the compiler it can optimise assuming this is actually unreachable)

- if cfg!(debug_assertions) { … } (Turns into if(0){…} in release mode. There’s also a macro variant if you need debug code to be compiled out.)

This way you can decide on a case by case basis when your asserts are worth keeping in release mode.

And it’s worth noting, sometimes a well placed assert before the start of a loop can improve performance thanks to llvm.

show 1 reply
addaonyesterday at 7:09 PM

You can (and probably should) undef NDEBUG even for release builds.