forked from phoedos/pmd
Update documentation
This commit is contained in:
@ -304,7 +304,7 @@ folder: pmd/rules
|
||||
* [ByteInstantiation](pmd_rules_java_performance.html#byteinstantiation): Calling new Byte() causes memory allocation that can be avoided by the static Byte.valueOf().It m...
|
||||
* [ConsecutiveAppendsShouldReuse](pmd_rules_java_performance.html#consecutiveappendsshouldreuse): Consecutive calls to StringBuffer/StringBuilder .append should be chained, reusing the target obj...
|
||||
* [ConsecutiveLiteralAppends](pmd_rules_java_performance.html#consecutiveliteralappends): Consecutively calling StringBuffer/StringBuilder.append(...) with literals should be avoided.Sinc...
|
||||
* [InefficientEmptyStringCheck](pmd_rules_java_performance.html#inefficientemptystringcheck): String.trim().length() is an inefficient way to check if a String is really empty, as itcreates a...
|
||||
* [InefficientEmptyStringCheck](pmd_rules_java_performance.html#inefficientemptystringcheck): String.trim().length() == 0 (or String.trim().isEmpty() for the same reason) is an inefficientway...
|
||||
* [InefficientStringBuffering](pmd_rules_java_performance.html#inefficientstringbuffering): Avoid concatenating non-literals in a StringBuffer constructor or append() since intermediate buf...
|
||||
* [InsufficientStringBufferDeclaration](pmd_rules_java_performance.html#insufficientstringbufferdeclaration): Failing to pre-size a StringBuffer or StringBuilder properly could cause it to re-size many times...
|
||||
* [IntegerInstantiation](pmd_rules_java_performance.html#integerinstantiation): Calling new Integer() causes memory allocation that can be avoided by the static Integer.valueOf(...
|
||||
|
@ -389,12 +389,27 @@ buf.append("1m"); // good
|
||||
|
||||
**Priority:** Medium (3)
|
||||
|
||||
String.trim().length() 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.
|
||||
String.trim().length() == 0 (or String.trim().isEmpty() for the same reason) is an inefficient
|
||||
way to check if a String is really blank, 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. A Smarter code to
|
||||
check for an empty string would be:
|
||||
|
||||
```java
|
||||
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).
|
||||
|
||||
**This rule is defined by the following Java class:** [net.sourceforge.pmd.lang.java.rule.performance.InefficientEmptyStringCheckRule](https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/performance/InefficientEmptyStringCheckRule.java)
|
||||
|
||||
@ -402,7 +417,7 @@ existing implementations.
|
||||
|
||||
``` java
|
||||
public void bar(String string) {
|
||||
if (string != null && string.trim().size() > 0) {
|
||||
if (string != null && string.trim().length() > 0) {
|
||||
doSomething();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user