# Replace Equality Check with isEmpty()
# Description
This rule replaces comparisons of length()
or size()
with 0
with a call to isEmpty()
. Any occurrences of such a comparison will be replaced, regardless of surrounding control structure.
# Benefits
The time complexity of any isEmpty()
method implementation should be O(1)
whereas some implementations of size()
can be O(n)
. Thus, using this rule provides performance benefits.
# Tags
# Code Changes
# size, length
Pre
Map<String, String> m = new HashMap<>();
if(m.size() == 0);
String s = "";
if(s.length() == 0);
Post
Map<String, String> m = new HashMap<>();
if(m.isEmpty());
String s = "";
if(s.isEmpty());
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.