On 6/4/10 2:10 PM, 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.

It's funny because I was an heavy user of asserts when I was writing C code, and it saved me a hell lot of time.

Here, we should make a distinction between using asserts and if(). Let me take an exemple.

Suppose we are writing an User API, with a method taking a parameter that should not be null. Clearly, using an assert in this case is *not* valid, as it might be disabled art runtime. Now, if we are writing a method used internally, then there is no need to do a if( condition not met ) then throw new IllegalArgumentException, as it's unlikely to happen, if we have tests covering those cases.

However, I won't push to much if you feel more comfortable using an if+ IllegalArgumentException. In any case, what is important is to cover the case of bad arguments being used...
My 2 cents,
Stefan




--
Regards,
Cordialement,
Emmanuel Lécharny
www.nextury.com


Reply via email to