We often get asked “what’s with all those assert statements in your code”?
This concept was hammered in my head when I was a junior programmer, so I
feel kind of old every time I am asked to explain it. But hey…
An assert is usually defined as a “debug-only macro that aborts execution if its argument is false”. (See Steve Maguire’s Writing Solid Code) In other languages, like ActionScript, where you don’t have macros, you can use other techniques to achieve the same effect. The idea is that you use an assert as a diagnostic tool to help you narrow down runtime problems in debug builds. If a function asserts that the arguments passed to it are valid (non-NULL, within an expected range or whatever else constitutes valid in the context of the function), you will know as soon as something dodgy comes in, because your app will crash inside that function in debug. Without the assert the function may not crash, but instead pass the bad data down the line, making the chain of responsibility, and thus the time it takes you to get to the source of the bad data, longer.
Leave a Reply