# Remove Null-Checks Before Instanceof
# Description
Finds and removes null-checks before occurrences of instanceof
. Since null
is not an instance of anything, the null-check is redundant.
# Benefits
Improves readability by removing redundant code.
# Tags
# Code Changes
# Null-check in conjunction with instanceof
Pre
boolean isUser = x != null && x instanceof User;
Post
boolean isUser = x instanceof User;
# Null-check in disjunction with instanceof
Pre
boolean isNotUser = y == null || !(y instanceof User);
Post
boolean isNotUser = !(y instanceof User);
# Conjunction in if statement
Pre
if(x != null && x instanceof User) {
repository.save((User)x);
}
Post
if(x instanceof User) {
repository.save((User)x);
}
Use a Java Refactoring Tool
Automate this Refactoring system-wide
You can apply this refactoring for free with the jSparrow Eclipse IDE plug-in.
Install the plug-in for Eclipse IDE: Eclipse Marketplace.