|
The class java.util.Arrays has a asList method that should be use when you want to create a new List from an array of objects. It is faster than executing a loop to cpy all the elements of the array one by one
Repository: pmd
Key: UseArraysAsList
Available since 07 Jan 2014
|
|
Use String.indexOf(char) when checking for the index of a single character; it executes faster.
Repository: pmd
Key: UseIndexOfChar
Available since 07 Jan 2014
|
|
Use StringBuffer.length() to determine StringBuffer length rather than using StringBuffer.toString().equals() or StringBuffer.toString().length() ==.
Repository: pmd
Key: UseStringBufferLength
Available since 07 Jan 2014
|
|
The imports part of a file should be handled by the Integrated Development Environment (IDE), not manually by the developer.
Unused and useless imports should not occur if that is the case.
Leaving them in reduces the code's readability, since their presence can be confusing.
The following code snippet illustrates this rule:
package my.company;
import java.lang.String; // Non-Compliant - java.lang classes are always implicitly imported
import my.company.SomeClass; // Non-Compliant - same package files are always implicitly imported
import java.io.File; // Non-Compliant - File is not used
import my.company2.SomeType;
import my.company2.SomeType; // Non-Compliant - 'SomeType' is already imported
class ExampleClass {
public String someString;
public SomeType something;
}
Repository: squid
Key: UselessImportCheck
Available since 07 Jan 2014
|
|
An operation on an Immutable object (BigDecimal or BigInteger) won't change the object itself. The result of the operation is a new object. Therefore, ignoring the operation result is an error.
Repository: pmd
Key: UselessOperationOnImmutable
Available since 07 Jan 2014
|
|
Useless parentheses can sometimes be misleading and so should be removed.
The following code snippet illustrates this rule:
return 3; // Compliant
return (x); // Non-Compliant
return (x + 1); // Non-Compliant
int x = (y / 2 + 1); // Non-Compliant
int y = (4+X) * y; // Compliant
Repository: squid
Key: UselessParenthesesCheck
Available since 07 Jan 2014
|
|
Utility classes, which are a collection of static members, are not meant to be instantiated.
They should therefore not have public constructors.
Java adds an implicit public constructor to every class which does not define at least one explicitly.
Hence, at least one non-public constructor should be defined.
The following code:
class StringUtils { // Non-Compliant
public static String concatenate(String s1, String s2) {
return s1 + s2;
}
}
should be refactored into:
class StringUtils { // Compliant
private StringUtils {
}
public static String concatenate(String s1, String s2) {
return s1 + s2;
}
}
Repository: squid
Key: S1118
Available since 07 Jan 2014
|