I like the idea of an MWAssert and that was on my personal TODO list for some time (down to the class name). It would indeed allow for unobtrusive one line "unchecked" exceptions.

On Wed, Jul 31, 2013 at 4:42 AM, Tim Starling <tstarling@wikimedia.org> wrote:
On 31/07/13 18:36, Daniel Kinzler wrote:
> Assertions are things that should *always* be true.

> In my mind, assertions should just throw an (usually unhandled)
> exception, like Java's AssertionError.

Indeed. In C, assert() will abort the program if it is enabled, which
is hard to miss. It is not comparable to the PHP assert() function.

> I don't really see how the resulting boiler plate would be cleaner
> or safer:
> if ( $foo > $bar ) { throw new OMGWTFError(); }

The reasons I don't like assert() are:

1. It doesn't throw an exception
2. It acts like eval()

We could have a library of PHPUnit-style assertion functions which
throw exceptions and don't act like eval(), I would be fine with that.
Maybe MWAssert::greaterThan( $foo, $bar ) or something.

-- Tim Starling

Wikidata-tech mailing list

-Aaron S