Among the C and C++ standard headers there are the two headers assert.h
(for C) and cassert
(for C++), that are especially useful for developers as they define the preprocessor macro assert(...)
This macro allows to check user defined boolean conditions at runtime.
#include <cassert>
void Foo::setArrayEntry(byte index, byte value) {
assert(index < Foo::kArraySize);
array_[index] = value;
}
void Foo::callBar() {
assert(objPtr_);
objPtr_->bar();
}
Invocations of the assert
macro completely disappear when the software is compiled adding the compiler command line macro definition -DNDEBUG
. The assert macro even improves the readability of code as it provides information about invariants and what is expected to happen or not to happen.
The assert
macro is meant to be used in debug versions of a program, exclusively. A debug version of the firmware to be run on the target platform does not make sense. However, soon we will have some sort of regression testing system based on running virtual firmware builds on x86. In such an environment, assertions are very powerful, detecting all those types of errors that the programmer can anticipate.
@jesse & @algernon: Could you please add -DNDEBUG
to the compiler command lines to ensure to prevent any run time assertions for regular firmware builds?
I would definitely encourage any developer to use the assert
macro in any newly developed C++ code that might later end up being regression tested.