Improve implementation hint in InefficientEmptyStringCheck

The alternative implementation mentions iterating over the string and
checking for whitespace which is pretty clear, however, showing the
implementation doesn't hurt, nor does the hint for Apache
commons-lang's StringUtils.isBlank which does exactly that.

The if statement in the demonstration of inefficient code can lead to
confusion because users _might_ think that the check is only
inefficient inside an if statement although it's clear from the
description that it's not.
This commit is contained in:
Karl-Philipp Richter
2018-05-21 06:10:07 +02:00
parent 1d3906356d
commit 1f18181ef5

View File

@ -13,7 +13,7 @@ import net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence;
* This rule finds code which inefficiently determines empty strings. This code
*
* <pre>
* if(str.trim().length()==0){....
* str.trim().length()==0
* </pre>
*
* <p>
@ -22,8 +22,16 @@ import net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence;
* </p>
*
* <pre>
* Character.isWhitespace(str.charAt(i));
* private boolean checkTrimEmpty(String str) {
* for(int i=0; i<str.length(); i++) {
* if(!Character.isWhitespace(str.charAt(i))) {
* return false;
* }
* }
* return true;
* }
* </pre>
* or Apache commons-lang's <code>StringUtils.isBlank</code>.
*
* @author acaplan
*/