Re: Assertions

From: Date: Thu, 17 Oct 2013 15:51:29 +0000
Subject: Re: Assertions
References: 1 2 3 4 5 6 7  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message
On Thu, Oct 17, 2013 at 5:39 PM, J David <[email protected]> wrote:

> Assertions have three defining characteristics:
>
> 1) They are (potentially inefficient) checks run in
> debug/non-production that completely disappear when run in production.
> 2) When they pass, they produce no side effects.
> 3) When an assertion fails, which can only ever happen in
> debug/non-production, the program dies.
>
> Each component of this triad interacts with the others to make
> assertions something different than the other tools in the debugging
> toolbox, which is why they have value.
>
> Talking about catching exceptions thrown by an assertion failure seems
> very contrary to that, because even if PHP is hardcoded to completely
> ignore everything between "assert" and ";" when parsing in production,
> any attempt to catch assertion-related exceptions won't get the same
> treatment and will still be present in the production code.  This can
> cause a change in behavior, particularly in the case of catching all
> exceptions, and encourages the generation of side effects (like trying
> to recover an error) from an assertion test which will no longer be
> performed when assertions are disabled for production.
>

Nobody is arguing that assertions should be used as a control-flow
structure. It's still most useful to implement them as exceptions. Apart
from being a lot better integrated than fatal errors (in particular they
have such fancy things like stacktraces...) exceptions can also be
gracefully handled where necessary. E.g. PHPUnit will catch the exception
and tell you that an assertion failed, but it will still be able to
continue running the rest of the testsuite. A framework (or front
controller in general) can easily catch the exception at the top level and
display an appropriate debugging page (rather than doing ugly
register_shutdown_function hacks that are necessary to have even remotely
graceful handling of fatals). A deamon or server will be able to process
further requests, even if one particular had an assertion failure.
Assertions shouldn't be used for control flow, but it should still be
possible to handle them gracefully. Exceptions offer that, fatals do not.

Nikita


Thread (36 messages)

« previous php.internals (#69637) next »