| Active/Severity | Name [expand / collapse] | Sort by: | ||
|---|---|---|---|---|
|
Most of the time a block of code is empty when a piece of code is really missing. So such empty block must be either filled or removed. When a block contains a comment, this block is not considered to be empty. The following code snippet illustrates this rule:
void doSomething() {
for (int i = 0; i < 42; i++) // Non-Compliant
{
}
for (int i = 0; i < 42; i++); // Compliant
if (myVar == 4) // Compliant - contains a comment
{
// Do nothing because of X and Y
}
else // Compliant
{
doSomething();
}
try // Non-Compliant
{
}
catch (Exception e) // Compliant
{
// Ignore
}
}
Repository: squid
Key: S00108
Available since 07 Jan 2014
|
||||
|
Nested code blocks can be used to create a new scope and restrict the visibility of the variables defined within. Using this feature in a method typically indicates that it takes on too many responsibilities, and should be refactored into smaller ones. The following code:
public void evaluate(int operator) {
switch (operator) {
/* ... */
case ADD: { // Non-Compliant - nested code block '{' ... '}'
int a = stack.pop();
int b = stack.pop();
int result = a + b;
stack.push(result);
break;
}
/* ... */
}
}
should be refactored into:
public void evaluate(int operator) {
switch (operator) {
/* ... */
case ADD: // Compliant
evaluateAdd();
break;
/* ... */
}
}
private void evaluateAdd() {
int a = stack.pop();
int b = stack.pop();
int result = a + b;
stack.push(result);
}
Repository: squid
Key: S1199
Available since 07 Jan 2014
|
||||
|
Non-static initializers are rarely used, and can be confusing for most developers. When possible, they should be refactored into standard constructors or field initializers. The following code:
class MyClass {
private static final Map
could be refactored using Guava into:
class MyClass {
// Compliant
private static final Map
Repository: squid
Key: S1171
Available since 07 Jan 2014
|
||||
|
Overloading this method is misleading:
Another name should be picked for the method. The following code:
public void finalize(int someParameter) { // Non-Compliant
/* ... */
}
should be refactored into:
public void someBetterName(int someParameter) { // Compliant
/* ... */
}
Repository: squid
Key: S1175
Available since 07 Jan 2014
|
||||
|
The contract of the The following code snippet illustrates this rule:
public class MyClass {
@Override
public void finalize() { // Non-Compliant
/* ... */
}
}
Repository: squid
Key: S1174
Available since 07 Jan 2014
|
||||
|
Deprecated
Override both public boolean Object.equals(Object other), and public int Object.hashCode(), or override neither. Even if you are inheriting a hashCode() from a parent class, consider implementing hashCode and explicitly delegating to your superclass. Example :
// this is bad
public class Bar {
public boolean equals(Object o) {
// do some comparison
}
}
// and so is this
public class Baz {
public int hashCode() {
// return some hash value
}
}
// this is OK
public class Foo {
public boolean equals(Object other) {
// do some comparison
}
public int hashCode() {
// return some hash value
}
}
This rule is deprecated, use squid:S1206 instead.
Repository: pmd
Key: OverrideBothEqualsAndHashcode
Available since 07 Jan 2014
|
||||
|
Overriding a method just to call the same method from the super class without performing any other actions is useless and misleading. The following code snippet illustrates this rule:
public void doSomething() { // Non-Compliant
super.doSomething();
}
@Override
public boolean isLegal(Action action) { // Non-Compliant
return super.isLegal(action);
}
@Override
public boolean isLegal(Action action) { // Compliant - not simply forwarding the call
return super.isLegal(new Action(/* ... */));
}
@Id
@Override
public int getId() { // Compliant - there is annotation different from @Override
return super.getId();
}
Repository: squid
Key: S1185
Available since 07 Jan 2014
|
||||
|
Sharing some naming conventions is a key point to make it possible for a team to efficiently collaborate. This rule allows to check that all package names match a provided regular expression. The following code snippet illustrates this rule when the regular expression value is "^[a-z]+(\\.[a-z][a-z0-9]*)*$": package org.Example; // Non-Compliant package org.example; // Compliant
Repository: squid
Key: S00120
Available since 07 Jan 2014
|
||||
|
Creating temporary primitive wrapper objects only for For example, the following code: new Integer(myInteger).toString(); // Non-Compliant should be refactored into: Integer.toString(myInteger); // Compliant
Repository: squid
Key: S1158
Available since 07 Jan 2014
|
||||
|
Using checked exceptions forces method callers to deal with errors, either by propagating them or by handling them. This makes those exceptions fully part of the API of the method. To keep the complexity for callers reasonable, methods should not throw more than one kind of checked exception. The following code:
public void delete() throws IOException, SQLException { // Non-Compliant
/* ... */
}
should be refactored into:
public void delete() throws SomeApplicationLevelException { // Compliant
/* ... */
}
Overriding methods are not checked by this rule and are allowed to throw several checked exceptions.
Repository: squid
Key: S1160
Available since 07 Jan 2014
|
||||
|
Return of boolean literal statements wrapped into if-then-else ones should be simplified. For example, the following code:
if (someBooleanMethod()) { // Non-Compliant
return true;
} else {
return false;
}
should be refactored into: return someBooleanMethod(); // Compliant and, the following code:
if (someBooleanMethod()) { // Non-Compliant
return false;
} else {
return true;
}
should be refactored into: return !someBooleanMethod(); // Compliant
Repository: squid
Key: S1126
Available since 07 Jan 2014
|
||||
|
Returning from a The following code snippet illustrates this rule. The developer expects to get "ERROR" in the console whereas "OK" is displayed.
public static void main(String[] args) {
try {
doSomethingWhichThrowsException();
System.out.println("OK");
} catch (RuntimeException e) {
System.out.println("ERROR");
}
}
public static void doSomethingWhichThrowsException() {
try {
throw new RuntimeException();
} finally {
/* ... */
return; // Non-Compliant - prevents the RuntimeException from being propagated
}
}
Repository: squid
Key: S1143
Available since 07 Jan 2014
|
||||
|
Right curly brace and next "else", "catch" and "finally" keywords should be located on the same line
Sharing some coding conventions is a key point to make it possible for a team to efficiently collaborate.
This rule make it mandatory to place closing curly braces on the same line that next The following code snippet illustrates this rule:
public void myMethod() {
if(something) {
executeTask();
} else if (somethingElse) { // Compliant
doSomethingElse();
}
else { // Non-Compliant
generateError();
}
try {
generateOrder();
} catch (Exception e) { // Compliant
log(e);
}
finally { // Non-Compliant
closeConnection();
}
}
Repository: squid
Key: RightCurlyBraceSameLineAsNextBlockCheck
Available since 07 Jan 2014
|
||||
|
Sharing some coding conventions is a key point to make it possible for a team to efficiently collaborate. This rule make it mandatory to place right curly braces at the beginning of lines of code. The following code snippet illustrates this rule:
public void myMethod() {
if(something) {
executeTask();} // Non-Compliant
else if (somethingElse) {
doSomethingElse();
} // Compliant
try {
generateOrder();
} finally { // Compliant
closeConnection();
} // Compliant
try {
generateOrder();
} finally { // Compliant
closeConnection(); } // Non-Compliant
}
Repository: squid
Key: RightCurlyBraceStartLineCheck
Available since 07 Jan 2014
|
||||
|
Deprecated
StringBuffer sb = new StringBuffer('c'); The char will be converted into int to intialize StringBuffer size. This rule is deprecated, use squid:S1317 instead.
Repository: pmd
Key: StringBufferInstantiationWithChar
Available since 07 Jan 2014
|
||||
|
Duplicated string literals are error-prone to refactor, as one must pay attention to update all occurrences. Constants can be referenced from many places, but there value is updated in a single place. The following code:
public void run() {
prepare("action1"); // Non-Compliant - "action1" is duplicated 3 times
execute("action1");
release("action1");
}
@SuppressWarning("all") // Compliant - annotations are excluded
private void method1(} { /* ... */ }
@SuppressWarning("all")
private void method2(} { /* ... */ }
public String method3(String a) {
System.out.println("'" + a + "'"); // Compliant - literal "'" has less than 5 characters and is excluded
return ""; // Compliant - literal "" has less than 5 characters and is excluded
}
should be refactored into:
private static final String ACTION_1 = "action1"; // Compliant
public void run() {
prepare(ACTION_1); // Compliant
execute(ACTION_1);
release(ACTION_1);
}
Repository: squid
Key: S1192
Available since 07 Jan 2014
|
||||
|
Appending The following code:
public void display(int i){
System.out.println("Output is " + String.valueOf(i)); // Non-Compliant
}
should be refactored into:
public void display(int i){
System.out.println("Output is " + i); // Compliant
}
Repository: squid
Key: S1153
Available since 07 Jan 2014
|
||||
|
It is preferable to place string literals on the left-hand side of an The following code:
String myString = null;
System.out.println("Equal? " + myString.equals("foo")); // Non-Compliant - will raise a NPE
System.out.println("Equal? " + (myString != null && myString.equals("foo"))); // Non-Compliant - null check could be removed
should be refactored into:
System.out.println("Equal?" + "foo".equals(myString)); // Compliant - properly deals with the null case
Repository: squid
Key: S1132
Available since 07 Jan 2014
|
||||
|
String literals, just like any other The following code:
if (variable == "foo") { /* ... */ } // Non-Compliant
if (variable != "foo") { /* ... */ } // Non-Compliant
should be refactored into:
if ("foo".equals(variable)) { /* ... */ } // Compliant
if (!"foo".equals(variable)) { /* ... */ } // Compliant
Repository: squid
Key: StringEqualityComparisonCheck
Available since 07 Jan 2014
|
||||