From: Frederik De B. <fre...@pa...> - 2004-06-03 11:23:47
|
I forgot to CC the list as well, so here's my description of "assert": > I might have the wrong idea, but why are there numerous 'assert' calls > in the code, in things like function getRevisionBefore($version) in > WikiDB.php? I'm quoting from the "Programming With Assertions" introduction here: An assertion is a statement that enables you to test your assumptions about your program. For example, if you write a method that calculates the speed of a particle, you might assert that the calculated speed is less than the speed of light. Each assertion contains a boolean expression that you believe will be true when the assertion executes. If it is not true, the system will throw an error. By verifying that the boolean expression is indeed true, the assertion confirms your assumptions about the behavior of your program, increasing your confidence that the program is free of errors. Experience has shown that writing assertions while programming is one of the quickest and most effective ways to detect and correct bugs. As an added benefit, assertions serve to document the inner workings of your program, enhancing maintainability. > All that happens in the wiki is an unsightly error message (and no page template, etc) when it's triggered. Assertions should look like big red warning signs to remind the programmer that something is very wrong. And again, they're not something the client will ever see. A good read is "bug detection with PHP assertions". This article also mentions how to turn off assertions for production code, so that the user doesn't get to see the result of an assertion. Even better, the evaluation of assertions can be turned off completely, improving the speed of your application (although marginally). http://www.sitepoint.com/article.php/1008 Frederik P.S. The mentioned article, Programming with Assertions, can be found here: http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html |