Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Reasonable usage of assert

Reply
Thread Tools

Reasonable usage of assert

 
 
Timo Nentwig
Guest
Posts: n/a
 
      04-16-2004
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
 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Daniel_Sj=F6blom?=
Guest
Posts: n/a
 
      04-16-2004
Timo Nentwig wrote:
> 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.
--
Daniel Sjöblom
Remove _NOSPAM to reply by mail
 
Reply With Quote
 
 
 
 
Bryce (Work)
Guest
Posts: n/a
 
      04-16-2004
On Fri, 16 Apr 2004 20:42:49 +0200, Timo Nentwig <>
wrote:

>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.

--
now with more cowbell
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      04-16-2004
On Fri, 16 Apr 2004 20:42:49 +0200, Timo Nentwig <>
wrote or quoted :

>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.
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      04-17-2004
Timo Nentwig wrote:

> 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



 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      04-17-2004
Timo Nentwig wrote:
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
To assert or not to assert... ImpalerCore C Programming 79 05-17-2010 12:47 PM
assert 0, "foo" vs. assert(0, "foo") Thomas Guettler Python 3 02-23-2005 07:53 PM
assert(x) and '#define ASSERT(x) assert(x)' Alex Vinokur C Programming 5 11-25-2004 08:48 PM
RE: remove assert statement (Was: Re: PEP new assert idiom) Robert Brewer Python 1 11-07-2004 06:53 PM
Looking for power supply spare for 2924M at reasonable price Rainer Jungeilges Cisco 2 06-17-2004 02:06 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57