Menu

#416 NullAssignment isn't flexible enough

open
nobody
None
5
2012-10-07
2006-01-24
No

consider the following code:

final Object x;

if(someCondition)
{
x = null;
}
else
{
x = new Object();
}

The above code does not match the intent of the rule
(at least what I think the intent is) of overwriting a
value with null.

Also consider:

Object x;

x = null;

for(...)
{
if(someCondition)
{
x = something;
break;
}
}

Some people (ok me) don't like to write Object x = null.

I think the rule should be augmented to be something like:

Assigning null to an object is fine if there have been
no prior assignments.

Thanks,

..darcy

Discussion

  • D'Arcy Smith

    D'Arcy Smith - 2006-01-25

    Logged In: YES
    user_id=554117

    Looks like 3.5 fixes the first case above but not the second
    case... my number of PMD warnings just went down :-)

     
  • Tom Copeland

    Tom Copeland - 2007-10-25

    Logged In: YES
    user_id=5159
    Originator: NO

    Yup, we no longer flag this if it's an assignment to a final variable. I've put a note in that rule about this second case...

    Yours,

    tom
    http://pmdapplied.com/

     

Log in to post a comment.