Discussion On Asserts
Discussion On Asserts
Agenda
Background
What is Assert
Usage of Assert/Code snippets
Customizing Assert
How not to use Asserts
Evils of asserts
References
Background
Defensive programming:
Anticipating failures
Add supporting code to detect/ isolate
failures
Error Handling
Adding diagnostics to the program
What is Assert
State a fact confidently and forcefully
In C- Assert is a macro used to add
diagnostics to the program
An assertion is a Boolean expression
at a specific point in a program that
will be true unless there is a bug in
the program.
Usage of Assert
assert(expression);
Used to check pre-conditions and
post conditions
When expression is false:
Assertion failed: expression, file
filename, line number is displayed.
Then calls abort();
Example
Customizing Assert
As seen assert displays expression,
file filename, line number when
assert fails
What if the reason for its failure is to
be known?
Example
Evils of asserts
Assert is active only in debug mode,
hence debug only diagnostic will not
help track problems in production code.
False sense of security
assert(skyDiver->AttachParachute());
skyDiver->JumpFromPlane();
References
http://www.programmingsimplified.co
m/c/source-code/assert
Writing Solid Code_ Microsoft's
Techniques for Developing Bug-Free
C Programs by Steve Maguire.
Matthew A Telles_ Yuan Hsieh-The
science of debugging-Coriolis (2001)
http://thesoftwarecondition.com/blog
/2010/08/01/cc-assertions-and-defini
ng-your-own-assert-macro/