On Fri, Jun 4, 2010 at 4:08 PM, Alan D. Cabrera <list@toolazydogs.com> wrote:

On Jun 4, 2010, at 5:10 AM, Stefan Seelmann wrote:

> Emmanuel Lecharny wrote:
>> Hi guys,
>> currently (and probably because nobody uses them, due to some Java 1.3
>> habits we have), we don't use asserts to do simple things like checking
>> methods parameters (pre-conditions).
>> Should we start using them ?
> I never used them before.
> For me assert is a bit magic because they are disabled at runtime and
> the assert statement isn't evaluated unless they are enabled. I prever
> to throw a IllegalArgumentException instead.

I like asserts because they have the dual function of performing useful sanity checking when turned on and also provide implicit documentation to other developers as to what my fundamental assumptions are when I designed my code.  Take for example

       assert Thread.holdsLock(foo) : "I should be protected before you call me";

This is a nice bit of code that explains my assumptions to other developers when they use/extend my code with the added benefit that it gets turned off once the code is deployed in production.

Slavish use of asserts can lead to trouble; they are not a panacea.  Frontline argument and state checking at the client API end should never use asserts for obvious reasons.  However, performing the same kind of heavy weight checking in my internal classes where I have complete control is a bit of overkill; here asserts fit nicely.  They also serve to warn the developer that they are now troweling inside the bowels of a codebase.

Just my 2 cents.

Alan you make very good points. I don't think we should be using asserts for API semantic checks nor going wild with it but your subtle usage to show correctness to readers of the code is very important.

Seelmann's points are also solid as well. I would rather use the proper IAE in conjunction with the proper parameter checks. This falls in line with Alan's comments.

I'd like to see Alan's approach while writing code to help readers understand. Plus it has value when assertion checking is enabled during tests. 

My 2 cents.
Alex Karasulu
My Blog :: http://www.jroller.com/akarasulu/
Apache Directory Server :: http://directory.apache.org
Apache MINA :: http://mina.apache.org
To set up a meeting with me: http://tungle.me/AlexKarasulu