You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@directory.apache.org by Stefan Seelmann <se...@apache.org> on 2011/10/09 18:16:07 UTC

Should we use Java assert?

Hi guys,

I just had a strange behaviour that the TestSnapshotBTree worked when
I started it with "mvn test" but not when started from within Eclipse.
I found out that in the test a Java "assert" is used but Java
assertions are used. Java assertions are disabled by default (the must
be enabled with -ea) but the maven surefire plugin enables them by
default so it worked with "mvn test". I just fixed it by using JUnit
assertTrue instead, not a big problem.

But I also discovered that Java assertions are used within the JDBM
productive code. I'm not sure if it was always used or if it was
introduced during the recent refactorings. But I think we should not
use Java assert at all. They should not be used as guards to check
arguments or state because if disabled they don't fire an exception.
They are just not reliable. I think its better to throw something like
an IllegalArgumentException or IllegalStateException in such cases.

Thoughts?

Kind Regards,
Stefan

Re: Should we use Java assert?

Posted by Emmanuel Lecharny <el...@apache.org>.
I agree with Stefan : Asserts where good back when we were coding in C, but
in Java, there are too many drawbacks.

Let's not use them at all.

Thanks Stefan !

On Sun, Oct 9, 2011 at 6:30 PM, Selcuk AYA <ay...@gmail.com> wrote:

>  On Sun, Oct 9, 2011 at 7:16 PM, Stefan Seelmann <se...@apache.org>
> wrote:
> > Hi guys,
> >
> > I just had a strange behaviour that the TestSnapshotBTree worked when
> > I started it with "mvn test" but not when started from within Eclipse.
> > I found out that in the test a Java "assert" is used but Java
> > assertions are used. Java assertions are disabled by default (the must
> > be enabled with -ea) but the maven surefire plugin enables them by
> > default so it worked with "mvn test". I just fixed it by using JUnit
> > assertTrue instead, not a big problem.
> >
> > But I also discovered that Java assertions are used within the JDBM
> > productive code. I'm not sure if it was always used or if it was
> > introduced during the recent refactorings. But I think we should not
> > use Java assert at all. They should not be used as guards to check
> > arguments or state because if disabled they don't fire an exception.
> > They are just not reliable. I think its better to throw something like
> > an IllegalArgumentException or IllegalStateException in such cases.
> >
> I had introduced the asserts with the jdbm refactoring changes
> thinking they are always enabled. I will change them to throw
> illegalstate exceptions as you suggested.
>
>
> > Thoughts?
> >
> > Kind Regards,
> > Stefan
> >
>
> regards,
> Selcuk
>



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

Re: Should we use Java assert?

Posted by Selcuk AYA <ay...@gmail.com>.
On Sun, Oct 9, 2011 at 7:16 PM, Stefan Seelmann <se...@apache.org> wrote:
> Hi guys,
>
> I just had a strange behaviour that the TestSnapshotBTree worked when
> I started it with "mvn test" but not when started from within Eclipse.
> I found out that in the test a Java "assert" is used but Java
> assertions are used. Java assertions are disabled by default (the must
> be enabled with -ea) but the maven surefire plugin enables them by
> default so it worked with "mvn test". I just fixed it by using JUnit
> assertTrue instead, not a big problem.
>
> But I also discovered that Java assertions are used within the JDBM
> productive code. I'm not sure if it was always used or if it was
> introduced during the recent refactorings. But I think we should not
> use Java assert at all. They should not be used as guards to check
> arguments or state because if disabled they don't fire an exception.
> They are just not reliable. I think its better to throw something like
> an IllegalArgumentException or IllegalStateException in such cases.
>
I had introduced the asserts with the jdbm refactoring changes
thinking they are always enabled. I will change them to throw
illegalstate exceptions as you suggested.


> Thoughts?
>
> Kind Regards,
> Stefan
>

regards,
Selcuk