EnumSet + contains: strange behavior

U

Ulrich Scholz

Dear all,

The following example of EnumTest seems to be inconsitent with the
interface definition of contains: It executes the lines marked with
XXX, which clearly is an error. Does anyone have an explanation?

Thank you,

Ulrich


import java.util.*;

public class EnumSetTest
{

public enum EnumTest
{
ONE,
TWO,
}

public final void testEnumSet()
{
final EnumSet<EnumTest> result = EnumSet.noneOf(EnumTest.class);

if(result.isEmpty())
{
System.out.println("empty"); // enters this branch: correct
}
else
{
System.out.println("not empty");
}

if(result.contains(EnumTest.ONE));
{
System.out.println("error"); // XXX
}

if(result.contains(EnumTest.TWO));
{
System.out.println("error"); // XXX
}
}
}
 
O

Oliver Wong

Ulrich Scholz said:
Dear all,

The following example of EnumTest seems to be inconsitent with the
interface definition of contains: It executes the lines marked with
XXX, which clearly is an error. Does anyone have an explanation?

Thank you,

Ulrich


import java.util.*;

public class EnumSetTest
{

public enum EnumTest
{
ONE,
TWO,
}

public final void testEnumSet()
{
final EnumSet<EnumTest> result = EnumSet.noneOf(EnumTest.class);

if(result.isEmpty())
{
System.out.println("empty"); // enters this branch: correct
}
else
{
System.out.println("not empty");
}

if(result.contains(EnumTest.ONE));
{
System.out.println("error"); // XXX
}

if(result.contains(EnumTest.TWO));
{
System.out.println("error"); // XXX
}
}
}

There are semicolons after the if statement. Get rid of them.

- Oliver
 
T

Thomas Hawtin

There are semicolons after the if statement. Get rid of them.

I once spent a day or two trying to optimise some embedded code before I
realised I had a semicolon in the middle of an if statement (pessimising
the code would have actually made it sound better).

So, always put opening braces where god intended (and as instructed by
the Java coding standard). And never treat them as optional in
if/else/for/do/while statements (except in the else-if chain
idiom).</code-style-police>

Tom Hawtin
 
D

Dale King

Thomas said:
I once spent a day or two trying to optimise some embedded code before I
realised I had a semicolon in the middle of an if statement

Lucky you. Early in my career I spent a week trying to debug a problem
like this (and I use that term loosely because the only debugging tool I
had was printf and this was in an interrupt so I couldn't call printf).
After a week I decided to scrap it and rewrite it without the
interrupts. It wasn't until I was half-way done rewriting it that I
found the semicolon. And of course I didn't keep a copy of the old code.
Doh!
(pessimising the code would have actually made it sound better).

Did you ever read the famous "Pessimal algorithms and simplexity
analysis" paper? It is a must read:

http://www.cs.uiuc.edu/class/fa05/cs473ug/resources/pessimal-algorithms.pdf
So, always put opening braces where god intended (and as instructed by
the Java coding standard). And never treat them as optional in
if/else/for/do/while statements (except in the else-if chain
idiom).</code-style-police>

And using a tool like checkstyle to enforce it.
 
O

Oliver Wong

Dale King said:
Thomas said:
Oliver said:
if(result.contains(EnumTest.ONE));
{
There are semicolons after the if statement. Get rid of them.
[...]
So, always put opening braces where god intended (and as instructed by
the Java coding standard). And never treat them as optional in
if/else/for/do/while statements (except in the else-if chain
idiom).</code-style-police>

And using a tool like checkstyle to enforce it.

FWIW, here's how I discovered the problem: I took the OP's SSCCE, copied
and pasted it into Eclipse, and hit CTRL-F to have Eclipse automatically
format the code for me. I always do this before actually reading the SSCCE,
because USENET tends to mangle the indentation. Eclipse formatted the code
as:

if(result.contains(EnumTest.ONE))
;
{
bla bla bla;
}

so it was immediately obvious to me what the problem was.

- Oliver
 
U

Ulrich Scholz

My suggestion is to have Eclipse mark empty block the same way as,
e.g., unused variables are marked. I had a short look at the Eclipse
homepage but I couldn't find the proper place to place this suggestion.
Any ideas?

Ulrich
 
P

Patricia Shanahan

Ulrich said:
My suggestion is to have Eclipse mark empty block the same way as,
e.g., unused variables are marked. I had a short look at the Eclipse
homepage but I couldn't find the proper place to place this suggestion.
Any ideas?

Ulrich

If they do that, it needs to be balanced by some annotation or other
convention to indicate that the programmer really meant the empty block.
Before annotations, I used:

while(some_complex_condition){
// EMPTY
}

Patricia
 
D

Dale King

Patricia said:
If they do that, it needs to be balanced by some annotation or other
convention to indicate that the programmer really meant the empty block.
Before annotations, I used:

while(some_complex_condition){
// EMPTY
}

And this is why I suggested checkstyle which has an eclipse plug-in. You
can configure it to generate warnings or errors in Eclipse if you are
missing braces, have empty statements (standalone ;), or empty blocks.
The empty block check can be configured to require an actual statement
or just text (i.e. a comment). You can also configure it differently
based on what type of block (e.g. allow only comments for catch but
require a statement for if and else).

See:
http://checkstyle.sourceforge.net/config_blocks.html#NeedBraces
http://checkstyle.sourceforge.net/config_coding.html#EmptyStatement
http://checkstyle.sourceforge.net/config_blocks.html#EmptyBlock
 
C

Christian Kaufhold

Patricia Shanahan said:
If they do that, it needs to be balanced by some annotation or other
convention to indicate that the programmer really meant the empty block.

continue;

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top