Article

Home » Server-side Coding » PHP & MySQL Tutorials » Effortless (or Better!) Bug Detection with PHP Assertions

About the Author

Webb Stacy

author_webb Webb is an almost-20-year veteran of the software industry who consults on software development and management issues, having held both technical and business leadership roles. He is involved in understanding how software developers think and the implications for software development tools. Visit him at Stacynetwork.com

View all articles by Webb Stacy...

Effortless (or Better!) Bug Detection with PHP Assertions

By Webb Stacy

January 21st, 2003

Reader Rating: 8.5

Page: 1 2 3 4 Next

If you're anything like me, you enjoy writing code a lot more than you enjoy testing and debugging it. If we could produce higher quality code with less test and debug effort, we'd jump at the chance, right?

Assertions just might do it for us.

Don't get me wrong. Testing and debugging are important parts of development. Give them short shrift at your peril. But the proper use of assertions leads to the earlier and more thorough detection and diagnosis of bugs. This leads to easier fixes, which frees you up for more enjoyable activities. How? Because:

  1. Bugs are harder to fix the later they are detected.
    It's much harder and trickier to find and fix a bug in a live Website than the first time you try out the code you've just written. Assertions find more bugs, sooner. This saves testing and debugging time.

  2. Bugs become harder to diagnose the further the symptom is removed from the cause.
    It is often laborious to work backwards from the symptom to find the cause of a bug. In the case of intermittent bugs, it is sometimes impossible. Assertions can spot the bugs early, before any symptoms show up. In a sense, they create symptoms right where things first went wrong. This saves testing and debugging time.

  3. Assertions are easy to use.
    Most of the time it's not much more difficult than writing comments. This adds very little testing and debugging time.

The small amount of time you invest in putting assertions in your code will pay itself back quickly, spotting bugs at a point when they are easy to diagnose and fix. And if others will use and modify your code (or you'll come back to it yourself after a few months away), these benefits grow both for you and for those others: understanding of the code improves, support requests decrease.

What are Assertions?

Developers in the heat of battle know--or assume--certain things to be true. For example, they may know that a timestamp in a certain cache is never older than a day, that buyers won't choose Extra Large if they are buying one-size-fits-all socks, or that there is an entry in the customers table that matches the customer id in every shopping cart.

But assumptions aren't always correct. Sometimes, it's as simple as not having accounted for an unusual situation. Perhaps a third-party module does let files linger for more than a day, or Extra Large is passed as a default size when no size is specified.

More often, the assumptions that were true when the program was originally written are no longer true as new functionality is added. In enhancing an online store to let people make purchases before registering, maybe there is no longer a guarantee that there's an entry in the customers table at the moment they put something in the shopping cart.

If there are circumstances where the assumptions are not true, developers need to revisit them (and probably fix a bug.) Short of mathematical proof, the best way to find out is to use assertions.
Assertions are run-time checks that a PHP expression is true. If it is, execution continues unchanged; if not, some other action is taken. The default is to issue a warning.

They are normally checked only during development, not after the site has been deployed. This frees developers from worrying about assertion performance issues in production code.

But assertions don't need to be removed from the code to make this happen. There is an easy way to control whether or not assertions execute, as we'll see below. They can stay with the rest of the code and be enabled or disabled as necessary, providing built-in sanity checking when it's time to do further work.

Using Assertions

On the surface, assertions in PHP look just like a function call:

   assert(false);

If assertions are enabled (the PHP default) this statement will cause a warning to be generated:

Warning: Assertion failed in test.php on line 43

Whereas:

assert(true);

will cause no warnings.

In PHP, the argument to assert is either a Boolean value (like the preceding examples) or a string to be evaluated as PHP code. Using the string parameter is generally more informative:

assert('false');

results in:

Warning: Assertion "false" failed in test.php on line 43

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links