Adjust InefficientEmptyStringCheck documentation

InefficientEmptyStringCheckRule.java and the corresponding section
both contain information which the other doesn't. The class Javadoc and
the description section in performance.xml has thus been adjusted.
This commit is contained in:
Karl-Philipp Richter
2018-05-21 22:59:41 +02:00
parent c417d68191
commit 16ad6904bd
2 changed files with 35 additions and 13 deletions

View File

@ -10,20 +10,22 @@ import net.sourceforge.pmd.lang.java.rule.AbstractInefficientZeroCheck;
import net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence;
/**
* This rule finds code which inefficiently determines empty strings. This code
* This rule finds code which inefficiently determines empty strings.
*
* <p>
* <pre>
* str.trim().length()==0
* </pre>
*
* <p>
* is quite inefficient as trim() causes a new String to be created. Smarter
* code to check for an empty string would be:
* </p>
* or
* <pre>
* str.trim().isEmpty()
* </pre>
* (for the same reason) is quite inefficient as trim() causes a new String to
* be created. A Smarter code to check for an empty string would be:
*
* <pre>
* private boolean checkTrimEmpty(String str) {
* for(int i=0; i<str.length(); i++) {
* for(int i = 0; i < str.length(); i++) {
* if(!Character.isWhitespace(str.charAt(i))) {
* return false;
* }
@ -31,7 +33,11 @@ import net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence;
* return true;
* }
* </pre>
* or Apache commons-lang's <code>StringUtils.isBlank</code>.
* or you can refer to Apache's <code>StringUtils#isBlank</code>
* (in commons-lang), Spring's <code>StringUtils#hasText</code> (in the Spring
* framework) or Google's <code>CharMatcher#whitespace</code> (in Guava) for
* existing implementations (some might include the check for != null).
* </p>
*
* @author acaplan
*/

View File

@ -366,18 +366,34 @@ buf.append("1m"); // good
class="net.sourceforge.pmd.lang.java.rule.performance.InefficientEmptyStringCheckRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_performance.html#inefficientemptystringcheck">
<description>
String.trim().length() is an inefficient way to check if a String is really empty, as it
<![CDATA[
String.trim().length() == 0 (or String.trim().isEmpty() for the same reason) is an
inefficient way to check if a String is really empty, as it
creates a new String object just to check its size. Consider creating a static function that
loops through a string, checking Character.isWhitespace() on each character and returning
false if a non-whitespace character is found. You can refer to Apache's StringUtils#isBlank (in commons-lang),
Spring's StringUtils#hasText (in the Spring framework) or Google's CharMatcher#whitespace (in Guava) for
existing implementations.
false if a non-whitespace character is found. A Smarter code to check for an empty string
would be:
private boolean checkTrimEmpty(String str) {
for(int i = 0; i < str.length(); i++) {
if(!Character.isWhitespace(str.charAt(i))) {
return false;
}
}
return true;
}
You can refer to Apache's StringUtils#isBlank (in commons-lang),
Spring's StringUtils#hasText (in the Spring framework) or Google's
CharMatcher#whitespace (in Guava) for existing implementations (some might
include the check for != null).
]]>
</description>
<priority>3</priority>
<example>
<![CDATA[
public void bar(String string) {
if (string != null && string.trim().size() > 0) {
if (string != null && string.trim().length() > 0) {
doSomething();
}
}