Showing 651 of 651 total issues
When doing a String.toLowerCase()/toUpperCase() call, use a Locale Open
nodeName = tokens[1].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 'macAddress' could be made final; it is only initialized in the declaration or constructor. Open
private String macAddress;
- 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;
}
}
Position literals first in String comparisons for EqualsIgnoreCase Open
} else if (mode.equalsIgnoreCase("hex4")) {
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
PositionLiteralsFirstInCaseInsensitiveComparisons
Since: PMD 5.1
Priority: Medium
Categories: Style
Remediation Points: 50000
Position literals first in comparisons, if the second argument is null then NullPointerExceptions can be avoided, they will just return false.
Example:
class Foo {
boolean bar(String x) {
return x.equalsIgnoreCase('2'); // should be '2'.equalsIgnoreCase(x)
}
}
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();'
}
}
Fields should be declared at the top of the class, before any method declarations, constructors, initializers or inner classes. Open
private static final long MEGABYTE = 1024L * 1024L;
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
FieldDeclarationsShouldBeAtStartOfClass
Since: PMD 5.0
Priority: Medium
Categories: Style
Remediation Points: 50000
Fields should be declared at the top of the class, before any method declarations, constructors, initializers or inner classes.
Example:
public class HelloWorldBean {
// Field declared before methods / inner classes - OK
private String _thing;
public String getMessage() {
return 'Hello World!';
}
// Field declared after methods / inner classes - avoid this
private String _fieldInWrongLocation;
}
When doing a String.toLowerCase()/toUpperCase() call, use a Locale Open
protocolGraph.put(nodeName.toUpperCase(), toNodes);
- 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);
}
Avoid reassigning parameters such as 'linePtr' Open
private int collectNodes(Session session, int linePtr) {
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
AvoidReassigningParameters
Since: PMD 1.0
Priority: Medium High
Categories: Style
Remediation Points: 50000
Reassigning values to incoming parameters is not recommended. Use temporary local variables instead.
Example:
public class Foo {
private void foo(String bar) {
bar = 'something else';
}
}
Position literals first in String comparisons for EqualsIgnoreCase Open
} else if (mode.equalsIgnoreCase("hex2")) {
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
PositionLiteralsFirstInCaseInsensitiveComparisons
Since: PMD 5.1
Priority: Medium
Categories: Style
Remediation Points: 50000
Position literals first in comparisons, if the second argument is null then NullPointerExceptions can be avoided, they will just return false.
Example:
class Foo {
boolean bar(String x) {
return x.equalsIgnoreCase('2'); // should be '2'.equalsIgnoreCase(x)
}
}
These nested if statements could be combined Open
if (!inputQueue.isEmpty()) {
isProcessing = true;
process(inputQueue.poll());
}
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
CollapsibleIfStatements
Since: PMD 3.1
Priority: Medium
Categories: Style
Remediation Points: 50000
Sometimes two consecutive 'if' statements can be consolidated by separating their conditions with a boolean short-circuit operator.
Example:
void bar() {
if (x) { // original implementation
if (y) {
// do stuff
}
}
}
void bar() {
if (x && y) { // optimized implementation
// do stuff
}
}
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 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.getPassword()
.equals(Security.createHash(loginInfo.getPassword()))) {
response.put("status", "failure");
} else {
response.put("status", "success");
- 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;
}
Avoid if (x != y) ..; else ..; Open
if (bytes.length != 4) {
return "INVALID-ADDRESS";
} else {
String[] address = new String[4];
for (int i = 0; i < bytes.length; i++) {
- 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;
}
Private field 'eventBusIds' could be made final; it is only initialized in the declaration or constructor. Open
private HashSet<String> eventBusIds = new HashSet<String>();
- 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();'
}
}
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();'
}
}