logoalt Hacker News

josephgyesterday at 10:58 PM1 replyview on HN

I hear you, but sometimes this is what I want.

For example, I’m pretty sure some complex invariant holds. Checking it is expensive, and I don’t want to actually check the invariant every time this function runs in the final build. However, if that invariant were false, I’d certainly like to know that when I run my unit tests.

Using debug_assert is a way to do this. It also communicates to anyone reading the code what the invariants are.

If all I had was assert(), there’s a bunch of assertions I’d leave out of my code because they’re too expensive. debug_assert lets me put them in without paying the cost.

And yes, you should run unit tests in release mode too.


Replies

addaontoday at 2:00 AM

But how do you test the recovery path if the invariant is violated in production code? You literally can’t write a test for that code path…

show 1 reply