Showing 651 of 651 total issues
Consider simply returning the value vs storing it in local variable 'returnVar' Open
return returnVar;
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
UnnecessaryLocalBeforeReturn
Since: PMD 3.3
Priority: Medium
Categories: Style
Remediation Points: 50000
Avoid the creation of unnecessary local variables
Example:
public class Foo {
public int foo() {
int x = doSomething();
return x; // instead, just 'return doSomething();'
}
}
When doing a String.toLowerCase()/toUpperCase() call, use a Locale Open
toNodes.add(protocolName.toUpperCase());
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
UseLocaleWithCaseConversions
Since: PMD 2.0
Priority: Medium
Categories: Style
Remediation Points: 50000
When doing String.toLowerCase()/toUpperCase() conversions, use Locales to avoids problems with languages that have unusual conventions, i.e. Turkish.
Example:
class Foo {
// BAD
if (x.toLowerCase().equals('list')) { }
/*
* This will not match 'LIST' when in Turkish locale
* The above could be
* if (x.toLowerCase(Locale.US).equals('list')) { }
* or simply
* if (x.equalsIgnoreCase('list')) { }
*/
// GOOD
String z = a.toLowerCase(Locale.EN);
}
Private field 'eventBuses' could be made final; it is only initialized in the declaration or constructor. Open
private HashMap<String, EventBus> eventBuses = new HashMap<String, EventBus>();
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
ImmutableField
Since: PMD 2.0
Priority: Medium
Categories: Style
Remediation Points: 50000
Identifies private fields whose values never change once they are initialized either in the declaration of the field or by a constructor. This helps in converting existing classes to becoming immutable ones.
Example:
public class Foo {
private int x; // could be final
public Foo() {
x = 7;
}
public void foo() {
int a = x + 2;
}
}
Consider simply returning the value vs storing it in local variable 'returnVar' Open
return returnVar;
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
UnnecessaryLocalBeforeReturn
Since: PMD 3.3
Priority: Medium
Categories: Style
Remediation Points: 50000
Avoid the creation of unnecessary local variables
Example:
public class Foo {
public int foo() {
int x = doSomething();
return x; // instead, just 'return doSomething();'
}
}
A switch with less than three branches is inefficient, use a if statement instead. Open
switch(nextHeaderType) {
default: return Protocol.get("END_PROTOCOL");
}
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
TooFewBranchesForASwitchStatement
Since: PMD 4.2
Priority: Medium
Categories: Style
Remediation Points: 50000
Switch statements are intended to be used to support complex branching behaviour. Using a switch for only a few cases is ill-advised, since switches are not as easy to understand as if-then statements. In these cases use the if-then statement to increase code readability.
Example:
// With a minimumNumberCaseForASwitch of 3
public class Foo {
public void bar() {
switch (condition) {
case ONE:
instruction;
break;
default:
break; // not enough for a 'switch' stmt, a simple 'if' stmt would have been more appropriate
}
}
}
Consider using varargs for methods or constructors which take an array the last parameter. Open
private void removeBlankLines(String[] lines) {
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
UseVarargs
Since: PMD 5.0
Priority: Medium Low
Categories: Style
Remediation Points: 50000
Java 5 introduced the varargs parameter declaration for methods and constructors. This syntactic sugar provides flexibility for users of these methods and constructors, allowing them to avoid having to deal with the creation of an array.
Example:
public class Foo {
public void foo(String s, Object[] args) {
// Do something here...
}
public void bar(String s, Object... args) {
// Ahh, varargs tastes much better...
}
}
Avoid if (x != y) ..; else ..; Open
if (!eventBusIds.contains(id)) {
EventBus eventBus = new EventBus(id);
eventBusIds.add(id);
eventBuses.put(id, eventBus);
return eventBus;
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
ConfusingTernary
Since: PMD 1.9
Priority: Medium
Categories: Style
Remediation Points: 50000
Avoid negation within an 'if' expression with an 'else' clause. For example, rephrase: if (x != y) diff(); else same();
as: if (x == y) same(); else diff();
. Most 'if (x != y)' cases without an 'else' are often return cases, so consistent use of this rule makes the code easier to read. Also, this resolves trivial ordering problems, such as 'does the error case go first?' or 'does the common case go first?'.
Example:
boolean bar(int x, int y) {
return (x != y) ? diff : same;
}
Consider simply returning the value vs storing it in local variable 'returnVar' Open
return returnVar;
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
UnnecessaryLocalBeforeReturn
Since: PMD 3.3
Priority: Medium
Categories: Style
Remediation Points: 50000
Avoid the creation of unnecessary local variables
Example:
public class Foo {
public int foo() {
int x = doSomething();
return x; // instead, just 'return doSomething();'
}
}
Consider simply returning the value vs storing it in local variable 'returnVar' Open
return returnVar;
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
UnnecessaryLocalBeforeReturn
Since: PMD 3.3
Priority: Medium
Categories: Style
Remediation Points: 50000
Avoid the creation of unnecessary local variables
Example:
public class Foo {
public int foo() {
int x = doSomething();
return x; // instead, just 'return doSomething();'
}
}
Consider simply returning the value vs storing it in local variable 'returnVar' Open
return returnVar;
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
UnnecessaryLocalBeforeReturn
Since: PMD 3.3
Priority: Medium
Categories: Style
Remediation Points: 50000
Avoid the creation of unnecessary local variables
Example:
public class Foo {
public int foo() {
int x = doSomething();
return x; // instead, just 'return doSomething();'
}
}
Private field 'bucketCapacity' could be made final; it is only initialized in the declaration or constructor. Open
private int bucketCapacity = 20000;
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
ImmutableField
Since: PMD 2.0
Priority: Medium
Categories: Style
Remediation Points: 50000
Identifies private fields whose values never change once they are initialized either in the declaration of the field or by a constructor. This helps in converting existing classes to becoming immutable ones.
Example:
public class Foo {
private int x; // could be final
public Foo() {
x = 7;
}
public void foo() {
int a = x + 2;
}
}
All methods are static. Consider using a utility class instead. Alternatively, you could add a private constructor or make the class abstract to silence this warning. Open
public class ByteOperator {
public static int parseBytesint(byte[] bytes)
throws ArrayIndexOutOfBoundsException {
if (bytes.length > 4) {
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
UseUtilityClass
Since: PMD 0.3
Priority: Medium
Categories: Style
Remediation Points: 50000
For classes that only have static methods, consider making them utility classes. Note that this doesn't apply to abstract classes, since their subclasses may well include non-static methods. Also, if you want this class to be a utility class, remember to add a private constructor to prevent instantiation. (Note, that this use was known before PMD 5.1.0 as UseSingleton).
Example:
public class MaybeAUtility {
public static void foo() {}
public static void bar() {}
}
Consider simply returning the value vs storing it in local variable 'returnVar' Open
return returnVar;
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
UnnecessaryLocalBeforeReturn
Since: PMD 3.3
Priority: Medium
Categories: Style
Remediation Points: 50000
Avoid the creation of unnecessary local variables
Example:
public class Foo {
public int foo() {
int x = doSomething();
return x; // instead, just 'return doSomething();'
}
}
Consider simply returning the value vs storing it in local variable 'returnVar' Open
return returnVar;
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
UnnecessaryLocalBeforeReturn
Since: PMD 3.3
Priority: Medium
Categories: Style
Remediation Points: 50000
Avoid the creation of unnecessary local variables
Example:
public class Foo {
public int foo() {
int x = doSomething();
return x; // instead, just 'return doSomething();'
}
}
Avoid if (x != y) ..; else ..; Open
if (!reverse) {
// extra bits after higher boundary
byte[] b = Arrays.copyOfRange(original, higher, higher + 1);
int val = b[0];
val = val >> (8 - extraBits);
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
ConfusingTernary
Since: PMD 1.9
Priority: Medium
Categories: Style
Remediation Points: 50000
Avoid negation within an 'if' expression with an 'else' clause. For example, rephrase: if (x != y) diff(); else same();
as: if (x == y) same(); else diff();
. Most 'if (x != y)' cases without an 'else' are often return cases, so consistent use of this rule makes the code easier to read. Also, this resolves trivial ordering problems, such as 'does the error case go first?' or 'does the common case go first?'.
Example:
boolean bar(int x, int y) {
return (x != y) ? diff : same;
}
Consider simply returning the value vs storing it in local variable 'returnVar' Open
return returnVar;
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
UnnecessaryLocalBeforeReturn
Since: PMD 3.3
Priority: Medium
Categories: Style
Remediation Points: 50000
Avoid the creation of unnecessary local variables
Example:
public class Foo {
public int foo() {
int x = doSomething();
return x; // instead, just 'return doSomething();'
}
}
Consider simply returning the value vs storing it in local variable 'returnVar' Open
return returnVar;
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
UnnecessaryLocalBeforeReturn
Since: PMD 3.3
Priority: Medium
Categories: Style
Remediation Points: 50000
Avoid the creation of unnecessary local variables
Example:
public class Foo {
public int foo() {
int x = doSomething();
return x; // instead, just 'return doSomething();'
}
}
Consider simply returning the value vs storing it in local variable 'returnVar' Open
return returnVar;
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
UnnecessaryLocalBeforeReturn
Since: PMD 3.3
Priority: Medium
Categories: Style
Remediation Points: 50000
Avoid the creation of unnecessary local variables
Example:
public class Foo {
public int foo() {
int x = doSomething();
return x; // instead, just 'return doSomething();'
}
}
Avoid if (x != y) ..; else ..; Open
} else if (!lie.getLoginHash().equals(loginHash)) {
return "failure";
} else {
log.info("Login id entity = \n" + lie.toString());
log.info("ID = " + lie.getId());
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
ConfusingTernary
Since: PMD 1.9
Priority: Medium
Categories: Style
Remediation Points: 50000
Avoid negation within an 'if' expression with an 'else' clause. For example, rephrase: if (x != y) diff(); else same();
as: if (x == y) same(); else diff();
. Most 'if (x != y)' cases without an 'else' are often return cases, so consistent use of this rule makes the code easier to read. Also, this resolves trivial ordering problems, such as 'does the error case go first?' or 'does the common case go first?'.
Example:
boolean bar(int x, int y) {
return (x != y) ? diff : same;
}
When doing a String.toLowerCase()/toUpperCase() call, use a Locale Open
protocolName = protocolName.toUpperCase();
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
UseLocaleWithCaseConversions
Since: PMD 2.0
Priority: Medium
Categories: Style
Remediation Points: 50000
When doing String.toLowerCase()/toUpperCase() conversions, use Locales to avoids problems with languages that have unusual conventions, i.e. Turkish.
Example:
class Foo {
// BAD
if (x.toLowerCase().equals('list')) { }
/*
* This will not match 'LIST' when in Turkish locale
* The above could be
* if (x.toLowerCase(Locale.US).equals('list')) { }
* or simply
* if (x.equalsIgnoreCase('list')) { }
*/
// GOOD
String z = a.toLowerCase(Locale.EN);
}