You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@directory.apache.org by Emmanuel Lecharny <el...@gmail.com> on 2010/06/04 12:15:25 UTC

Should we use assert ?

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 ?

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



Re: Should we use assert ?

Posted by Emmanuel Lecharny <el...@gmail.com>.
On 6/4/10 2:16 PM, Felix Knecht wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> On 06/04/10 12:15, 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 ?
>>      
> +1
> Just make sure, that assert isn't used in places where it's
> shared/apacheds/... specific as an AssertationError is thrown in case.
>    
See my other mail about when to use asserts or not (ie, User API should 
not use them)

> What about error messages? Do we need them here? If so it can blow up
> the code, just imagine something like
> assert ( ( value instanceof String ) || ( value instanceof byte[] ) ) :
> "String or byte[] expected instead of " + value.getClass()
>    

Yes, assert must be used with a message.
>
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v2.0.15 (GNU/Linux)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
>
> iEYEARECAAYFAkwI7rUACgkQ2lZVCB08qHEFLQCfSzuVCIOGenudn7TExxLO/2EL
> 7JIAoIOuHtV2fqafEsVQLnkTjo+mFEpZ
> =QAuM
> -----END PGP SIGNATURE-----
>
>    


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



Re: Should we use assert ?

Posted by Emmanuel Lecharny <el...@gmail.com>.
On 6/4/10 2:21 PM, Felix Knecht wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Reading http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html#usage
> I have to withdraw my +1 and agree with Stefans objections
>    

Ok, not a big deal :)

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



Re: Should we use assert ?

Posted by Felix Knecht <fe...@apache.org>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Reading http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html#usage
I have to withdraw my +1 and agree with Stefans objections

On 06/04/10 14:16, Felix Knecht wrote:
> On 06/04/10 12:15, 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 ?
> 
> +1
> Just make sure, that assert isn't used in places where it's
> shared/apacheds/... specific as an AssertationError is thrown in case.
> What about error messages? Do we need them here? If so it can blow up
> the code, just imagine something like
> assert ( ( value instanceof String ) || ( value instanceof byte[] ) ) :
> "String or byte[] expected instead of " + value.getClass()
> 
> 
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.15 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkwI77AACgkQ2lZVCB08qHGVDACfdHyhixrHw/gfZqQXxxiWdok7
3TEAn1o3pr6VinMwDhEt5/G/lNTbQMBK
=zjLR
-----END PGP SIGNATURE-----

Re: Should we use assert ?

Posted by Felix Knecht <fe...@apache.org>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 06/04/10 12:15, 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 ?

+1
Just make sure, that assert isn't used in places where it's
shared/apacheds/... specific as an AssertationError is thrown in case.
What about error messages? Do we need them here? If so it can blow up
the code, just imagine something like
assert ( ( value instanceof String ) || ( value instanceof byte[] ) ) :
"String or byte[] expected instead of " + value.getClass()


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.15 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkwI7rUACgkQ2lZVCB08qHEFLQCfSzuVCIOGenudn7TExxLO/2EL
7JIAoIOuHtV2fqafEsVQLnkTjo+mFEpZ
=QAuM
-----END PGP SIGNATURE-----

Re: Should we use assert ?

Posted by Alex Karasulu <ak...@apache.org>.
On Fri, Jun 4, 2010 at 4:08 PM, Alan D. Cabrera <li...@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

Re: Should we use assert ?

Posted by "Alan D. Cabrera" <li...@toolazydogs.com>.
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



Re: Should we use assert ?

Posted by Emmanuel Lecharny <el...@gmail.com>.
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



Re: Should we use assert ?

Posted by Stefan Seelmann <se...@apache.org>.
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.

My 2 cents,
Stefan