Here is a more complete list of suggested static code checks. The list may at times seem silly or obvious but I can assure one that all of these have or currently exist in the code base.
I have included all those from the original top ten list even if they are currently under discussion and review.
In no particular order here is more complete list of static code checks and suggested changes:
-
Unnecessary ânullâ check before âinstanceofâ expression
Any null check followed by an instanceof check can be removed. Since the instanceof operator always returns false for null, there is no need to have an additional null check.
-
Unnecessary âreturnâ statement
Remove any unnecessary return statements at the end of constructors and methods returning voidâŚ
-
Unused declaration
Remove methods or fields in the current scope that are not used or not reachable from entry points.
-
Variable is assigned to itself
Find assignments of a variable to itself. This usually represents a typo or cut-and-paste error.
-
ââ imports
Replace any import statements which cover entire packages (â importsâ) with individual import statements.
-
Import from the same package
Remove any import statements which refer to the same package as the containing file. Such imports are unnecessary.
-
âjava.langâ import
Remove any import statements which refer to the java.lang package. Such import statements are unnecessary.
-
Unused import
Remove any import statements that are unused.
-
Missing @Override annotation
Add missing @Override to any methods which override methods in a superclass but do not have the @java.lang.Override annotation.
-
Method is identical to its super method
Find any method with a signature identical to its super method and either has an identical body to the super method or only calls the super method. Such a method is redundant and can be removed.
-
Unnecessary boxing
Find âboxingâ, e.g. wrapping of primitive values in objects. Boxing is unnecessary under Java 5 and newer, and can be safely removed.
-
Unnecessary unboxing
Find âunboxingâ, e.g. explicit unwrapping of wrapped primitive values. Unboxing is unnecessary under Java 5 and newer, and can be safely removed.
-
Non-constant string concatenation as argument to logging call
Find non-constant string concatenations used as arguments to logging methods. Non-constant concatenations will be evaluated at runtime even when the logging message is not logged; this can negatively impact performance. Replace with a parameterized log message.
-
Number of placeholders does not match number of arguments in logging call
Find logging calls where the number of {}-placeholders in the string literal argument does not match the number of other arguments to the logging call. Correct as needed.
-
Constant naming convention
Correct any constants whose names do not follow the regular expression pattern: [A-Z][A-Z_\d]*. Static LOGGER constants are an example.
-
Enumerated class naming convention
Correct any enumerated classes whose names do not follow the specified regular expression pattern: [A-Z][A-Za-z\d]*.
-
Enumerated constant naming convention
Correct any enumerated constant whose names do not follow the regular expression pattern: [A-Z][A-Z_\d]*.
-
Non-constant field with upper-case name
Correct any non-static non-final fields whose names are all upper-case. Such fields cause confusion by breaking a common naming convention, and are often the result of developer error.
-
Call to âArrays.asList()â with too few arguments
Replace any calls to Arrays.asList() with zero arguments or only one argument with either a call to Collections.singletonList() or Collections.emptyList() which will save some memory.
-
Concatenation with empty string
Replace string concatenations where one of the arguments is the empty string. Such a concatenation is unnecessary and inefficient, particularly when used as an idiom for formatting non-String objects or primitives into Strings. Use String.valueOf() instead.
-
Manual array copy
Replace any manual copying of array contents with calls to System.arraycopy().
-
Manual array to collection copy
Replace any copying of array contents to a collection where each element is added individually using a for loop with a call to Collection.addAll(Arrays.asList()) or Collections.addAll().
-
Redundant âString.toString()â
Remove any to call toString() on a String object. This is entirely redundant.
-
Redundant âsubstring(0)â call
Remove any call to String.substring() with a constant argument equal to zero. Such calls are completely redundant, and may be removed.
-
Redundant call to âString.format()â
Remove any calls to String.format() where only a format string is provided, but no arguments. Such a call is unnecessary and can be replaced with just the string.
-
Single character string argument in âString.indexOf()â call
Find String literals of length one being used as a parameter in String.indexOf() or String.lastIndexOf() calls. These String literals may be replaced by equivalent character literals, gaining some performance enhancement.
-
Single character string concatenation
Find String literals of length one being used in concatenation. These literals may be replaced by equivalent character literals, gaining some performance enhancement.
-
String concatenation as arguments to âStringBuffer.append()â call
Find String concatenation used as the argument to StringBuffer.append(), StringBuilder.append() or Appendable.append(). Such calls may profitably be turned into chained append calls on the existing StringBuffer/Builder/Appendable, saving the cost of an extra StringBuffer/Builder
allocation.
-
âString.equals(ââ)â
Replace .equals() being called to compare a String with an empty string with comparing the length to zero as it is normally more efficient.
-
âStringBuffer.toString()â in concatenation
Remove StringBuffer.toString() or StringBuilder.toString() in String concatenations. In addition to being confusing, this code performs String allocation and copying, which is unnecessary.
-
Array comparison using â==â instead of âArrays.equals()â
Replace any use of == or != to test for array equality, with the java.util.Arrays.equals() method.
-
String comparison using â==â, instead of âequals()â
Replace any use of == or != to test for String equality with the equals() method.
-
Redundant type cast
Remove unnecessary cast expressions.
-
Mismatched read and write of array
Find and correct as needed any array fields or variables whose contents are read but not written, or written but not read. Such mismatched reads and writes are pointless, dead or erroneous code.
-
Explicit type can be replaced with <>
Correct all new expressions with type arguments which can be replaced with diamond type <>. That is no need to specify the type again.
-
Misordered âassertEquals()â arguments
Fix any calls to JUnit assertEquals() which have a non-literal as the expected result argument and a literal as the actual result argument. Such calls will behave fine for assertions which pass, but may give confusing error reports if their expected and actual arguments differ.
-
JUnit 4 test method in class extending JUnit 3 TestCase
Find and correct JUnit 4 @Test annotated methods which are located inside a class extending the abstract JUnit 3 class TestCase. Mixing JUnit APIâs like this is confusing and can lead to problems running the tests, for example a method annotated with @Ignore wonât be actually ignored if its name starts with test.
-
Simplifiable JUnit assertions
Fix any JUnit assert calls which can be replaced by simpler but equivalent calls; examples include: