Share

checkstyle

Tracker: Feature Requests

5 allow InnerAssignments in for loops - ID: 2801701
Last Update: Comment added ( daleking )

add a property to allow innerAssignments in for loops.

for example something like this:

for (String s : coll = (coll != null) ? coll : new ArrayList<String>())
{
//do something
}


Johannes Grahammer ( jgrahammer ) - 2009-06-05 12:24

5

Open

None

Nobody/Anonymous

Check

None

Public


Comment ( 1 )

Date: 2009-06-08 03:11
Sender: daleking

If you read the book Code Complete you might have seen how he will show
example code with a big indication in the margin that this is a "Coding
Horror". That is how I would label the example here.

I swear it took me about 30 seconds to try to figure out what that code
was supposed to be doing. I think because of the size of the for statement
I kept trying to parse it as a regular for statement.

So the question is, in this example does the code after the loop depend on
the fact that coll has been changed from null to an empty list by the for
statement? If so then you DEFINITELY should not be burying that inside the
for loop. I think the following would be much cleaner:

if( coll == null )
{
coll = new ArrayList<String>();
}

for( String s : coll )
{
//do something
}

If you do not need coll to be changed from null and are only doing it so
that you don't execute the loop, then it makes a lot more sense to just do:

if( coll != null )
{
for( String s : coll )
{
//do something
}
}




Attached File

No Files Currently Attached

Change

No changes have been made to this artifact.