Why is this an issue?
Using Collection.size()
to test for emptiness works, but using Collection.isEmpty()
makes the code more readable and can
be more performant. The time complexity of any isEmpty()
method implementation should be O(1)
whereas some implementations
of size()
can be O(n)
.
Noncompliant code example
if (myCollection.size() == 0) { // Noncompliant
/* ... */
}
Compliant solution
if (myCollection.isEmpty()) {
/* ... */
}