Re: Ensuring a method is overridden
On 09.09.2009 05:01, Roedy Green wrote:
On Tue, 08 Sep 2009 22:42:31 +0200, Robert Klemme
<shortcutter@googlemail.com> wrote, quoted or indirectly quoted
someone who said :
Also, asserts are (and should be) OFF most of the time.
An assert on program structure needs to be on only once.
I am not so sure about turning asserts off. Why?
1. a clever compiler optimises them out or makes them low overhead.
They cannot be optimized away by the compiler because enabling and
disabling them is a function of the JVM - not a compile time option.
Regarding the overhead: depending on the algorithm needed to verify
assertions they can take considerable runtime. This is nothing you want
to invest all the time. I have more than once done something like
assert classInvariant();
private boolean classInvariant() {
// complex calculation that returns true or false
}
As an example think of a class which contains a collection where only
objects with certain properties are allowed. The invariant checking
method might have to traverse the complete collection (which could be
large) and test every single object. Whatever optimization you are
going to make, you won't get this really fast.
The nice thing about this is - and that's where the smartness lies -
that the runtime won't even execute method classInvariant() at runtime
if assertions are off for that class.
As Lew excellently explained: assertions are to be used during
development and test phases - not in production.
Kind regards
robert
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/