A coworker has added the assert command a few times within our libraries in places where I would have used an if statement and thrown an exception. (I had never even heard o
The rule of thumb which is applicable across most languages (all that I vaguely know) is that an assert
is used to assert that a condition is always true whereas an if
is appropriate if it is conceivable that it will sometimes fail.
In this case, I would say that assert
is appropriate (based on my weak understanding of the situation) because records
should always be set before the given method is called. So a failure to set the record would be a bug in the program rather than a runtime condition. Here, the assert
is helping to ensure (with adequate testing) that there is no possible program execution path that could cause the code that is being guarded with the assert
to be called without records
having been set.
The advantage of using assert
as opposed to if
is that assert
can generally be turned off in production code thus reducing overhead. The sort of situations that are best handled with if
could conceivably occur during runtime in production system and so nothing is lost by not being able to turn them off.
Assert is not a substitute for normal flow control like if
or exceptions, because it is only meant to be used for debugging during development.
Assert should only be used in development as it is useful for debugging. So if you want you can use them for developing your website, but you should use exceptions for a live website.
It wholly depends on your development strategy. Most developers are unaware of assert()
and use downstream unit testing. But proactive and built-in testing schemes can sometimes be advantageous.
assert is useful, because it can be enabled and disabled. It doesn't drain performance if no such assertion handler is defined. Your collegue doesn't have one, and you should devise some code which temporary enables it in the development environment (if E_NOTICE/E_WARNINGs are on, so should be the assertion handler). I use it occasionally where my code can't stomach mixed variable types - I don't normally engage in strict typing in a weakly typed PHP, but there random use cases:
function xyz($a, $b) {
assert(is_string($a));
assert(is_array($b));
Which for example would compensate for the lack of type specifiers string $a, array $b
. PHP5.4 will support them, but not check.
No, your co-worker shouldn't be using it as a general purpose error handler. According to the manual:
Assertions should be used as a debugging feature only. You may use them for sanity-checks that test for conditions that should always be TRUE and that indicate some programming errors if not or to check for the presence of certain features like extension functions or certain system limits and features.
Assertions should not be used for normal runtime operations like input parameter checks. As a rule of thumb your code should always be able to work correctly if assertion checking is not activated.
If you are familiar with automated test suites, the "assert" verb is generally used to verify the output of some method or function. For example:
function add($a, $b) {
return $a + $b;
}
assert(add(2,2) == 5, 'Two and two is four, dummy!');
assert(is_numeric(add(2,2)), 'Output of this function to only return numeric values.');
Your co-worker shouldn't be using it as a general purpose error handler and in this case as an input check. It looks like it's possible for the records field to not be set by some user of your library.
Think of asserts as "power comments". Rather than a comment like:
// Note to developers: the parameter "a" should always be a number!!!
use:
assert('is_numeric(a) /* The parameter "a" should always be a number. */');
The meanings are exactly the same and are intended for the exact same audience, but the first comment is easily forgotten or ignored (no matter how many exclamation marks), while the "power comment" is not only available for humans to read and understand, it is also constantly machine-tested during development, and won't be ignored if you set up good assert handling in code and in work habits.
Seen this way, asserts are a completely different concept than if(error)... and exceptions, and they can co-exist.
Yes, you should be commenting your code, and yes, you should be using "power comments" (asserts) whenever possible.