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.


Regards,
Alan


Reply via email to