# 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);
}
🛠️ Auto-refactor Available
You can auto-refactor this with jSparrow.
Drop this button to your Eclipse IDE workspace to install jSparrow for free:
Need help? Check out our installation guide.