Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to fix SonarLint Cognitive Complexity Error for many If else condition

I have to write a lot of conditions but sonarlint is giving error to refactor the method to reduce its cognitive complexity. How to resolve this. I tried switch case but that is also giving the same error.

public enum ExampleEnum{
UNKNOWN(-1),
D0_D100(0),
D100_D200(100),
D200_D300(200),
D300_D400(300),
D400_D500(400),
D500_D600(500),
D600_D700(600),
D700_D800(700),
D800_D900(800),
D900_D1000(900);
public static ExampleEnum getScoreType(Integer score) {
    if (Objects.isNull(score) || score < D0_D100.score) {
        return UNKNOWN;
    } else if (score >= D0_D100.score && score < D100_D200.score) {
        return D0_D100;
    } else if (score >= D100_D200.score && score < D200_D300.score) {
        return D100_D200;
    } else if (score >= D200_D300.score && score < D300_D400.score) {
        return D200_D300;
    } else if (score >= D300_D400.score && score < D400_D500.score) {
        return D300_D400;
    } else if (score >= D400_D500.score && score < D500_D600.score) {
        return D400_D500;
    } else if (score >= D500_D600.score && score < D600_D700.score) {
        return D500_D600;
    } else if (score >= D600_D700.score && score < D700_D800.score) {
        return D600_D700;
    } else if (score >= D700_D800.score && score < D800_D900.score) {
        return D700_D800;
    } else if (score >= D800_D900.score && score < D900_D1000.score) {
        return D800_D900;
    } else {
        return D900_D1000;
    }
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Put the logic into a loop. something like:

public static ExampleEnum getScoreType(Integer score) {
    if (Objects.isNull(score) || score < D0_D100.score) {
        return UNKNOWN;
    for (int i =0; i< ExampleEnum.values().length-1;i++) {
        if (ExampleEnum.values()[i].score <= score && ExampleEnum.values()[i+1].score > score )
            return ExampleEnum.values()[i];
    }
    return D900_D1000;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading