logoalt Hacker News

epstoday at 8:54 AM1 replyview on HN

> Can assertions be used in production?

Yes

> What should I be asserting on?

Invariants

> Can I customize how assert behaves?

It should abort the program, logging the stack and whatever the context you pass into in, printf-style. If it doesn't abort, it just buries the issue of the program being in incorrect internal state. It should never be OK.


Replies

benj111today at 10:20 AM

I think part of the problem is that assert is used for different things.

You can use assert to cover a case that should never happen, you can also use assert to catch programming errors during development.

It's arguable that you don't want the second group in a production build. That should have been caught in testing.

But then there's the use where it's a lazy person's if statement. Instead of dealing with the issue assert it. I'm undecided whether this is a net good. Would the test have been implemented anyway?