The SonarLint tells me that I can reduce this block of code:
if (item != null && StringUtils.isNotBlank(item.getDateService())) {
LocalDate dateConverted = MapperUtil.convertToLocalDate(item.getDateService());
if (dateConverted != null) {
if (item.getInstallId() == null) {
if (dateConverted.getYear() == year) {
letter = "A";
} else {
if (dateConverted.getYear() == year - 1) {
letter = "C";
}
}
} else {
if (dateConverted.getYear() == year) {
letter = "E";
}
}
}
}
This is the error:
Refactor this method to reduce its Cognitive Complexity from 21 to the 15 allowed. How to refactor and reduce the complexity
Any suggestions?
Thanks.
Try splitting out some of the logic to another method - innerCall:
if (item != null && StringUtils.isNotBlank(item.getDateService())) {
LocalDate dateConverted = MapperUtil.convertToLocalDate(item.getDateService());
if (dateConverted != null) {
letter = innerCall(item, dateConverted);
}
}
...
public String innerCall(ItemClass item, DateConvertedClass dateConverted) {
String letter = null;
if (item.getInstallId() == null) {
if (dateConverted.getYear() == year) {
letter = "A";
} else {
if (dateConverted.getYear() == year - 1) {
letter = "C";
}
}
} else {
if (dateConverted.getYear() == year) {
letter = "E";
}
}
return letter;
}