Re: use of assert in Java [vs. exceptions]
"Giovanni Azua" <bravegag@hotmail.com> writes:
- degrades performance
I have two thoughts on this.
1.) Assume ?div(x,y)? had undefined behaviour when y == 0,
and we wanted to do this in an application:
for( x = 0; x < HUGE; ++x )div( x, y );
The application knows that in the case of y being 0, the
best thing to do is ?f()?, so
if( y )for( x = 0; x < HUGE; ++x )div( x, y ); else f();
. We can see that it is sufficient to do the test /once
outside of the loop/ for a primitive variable ?y? in a
single-threaded application. If the test would be done
inside of ?div? instead, it would not be possible to move
it outside of the loop.
2.) Assume ?div(x,y)? had undefined behaviour when y == 0.
One can always write a wrapper ?defensiveDiv(x,y)? that has
defined behaviour for y == 0 and otherwise the same behavior
as ?div(x,y)?, while it might be slower than ?div(x,y)?.
On the other hand, given only ?defensiveDiv(x,y)? it is not
possible to write a wrapper to get the behavior and run-time
properties of ?div(x,y)?.