Showing 24 of 24 total issues
Parameters to get have the same names but not the same order as the method arguments. Open
return get(host, pumlIndex, repo, path, commit);
- Read upRead up
- Exclude checks
When the names of parameters in a method call match the names of the method arguments, it contributes to clearer, more readable code. However, when the names match, but are passed in a different order than the method arguments, it indicates a mistake in the parameter order which will likely lead to unexpected results.
Noncompliant Code Example
public double divide(int divisor, int dividend) { return divisor/dividend; } public void doTheThing() { int divisor = 15; int dividend = 5; double result = divide(dividend, divisor); // Noncompliant; operation succeeds, but result is unexpected //... }
Compliant Solution
public double divide(int divisor, int dividend) { return divisor/dividend; } public void doTheThing() { int divisor = 15; int dividend = 5; double result = divide(divisor, dividend); //... }
Add a nested comment explaining why this method is empty, throw an UnsupportedOperationException or complete the implementation. Open
public void run(PumlConfiguration configuration, Environment environment) {}
- Read upRead up
- Exclude checks
There are several reasons for a method not to have a method body:
- It is an unintentional omission, and should be fixed to prevent an unexpected behavior in production.
- It is not yet, or never will be, supported. In this case an
UnsupportedOperationException
should be thrown. - The method is an intentionally-blank override. In this case a nested comment should explain the reason for the blank override.
Noncompliant Code Example
public void doSomething() { } public void doSomethingElse() { }
Compliant Solution
@Override public void doSomething() { // Do nothing because of X and Y. } @Override public void doSomethingElse() { throw new UnsupportedOperationException(); }
Exceptions
Default (no-argument) constructors are ignored when there are other constructors in the class, as are empty methods in abstract classes.
public abstract class Animal { void speak() { // default implementation ignored } }
Parameters to get have the same names but not the same order as the method arguments. Open
return get(host, pumlIndex, owner, repo, path, commit);
- Read upRead up
- Exclude checks
When the names of parameters in a method call match the names of the method arguments, it contributes to clearer, more readable code. However, when the names match, but are passed in a different order than the method arguments, it indicates a mistake in the parameter order which will likely lead to unexpected results.
Noncompliant Code Example
public double divide(int divisor, int dividend) { return divisor/dividend; } public void doTheThing() { int divisor = 15; int dividend = 5; double result = divide(dividend, divisor); // Noncompliant; operation succeeds, but result is unexpected //... }
Compliant Solution
public double divide(int divisor, int dividend) { return divisor/dividend; } public void doTheThing() { int divisor = 15; int dividend = 5; double result = divide(divisor, dividend); //... }
Replace this call to "replaceAll()" by a call to the "replace()" method. Open
return new String(Base64.getDecoder().decode(encoded.replaceAll("\n", "")));
- Read upRead up
- Exclude checks
The underlying implementation of String::replaceAll
calls the java.util.regex.Pattern.compile()
method each time it is
called even if the first argument is not a regular expression. This has a significant performance cost and therefore should be used with care.
When String::replaceAll
is used, the first argument should be a real regular expression. If it’s not the case,
String::replace
does exactly the same thing as String::replaceAll
without the performance drawback of the regex.
This rule raises an issue for each String::replaceAll
used with a String
as first parameter which doesn’t contains
special regex character or pattern.
Noncompliant Code Example
String init = "Bob is a Bird... Bob is a Plane... Bob is Superman!"; String changed = init.replaceAll("Bob is", "It's"); // Noncompliant changed = changed.replaceAll("\\.\\.\\.", ";"); // Noncompliant
Compliant Solution
String init = "Bob is a Bird... Bob is a Plane... Bob is Superman!"; String changed = init.replace("Bob is", "It's"); changed = changed.replace("...", ";");
Or, with a regex:
String init = "Bob is a Bird... Bob is a Plane... Bob is Superman!"; String changed = init.replaceAll("\\w*\\sis", "It's"); changed = changed.replaceAll("\\.{3}", ";");
See
- {rule:java:S4248} - Regex patterns should not be created needlessly