# 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:

Drag to your running Eclipse* workspace. *Requires Eclipse Marketplace Client

Need help? Check out our installation guide.

# Properties

Property Value
Rule ID RemoveNullCheckBeforeInstanceof
First seen in jSparrow version 3.8.0
Minimum Java version 1.1
Remediation cost 5 min
Links