Thursday, April 28, 2005

Assert Yourself

Well, assertiveness is a nice quality to have. It's a virtue. The same thing holds good for programming. In my article Software Programming can be humbling. Point 7 talks about assertions.

assert-0

The line above shows the affect of a bug in a software system. The line represents the state of the system. The bug is activated at the point "bad" in the line. The earlier we catch the bug, the better the chances of avoiding an ill affect later. Assertions can help catch bugs early and leave the system in a more stable state. This enables us to debug a little better.

In "C" assertions are supported by including assert.h.
assert(condition), takes in one parameter. From the man page of assert


The macro assert() generates no code, and hence does nothing
at all. Otherwise, the macro assert() prints an error message to stan-
dard output and terminates the program by calling abort() if expression
is false (i.e., condition equal to zero).

The purpose of this macro is to help the programmer find bugs in his
program. The message "assertion failed in file

No comments:

Ranking and Unranking permutations

I've been a big fan of Skiena's Algorithm Design Manual , I recently found my first edition of the book (although I own the third ed...