You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I think NullAssignment should be deprecated. Its justification is really bad:
Sometimes, this type of assignment is an indication that the programmer doesn't completely understand what is going on in the code.
This is completely unverifiable. The message is even worse:
Assigning an Object to null is a code smell. Consider refactoring.
null is not more dangerous when assigned into a variable than in other places, so the rule really has no reason to exist.
Using data flow analysis we should try to detect possible NPEs in a useful manner, not just blindly flag all null literals in the program. I think this kind of very noisy and badly justified rules make PMD look bad and they should be hunted down.
I'm neither for nor against deprecating this rule. I agree with your points (bad justification, too simple implementation by just looking at null literals).
I want to throw in this however: Maybe the initial reason (it's from PMD 1.02!!) for this rule is long gone, but it reminds me of the Null Object Pattern. Maybe the rule should suggest to use Null Objects instead of null? At least, the relationship of this rule and the Null object pattern should be considered in more detail before making a final decision.
I think we can't implement a strategy to recommend the null object pattern that wouldn't be wrong most of the time... The best solution we can write would boil down to flagging every null literal and say "maybe this should be be a null object instead?". I think the problem is that PMD can't be in the head of the programmer and recommend design decisions with so little data as a null assignment. My stance is that we shouldn't even try to make such recommendations, but accept that they're out of scope and concentrate our work on more useful rules.
Maybe another part of the reason this rule exists is to avoid manually cleaning up variables by setting them to null at the end of a function, for instance. This is mostly caught by UnusedAssignment now.
Maybe restrict the rule to types that provide a clear default value (collections, maps, arrays)? It's stil valid to have APIs that make distinction between null and empty list, but the same reasoning that's used for ReturnEmptyCollectionRatherThanNull could apply here..
Activity
adangel commentedon Jan 8, 2022
I'm neither for nor against deprecating this rule. I agree with your points (bad justification, too simple implementation by just looking at null literals).
I want to throw in this however: Maybe the initial reason (it's from PMD 1.02!!) for this rule is long gone, but it reminds me of the Null Object Pattern. Maybe the rule should suggest to use Null Objects instead of null? At least, the relationship of this rule and the Null object pattern should be considered in more detail before making a final decision.
oowekyala commentedon Jan 10, 2022
I think we can't implement a strategy to recommend the null object pattern that wouldn't be wrong most of the time... The best solution we can write would boil down to flagging every null literal and say "maybe this should be be a null object instead?". I think the problem is that PMD can't be in the head of the programmer and recommend design decisions with so little data as a null assignment. My stance is that we shouldn't even try to make such recommendations, but accept that they're out of scope and concentrate our work on more useful rules.
Maybe another part of the reason this rule exists is to avoid manually cleaning up variables by setting them to
nullat the end of a function, for instance. This is mostly caught by UnusedAssignment now.zbynek commentedon May 16, 2026
Maybe restrict the rule to types that provide a clear default value (collections, maps, arrays)? It's stil valid to have APIs that make distinction between
nulland empty list, but the same reasoning that's used forReturnEmptyCollectionRatherThanNullcould apply here..