Method manageColor
has 34 lines of code (exceeds 25 allowed). Consider refactoring. Open
private void manageColor() {
Color fg = Color.BLACK;
Color bg = Color.WHITE;
if (object != null) {
String param = object.toString();
- Create a ticketCreate a ticket
Method manageColor
has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring. Open
private void manageColor() {
Color fg = Color.BLACK;
Color bg = Color.WHITE;
if (object != null) {
String param = object.toString();
- Read upRead up
- Create a ticketCreate a ticket
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Make "object" transient or serializable. Open
private Object object;
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Fields in a Serializable
class must themselves be either Serializable
or transient
even if the class is
never explicitly serialized or deserialized. For instance, under load, most J2EE application frameworks flush objects to disk, and an allegedly
Serializable
object with non-transient, non-serializable data members could cause program crashes, and open the door to attackers. In
general a Serializable
class is expected to fulfil its contract and not have an unexpected behaviour when an instance is serialized.
This rule raises an issue on non-Serializable
fields, and on collection fields when they are not private
(because they
could be assigned non-Serializable
values externally), and when they are assigned non-Serializable
types within the
class.
Noncompliant Code Example
public class Address { //... } public class Person implements Serializable { private static final long serialVersionUID = 1905122041950251207L; private String name; private Address address; // Noncompliant; Address isn't serializable }
Compliant Solution
public class Address implements Serializable { private static final long serialVersionUID = 2405172041950251807L; } public class Person implements Serializable { private static final long serialVersionUID = 1905122041950251207L; private String name; private Address address; }
Exceptions
The alternative to making all members serializable
or transient
is to implement special methods which take on the
responsibility of properly serializing and de-serializing the object. This rule ignores classes which implement the following methods:
private void writeObject(java.io.ObjectOutputStream out) throws IOException private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException;
See
- MITRE, CWE-594 - Saving Unserializable Objects to Disk
- Oracle Java 6, Serializable
- Oracle Java 7, Serializable
Define a constant instead of duplicating this literal "count" 3 times. Open
lParam.endsWith("[" + dbu.replace("count", "cfu") + "]") ||
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Duplicated string literals make the process of refactoring error-prone, since you must be sure to update all occurrences.
On the other hand, constants can be referenced from many places, but only need to be updated in a single place.
Noncompliant Code Example
With the default threshold of 3:
public void run() { prepare("action1"); // Noncompliant - "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 }
Compliant Solution
private static final String ACTION_1 = "action1"; // Compliant public void run() { prepare(ACTION_1); // Compliant execute(ACTION_1); release(ACTION_1); }
Exceptions
To prevent generating some false-positives, literals having less than 5 characters are excluded.
0 is a valid index, but is ignored by this check. Open
return param != null && param.endsWith("]") && param.indexOf("]") > 0;
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Most checks against an indexOf
value compare it with -1 because 0 is a valid index. Any checks which look for values >0 ignore the
first element, which is likely a bug. If the intent is merely to check inclusion of a value in a String
or a List
, consider
using the contains
method instead.
This rule raises an issue when an indexOf
value retrieved either from a String
or a List
is tested against
>0
.
Noncompliant Code Example
String color = "blue"; String name = "ishmael"; List<String> strings = new ArrayList<String> (); strings.add(color); strings.add(name); if (strings.indexOf(color) > 0) { // Noncompliant // ... } if (name.indexOf("ish") > 0) { // Noncompliant // ... } if (name.indexOf("ae") > 0) { // Noncompliant // ... }
Compliant Solution
String color = "blue"; String name = "ishmael"; List<String> strings = new ArrayList<String> (); strings.add(color); strings.add(name); if (strings.indexOf(color) > -1) { // ... } if (name.indexOf("ish") >= 0) { // ... } if (name.contains("ae") { // ... }