0

I have referred to some sites online and It has made me curious about this function. One site recommended the use of assert as function precondition where it could be used for detecting problems related to manipulation of data during parallel threads. But as people say that assert should be removed during release of the software. But we can detect this problems with a simple if else and exit combo. So I dont get how using assert is any different.

7
  • 1
    assert() will usually compile to no code in a release build. See this Commented Sep 11, 2020 at 7:03
  • If you compile with preprocessor definitions NDEBUG, the definition for the assert becomes a function that takes a boolean and does nothing. Commented Sep 11, 2020 at 7:17
  • preconditions are something you assume to hold, you don't want to test them every time the function is called, unless you are in a debug build, then you use assert Commented Sep 11, 2020 at 7:27
  • @Mansoor in case of NDEBUG the macro will be #define assert(condition) ((void)0) so condition won't be evaluated in release build. Which is an important difference to becomes a function that takes a boolean and does nothing. A reason why you should not have an expression as the condition for assert() that has side effects. Commented Sep 11, 2020 at 7:39
  • @t.niese Takes does not mean evaluate, merely that if you do not provide it, the macro will complain. Commented Sep 11, 2020 at 8:13

1 Answer 1

1

In the end it comes down to there being two (or more) kinds of "errors" that you can encounter in your program:

If you have a text field and ask a user to input a date in "yyyy-mm-dd" format, and the user writes "It is tuesday". Then that is a type of error that you can catch and respond to - and moreover, it is not something that is wrong with the program.

On the other hand, if you have a date picker (displays a calendar and lets the user pick there), then it should only be able to return valid dates (and possibly a "user refused to pick" value). If it can return "2020-14-52", then the date picker code is just broken.

If you are concerned that your date picker may be bugged (maybe you are in the process of writing your own), then you can add some assert(validDate(date)) calls to all the date returns you get from your picker. That way the program will terminate with a big red finger pointing at the failed assert every time the date picker returns an invalid date. This is very valuable during development.

However, once you are satisfied that your date picker is working and only returns valid dates, then all those extra checks are redundant and can be removed. But here the assert has another nice feature; if you compile in debug mode then the check stays, but if you compile in release mode then it becomes an empty call that can be optimized out by the compiler.

In short: Error handling is for stuff that can go wrong during execution in a correctly working program. Asserts are for stuff that must not be able to go wrong (and if it does then something is fundamentally broken in the code).

Sign up to request clarification or add additional context in comments.

2 Comments

Something like assert(validDate(date)) can be problematic. In release build assert is #define assert(condition) ((void)0), as of that validDate(date) won't be evaluated in release build. If validDate has no side effects, it is not problematic, but if it has, then you out of sudden could have different behavior in the release build then you have in the debug build.
@t.niese Good point. It is a made up example, so that validateDate does not actually exist. But it is an important, general point that test and validation code should not have side effects on the rest of the program.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.