From 72f5539a8bf7cdc723a943f851f62fa06df4867d Mon Sep 17 00:00:00 2001 From: "Travis CI (pmd-bot)" Date: Tue, 22 May 2018 04:58:32 +0000 Subject: [PATCH] Update documentation --- docs/pages/pmd/rules/java.md | 2 +- docs/pages/pmd/rules/java/performance.md | 29 ++++++++++++++++++------ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/docs/pages/pmd/rules/java.md b/docs/pages/pmd/rules/java.md index 64f3cead75..4332fa56fa 100644 --- a/docs/pages/pmd/rules/java.md +++ b/docs/pages/pmd/rules/java.md @@ -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(... diff --git a/docs/pages/pmd/rules/java/performance.md b/docs/pages/pmd/rules/java/performance.md index 5127460a75..130f50f09b 100644 --- a/docs/pages/pmd/rules/java/performance.md +++ b/docs/pages/pmd/rules/java/performance.md @@ -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(); } }