Thursday, March 26, 2015

Sonar tip for code quality

Collection.isEmpty() should be used to test for emptiness

Using Collection.size() to test for emptiness works, but using Collection.isEmpty() makes the code more readable.
The following code:
if (myCollection.size() == 0) {  // Non-Compliant
  /* ... */
}
should be refactored into:
if (myCollection.isEmpty()) {    // Compliant
  /* ... */
}

No comments:

Post a Comment

java - fill distinct objects in ArrayList

If you have a list that contains some objects that it is considered as duplication when two or three fields of these objects are equal. How ...