Method beautify
has a Cognitive Complexity of 15 (exceeds 5 allowed). Consider refactoring. Open
public static String beautify(byte[] bytes, String mode)
throws IllegalArgumentException {
if (mode.equalsIgnoreCase("ip4")) {
if (bytes.length != 4) {
- 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
Method beautify
has 35 lines of code (exceeds 25 allowed). Consider refactoring. Open
public static String beautify(byte[] bytes, String mode)
throws IllegalArgumentException {
if (mode.equalsIgnoreCase("ip4")) {
if (bytes.length != 4) {
- Create a ticketCreate a ticket
Avoid too many return
statements within this method. Open
return "INVALID-ADDRESS";
- Create a ticketCreate a ticket
Add a private constructor to hide the implicit public one. Open
public class Beautify {
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Utility classes, which are collections of static
members, are not meant to be instantiated. Even abstract utility classes, which can
be extended, should not have public constructors.
Java adds an implicit public constructor to every class which does not define at least one explicitly. Hence, at least one non-public constructor should be defined.
Noncompliant Code Example
class StringUtils { // Noncompliant public static String concatenate(String s1, String s2) { return s1 + s2; } }
Compliant Solution
class StringUtils { // Compliant private StringUtils() { throw new IllegalStateException("Utility class"); } public static String concatenate(String s1, String s2) { return s1 + s2; } }
Exceptions
When class contains public static void main(String[] args)
method it is not considered as utility class and will be ignored by this
rule.
Similar blocks of code found in 2 locations. Consider refactoring. Open
} else if (mode.equalsIgnoreCase("hex4")) {
String hexString = Hex.encodeHexString(bytes);
StringBuilder builder = new StringBuilder();
for (int i = 0; i < hexString.length(); i += 4) {
builder.append(hexString.substring(i, i + 4));
- Read upRead up
- Create a ticketCreate a ticket
Duplicated Code
Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:
Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.
When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).
Tuning
This issue has a mass of 92.
We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.
The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.
If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.
See codeclimate-duplication
's documentation for more information about tuning the mass threshold in your .codeclimate.yml
.
Refactorings
- Extract Method
- Extract Class
- Form Template Method
- Introduce Null Object
- Pull Up Method
- Pull Up Field
- Substitute Algorithm
Further Reading
- Don't Repeat Yourself on the C2 Wiki
- Duplicated Code on SourceMaking
- Refactoring: Improving the Design of Existing Code by Martin Fowler. Duplicated Code, p76
Similar blocks of code found in 2 locations. Consider refactoring. Open
} else if (mode.equalsIgnoreCase("hex2")) {
String hexString = Hex.encodeHexString(bytes);
StringBuilder builder = new StringBuilder();
for (int i = 0; i < hexString.length(); i += 2) {
builder.append(hexString.substring(i, i + 2));
- Read upRead up
- Create a ticketCreate a ticket
Duplicated Code
Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:
Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.
When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).
Tuning
This issue has a mass of 92.
We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.
The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.
If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.
See codeclimate-duplication
's documentation for more information about tuning the mass threshold in your .codeclimate.yml
.
Refactorings
- Extract Method
- Extract Class
- Form Template Method
- Introduce Null Object
- Pull Up Method
- Pull Up Field
- Substitute Algorithm
Further Reading
- Don't Repeat Yourself on the C2 Wiki
- Duplicated Code on SourceMaking
- Refactoring: Improving the Design of Existing Code by Martin Fowler. Duplicated Code, p76
Identical blocks of code found in 2 locations. Consider refactoring. Open
return address[0] + "." + address[1] + "." + address[2] + "."
+ address[3];
- Read upRead up
- Create a ticketCreate a ticket
Duplicated Code
Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:
Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.
When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).
Tuning
This issue has a mass of 43.
We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.
The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.
If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.
See codeclimate-duplication
's documentation for more information about tuning the mass threshold in your .codeclimate.yml
.
Refactorings
- Extract Method
- Extract Class
- Form Template Method
- Introduce Null Object
- Pull Up Method
- Pull Up Field
- Substitute Algorithm
Further Reading
- Don't Repeat Yourself on the C2 Wiki
- Duplicated Code on SourceMaking
- Refactoring: Improving the Design of Existing Code by Martin Fowler. Duplicated Code, p76
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)
}
}
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;
}
Position literals first in String comparisons for EqualsIgnoreCase Open
if (mode.equalsIgnoreCase("ip4")) {
- 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)
}
}
Position literals first in String comparisons for EqualsIgnoreCase Open
} else if (mode.equalsIgnoreCase("hex")) {
- 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)
}
}
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)
}
}
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 Beautify {
public static String beautify(byte[] bytes, String mode)
throws IllegalArgumentException {
- 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() {}
}