Monday, December 19, 2016

assertions done right

The problem with using assertions to sanity check code with side effects is the hidden dependency on compiler switches, not the idea in itself. libc4life provides a custom set of assert macros that solves the problem. The basic macro evaluates the expression once for side effects, and injects the result into the specified condition; which is finally asserted. Turning off assertions still evaluates expressions for side effects, and errors are thrown out of band to make sure failures are detected. As an added bonus; the macros return the result of evaulating the expression, which means they can take part in longer expressions. Implementation is trivial; but considering it took me 30 years to get a round tuit, maybe posting this can help someone short circuit their process.

peace, out