4
Sonarlint for Eclipse, accuses error:
Refactor this method to reduce its Cognitive Complexity from 64 to the 15 allowed.
Rewrite this method to reduce your cognitive complexity of 64 for 15 allowed
I mean, my method has many if
s and else
s, many decision points and I need to decrease this.
In a method of with business rules this until it is "easy", I rewrite the method by separating into several other small methods and "Junitizáveis".
The problem is the method equals()
, I have some objects with various fields, and the method equals()
need to check the similarity of all these fields, check for null
, of equals
and ==
for primitives.
That one of mine equals()
is already a method with simple context, it only does a task, is already testable by Junit, but has several decision points within.
What to do in this case? Create several methods, one to test null
, one to test equals()
, one to test primitives and gather all in the equals()
main? Have any better approach?
NOTE: I know I can join several if
s in a single line, but this greatly disrupts the visualization by a human being and I am trying to avoid this practice.
For this class, for example, Sonarlint is accusing that it has complexity of 34
:
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;
}
}
Thanks, where I work the codes pass through a corporate sonar automatically at every push on a given branch, and depending on the "note" that the commit earn from sonar, this push is refused.
– res
This class I created just to have a minimum and testable code to post here, no problem missing something, it was just for reference even. And the way is to separate it into various methods of comparison, or merge the Ifs like you did.
– res
I love these workflows that encourage making bad code because some crappy software determined, in times of artificial intelligence the IT still applies a lot of natural intelligence lack. I’m sorry you have to do such an atrocity.
– Maniero