I'm Michael Suodenjoki - a software engineer living in Kgs. Lyngby, north of Copenhagen, Denmark. This is my personal site containing my blog, photos, articles and main interests.

Updated 2014.02.20 22:58 +0100

 

C++ assert best practices

This blogpost is about best practices in using assert in C++.

For C++, the assert() macro can be used by including the cassert header file. In C it is defined in the assert.h file.

For me, an assertion is documentation that reason about how the design and the code should work. They are pre-conditions, post-conditions and loop invariants that the programmer make up to document expectations for a programs execution. It is the programmers contract with his design understanding.

As such they are a very important understanding for how the program work. If an assert fires and you do not agree with it, do not just remove it. However, make sure that the reasoning in the assert and the surrounding comments (design documentation) are correct. If you are unsure about what the reasoning is, it should be brought up for discussion. Otherwise you easily end up in a situation in where programmers understands and develops the code opposed to one another. It is hardly a good thing for the program code if the programmers thwarts each other.

Best Practices

A major advantage of using asserts is that when an assert is triggered it is detected immediately and directly, rather than causing problems later through often obscure side-effects. Since an assertion failure usually reports the code location, one can often pin-point the cause without further debugging. In some systems, e.g Windows, the assert will be shown in a dialog window with buttons that allow you to ignore the assert, i.e. the program continues, or open/start the debugger with the assert line of code in focus. In other systems, e.g. Unix/Linux, the assert will abort the program and trigger a core dump.

Temporarily turning off asserts?

However if you get an assert it may be in a piece of code that is totally unrelated to the task at hand. Should you spend time immediately to fix the problem that the assert is telling you about? Well, if you can you should definitely do it. But often, you do not have enough understanding of the code surrounding the assert or the responsible developer is unavailable for discussion. In this case the assertion, becomes an obstruction for your task in hand. It is indeed annoying to have to click Ignore to several assert-windows popping up. It would be nice if you at runtime can turn off assertions temporarily. This may be possible if you wrap and use your own assert macro instead of the built-in assert() macro. The wrapper may be guarded by a if-statement checking a global variable that you can turn on/off for whether you want assertions to be shown or not. If you want can also output the assert to a log file.

Note that this is different than setting/defining the NDEBUG macro that compiles away all the asserts. Using NDEBUG will not make it possible for you to turn asserts on/off at runtime. NDEBUG is only recommended when compiling release builds.

More (February 2014):

More (December 2013):

More (November 2012):

Sources and More: