All Downloads are FREE. Search and download functionalities are using the official Maven repository.

resources.report.rules.pmd.String_and_StringBuffer_Rules.html Maven / Gradle / Ivy

Go to download

Sanity4J was created to simplify running multiple static code analysis tools on the Java projects. It provides a single entry point to run all the selected tools and produce a consolidated report, which presents all findings in an easily accessible manner.

There is a newer version: 1.8.2
Show newest version


String_and_StringBuffer_Rules


String_and_StringBuffer_Rules

ing_and_StringBuffer_Rules">

String and StringBuffer Rules

  • AvoidDuplicateLiterals: Code containing duplicate String literals can usually be improved by declaring the String as a constant field.
  • StringInstantiation: Avoid instantiating String objects; this is usually unnecessary.
  • StringToString: Avoid calling toString() on String objects; this is unnecessary.
  • InefficientStringBuffering: Avoid concatenating non literals in a StringBuffer constructor or append().
  • UnnecessaryCaseChange: Using equalsIgnoreCase() is faster than using toUpperCase/toLowerCase().equals()
  • UseStringBufferLength: Use StringBuffer.length() to determine StringBuffer length rather than using StringBuffer.toString().equals("") or StringBuffer.toString().length() ==.
  • AppendCharacterWithChar: Avoid concatenating characters as strings in StringBuffer.append.
  • ConsecutiveLiteralAppends: Consecutively calling StringBuffer.append with String literals
  • UseIndexOfChar: Use String.indexOf(char) when checking for the index of a single character; it executes faster.
  • InefficientEmptyStringCheck: 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.
  • InsufficientStringBufferDeclaration: Failing to pre-size a StringBuffer properly could cause it to re-size many times during runtime. This rule checks the characters that are actually passed into StringBuffer.append(), but represents a best guess "worst case" scenario. An empty StringBuffer constructor initializes the object to 16 characters. This default is assumed if the length of the constructor can not be determined.
  • UselessStringValueOf: No need to call String.valueOf to append to a string; just use the valueOf() argument directly.
  • StringBufferInstantiationWithChar: StringBuffer sb = new StringBuffer('c'); The char will be converted into int to intialize StringBuffer size.
  • UseEqualsToCompareStrings: Using '==' or '!=' to compare strings only works if intern version is used on both sides
  • AvoidStringBufferField: [ StringBuffers can grow quite a lot, and so may become a source of memory leak (if the owning class has a long life time).




  • © 2015 - 2024 Weber Informatics LLC | Privacy Policy