4 ms·
Normally, asserts are only compiled into debug versions of the code, and removed for release versions. I know a lot of languages default to this, and it alwa
by michaelt 10y ago
Normally, asserts are only compiled into debug versions
of the code, and removed for release versions.
I know a lot of languages default to this, and it always seemed like a bad decision to me. The performance benefits of most checks are trivial compared to the major downside of not testing the exact code you release.
Several times I've had to track down bugs that have been very difficult to find, as they were 'fixed' by side effects of assert statements and hence didn't show up in debug builds.
If a check really can't be performed in the release version for performance reasons, just separate it out into a unit test.
- minipci1321 10y agoFirst part of your message seem to be in contradiction with the second one, but anyways -- keeping asserts in the release image and incurring the penalty of added checks (execution speed + all these text strings stored in the read-only segment) does not make sense without the release-approving validation providing the exact coverage of all tested code. That can be tough to obtain.
- jasode 10y ago> bugs that have been very difficult to find, as they were 'fixed' by side effects of assert statements and hence didn't show up in debug builds. In those cases, the assert() was used incorrectly. However, that's not a reason to include the assert code in release builds. In a similar way, source code will often have troubleshooting code such as: #if _DEBUG // or #ifndef NDEBUG std::cerr << "inspect size: " << invoices.size() << std::endl; // more debugging-related code ... #endif Just because some programmers mistakenly put unintended side-effects in between the #if_DEBUG/#endif doesn't mean we want that code in the release builds. The purpose of assert() and _DEBUG is to not include them in release builds.
- marcosdumay 10y agoYou can't change your program without inserting some kind of side effect. It may be timing in a multi thread system, extra memory available for an out of bounds array read, or even the CPU being kept busy and heating up or avoiding yielding to some other program.
- dllthomas 10y agoYour troubleshooting code flushed stderr. That's a side effect, and occasionally it will matter.
- FranOntanaya 10y agoAsserts scare me, because I know someone out there is using them in place of validation without realizing they may be stripped out any time.