Thursday, November 4, 2010

ASSERT - how it should be

ASSERT in case of failure should dump the file/line along with string presentation of condition. Ideally some extra info similar to TRACE output:

ASSERT( conditionToFail)( var1, var2,...);

In debug mode it should ask to "break, ignore, ignore forever". On break set programmatic breakpoint and call debugger, than call condition once more.
Depend of interactivity ability (console, UI, none) prompt could be shown or not.

while( !condition )
    static boolean ignoreForever = false;
    if( ignoreForever )

    log( "condition=" #condition# " var1=" #var1 "var2=" #var2 );
    int pr = prompt( "assertion condition " #condition# " failed. break, ignore, ignore forever"? );
    if( pr == ignore )
    if( pr == IgnoreForever )
        ignoreForever = true;
    debugger; // programmatic breakpoint
    int repeatAgain = 0;
    condition; // second call for troubleshooting in debugger
    if( !repeatAgain ) // in debugger set to 1 and will cycle again

1 comment:

  1. Sometimes handy to put ASSERT into expression. It should than give back the value of condition.
    int c = test(a,b,c) ? TRUE: ASSERT( test(a,b,c) )(a,b,c), FALSE;

    That trick is quite difficult to make as macro. Something similar I done before.