On 19/03/12 21:43, Christian Aistleitner wrote:
Being able to vary based on configuration actually is a feature. An essential one. It lowers assert's impact on performance. But there is no need to mess with configuration. asserts work out of the box. You are only given the possibility to turn them off.
The ability to turn off asserts in C is damaging to system security and stability, and is part of C's toxic culture of trading off program correctness for negligible performance improvements.
There are cases where it does make sense to optimise for every last clock cycle, but such cases are very rare in modern programming.
In another post:
Have you tried real world examples?
[...]
function funcAssert() { assert( '$this->isOpen() && $this->mConn' ); $this->mConn++; }
[...]
assert_options( ASSERT_ACTIVE, 0 );
Yeah, very clever. Look, I have a test case where assert() is faster as well:
<?php function foo() { sleep(100); return true; }
assert_options( ASSERT_ACTIVE, 0 ); $t = microtime(true); assert('foo()'); print (microtime(true) - $t) . "\n"; $t = microtime(true); if (!foo()) { throw Exception('assert!'); } print (microtime(true) - $t) . "\n"; ?>
Wow, assert() is 17 million times faster that if() in this case! We should really use assert()!
My previous test of assert() involved a case where the assert() and the if() were doing roughly the same thing. In such cases, if() is faster, because it is not a function call.
The same holds true for the very software MediaWiki is built on. The software uses and relies on asserts, but gives you the possibility to turn assertion checking off. * MySQL uses asserts [1]. * PHP uses asserts [2].
asserts and the possibility to turn them on and off is not confusing there.
But MySQL and PHP are not the only adopters of asserts. Just take about any quality software. The source code takes advantage of asserts (e.g.: Libreoffice [3])
But it's not only practical software engineering. Literature is also strongly in favor of using asserts as well: In books: E.g.: S. McConnell. Code Complete [4] In papers: E.g.: G. Kudrjavets, N. Nagappan, T. Ball. Assessing the Relationship between Software Assertions and Code Quality: An Empirical Investigation [5] In talks: E.g.: T. Hoare. Assert early, assert often [6]
assert() is better than nothing. It's not better than exceptions and unit tests, especially not in PHP.
assert() in PHP shares very little in common with assert() in C. In C, assert() is an empty macro by default. In PHP, by default it raises a warning. PHP doesn't have macros, so to simulate the C performance feature, you have to put the source code inside a string, hiding it from automated source analysis and maintenance tools, and breaking syntax highlighting. I don't think you can defend the PHP feature with references that talk about the C feature.
-- Tim Starling