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

a drawn cute bird pointing at a graph that shows positive results

# Properties

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