Timo said:
I wonder when/how to use asserts reasonable. Assert is not supposed to
replace any exception or logging. For example what about constructors:
protected MyClass(Object blah)
{
assert blah != null : "I need blah";
this.blah = blah;
}
That's a rather involved question, actually. Sun's documentation will
tell you to never use assertions to check preconditions (meaning, throw
an IllegalArgumentException or IllegalStateException instead). That
would disqualify your usage above.
I'd loosen that up a bit to say "never replace an exception with an
assert statement for precondition violations". What I'm getting at is
that, for most people, there are a large set of limited-scope "APIs" of
sorts for which we normally wouldn't check preconditions at all. These
often include private methods (APIs exposed only to the current class)
or package-access methods (APIs exposed only to the current package).
In these cases, asserts could be used to check preconditions, since the
going assumption is that the preconditions wouldn't be violated anyway.
In public APIs, though, (which includes protected members of public
classes) I would certainly never use an assertion as you did.
--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation