# Remove Null-Checks Before Instanceof

# Properties

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

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

# 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);
}

Automatic Application of This Rule

The automatic application of this rule is supported in the following jSparrow version:

# Tags

1
default
You & jSparrow
default

Hey there! May I help you? 😊