Reasonable usage of assert

T

Timo Nentwig

Hi!

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;
}

Timo
 
?

=?ISO-8859-1?Q?Daniel_Sj=F6blom?=

Timo said:
Hi!

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;
}

Asserts should generally be used to catch programming errors, not
runtime errors.
 
B

Bryce (Work)

Hi!

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;
}

Timo

Asserts are usually for testing, and are designed to be null
statements when you build for production.

They are supposed to test pre and post conditions in an object.
 
R

Roedy Green

I wonder when/how to use asserts reasonable. Assert is not supposed to
replace any exception or logging. For example what about constructors:

Assert is to detect a serious program bug, not just something going
wrong like a socket disconnecting or some data being bad.
 
C

Chris Uppal

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:

One way to think of it is: If removing the assertion could conceivably make the
program run differently then you shouldn't be using an assertion.

The only role for assertions is as a kind of documentation.

That's for assertions that are in delivered code -- early code may well have
temporary "informal" assertions that are added in much the same spirit as
System.out.println()s.

(And, of course, this is a "council of perfection", to which reality may only
approximate ;-)

-- chris
 
C

Chris Smith

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top