# Replace equals() on Enum Constants
# Description
Replaces occurrences of equals()
on Enum constants with an identity comparison (==
).
In the case the equals relation is wrapped with an boolean negation the result will be an not equals (!=
).
# Benefits
Technically, equals and ==
are the same. ==
however is null safe and thus can help avoid programming errors.
# Tags
# Code Changes
# Enum in right-hand-side
Pre
public boolean isRoundUp(RoundingMode roundingMode) {
return roundingMode.equals(RoundingMode.UP);
}
Post
public boolean isRoundUp(RoundingMode roundingMode) {
return roundingMode == RoundingMode.UP;
}
# Enum in left-hand-side
Pre
public boolean isRoundUp(RoundingMode roundingMode) {
return RoundingMode.UP.equals(roundingMode);
}
Post
public boolean isRoundUp(RoundingMode roundingMode) {
return RoundingMode.UP == roundingMode;
}
# Enum in infix expression
Pre
public boolean isRoundUp(RoundingMode roundingMode) {
return !RoundingMode.UP.equals(roundingMode);
}
Post
public boolean isRoundUp(RoundingMode roundingMode) {
return !(RoundingMode.UP == roundingMode);
}
🛠️ 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.