Slashdot Mirror


User: normangray

normangray's activity in the archive.

Stories
0
Comments
1
First seen
Last seen
Profile
(view on slashdot.org)

Comments · 1

  1. That example is a WRONG use of assert -- you're using assert() as a poor-man's exception.

    The right way to code that is

    if (foo == null) { // bad state
        return some_error_value;
    }
    assert(foo != null); // do something which presumes that foo is not null...

    Writing an assert in your code is the statement that you have examined your code and believe you have proved that it is _impossible_ under any circumstances, for the assertion to be false -- not bad user input, not a bad call from client code, there are no possible bad conditions you haven't already handled, nothing.

    Thus if an assertion evaluates to false, the impossible has just happened. Black is white, gravity points upwards, your computer is an iguana. Your program has (in effect) already crashed, but the world hasn't noticed yet.

    So, what do _you_ want to do at this exciting point, in production code? Just carry on with your fingers crossed and see what happens next?