SonarLint for Eclipse, accuses the error:
Refactor this method to reduce its Cognitive Complexity from 64 to the 15 allowed.
Rewrite this method to reduce your cognitive complexity from 64 for 15 allowed
That is, my method has many if
s and else
s, many decision points and I need to decrease it.
In a business rules method, until it is "easy", I rewrite the method by separating into several other small methods and "JUnitizables".
The problem is theequals()
method, I have some objects with multiple fields, and the equals()
method needs to check the similarity of all these fields, check null
, equals
and ==
for primitives .
This my equals()
is already a method with simple context, it only does one task, it is already testable by JUnit, but it has several decision points inside.
What to do in this case? Create multiple methods, one to test null
, one to test equals()
, one to test primitives, and join all in equals()
master? Do you have a better approach?
NOTE: I know that I can join several% s of% s on a single line, but this greatly disrupts the visualization by a human being and I am trying to avoid this practice.
Example, for this class SonarLint is accusing that it has complexity of if
:
package testes;
import java.math.BigDecimal;
public class EntidadeGrande {
private int codUser;
private String descUser;
private long idUser;
private BigDecimal valueX;
private int scoreUser;
private int scoreStore;
private int cdLogo;
private String nmMother;
private String sgEstado;
private String dsBandeira;
private String codCupom;
/* UM MONTE DE GET E SET AQUI, HASHCODE TAMBÉM */
@Override
public boolean equals(Object o) {
if (o == null || o.getClass() != getClass()) {
return false;
}
final EntidadeGrande e = (EntidadeGrande) o;
if (getCodUser() != e.getCodUser()
|| getIdUser() != e.getIdUser()
|| getScoreUser() != e.getScoreUser()
|| getScoreStore() != e.getScoreStore()
|| getCdLogo() != e.getCdLogo()) {
return false;
}
if (getValueX() == null && e.getValueX() != null
|| getValueX() != null && e.getValueX() == null) {
return false;
}
if (!getValueX().equals(e.getValueX())) {
return false;
}
if (getDescUser() != null && e.getDescUser() == null
|| getDescUser() == null && e.getDescUser() != null) {
return false;
}
if (getNmMother() != null && e.getNmMother() == null
|| getNmMother() == null && e.getNmMother() != null) {
return false;
}
if (getSgEstado() != null && e.getSgEstado() == null
|| getSgEstado() == null && e.getSgEstado() != null) {
return false;
}
if (getDsBandeira() != null && e.getDsBandeira() == null
|| getDsBandeira() == null && e.getDsBandeira() != null) {
return false;
}
if (getCodCupom() != null && e.getCodCupom() == null
|| getCodCupom() == null && e.getCodCupom() != null) {
return false;
}
if (!getDescUser().equals(e.getDescUser())) {
return false;
}
if (!getNmMother().equals(e.getNmMother())) {
return false;
}
if (!getSgEstado().equals(e.getSgEstado())) {
return false;
}
if (!getDsBandeira().equals(e.getDsBandeira())) {
return false;
}
if (!getCodCupom().equals(e.getCodCupom())) {
return false;
}
return true;
}
}