简体   繁体   中英

Reducing the Cyclomatic Complexity of the code

Sonar gives a major violation error ("Cyclomatic Complexity") for the following code. Following method is used to get the date in a special format, eg 14-02-3 (Year-month-weekid).

How can I overcome this violation?

private String finalDateForProject; 
public String getFinalDateForProject() {
    return finalDateForProject;
}

public void setFinalDateForProject(Integer year,Integer month, Integer weekId) {

     String projectMonth;
        switch (month) {
            case 0:  projectMonth = "01";
                     break;
            case 1:  projectMonth = "02";
                     break;
            case 2:  projectMonth = "03";
                     break;
            case 3:  projectMonth = "04";
                     break;
            case 4:  projectMonth = "05";
                     break;
            case 5:  projectMonth = "06";
                     break;
            case 6:  projectMonth = "07";
                     break;
            case 7:  projectMonth = "08";
                     break;
            case 8:  projectMonth = "09";
                     break;
            case 9: projectMonth = "10";
                     break;
            case 10: projectMonth = "11";
                     break;
            case 11: projectMonth = "12";
                     break;
            default: projectMonth = " ";
                     break;
        }

        String yearEdited = year.toString();
        yearEdited = yearEdited.replace("20", ""); 


    String projectTrendDate = yearEdited +"-"+projectMonth+"-W"+weekId.toString();

            this.finalDateForProject =projectTrendDate;
}

One way to reduce cyclomatic complexity that I see is to replace the switch statement. Just create an array or HashMap that will map month index to number;

public void setFinalDateForProject(Integer year,Integer month, Integer weekId) {
    String[] months = new String[] {"01", "02", "03", "04", "05", ...}  
    // Replace switch statement
    String projectMonth = months[month];
    // Rest of your code
    ...
}

Another way to solve this problem will be to replace mapping of numbers to strings with converting integer to String using String.format . Use something like:

String projectMonth = String.format("%02d", month + 1);

A simple way to think about it is, cyclomatic complexity increases the more "branches" you have in your code. So with your switch statement you have a whole lot of branches (13 in fact, if I'm counting right). The switch statement can be replaced with this:

if (month < 0 || month > 11) {
    projectMonth = " ";
} else {
    month++;
    projectMonth = ((month < 10) ? "0" : "") + Integer.toString(month);
}

Note that this still has branches, namely the if/else and ternary ? . But these could probably be removed as well, a good alternative with an array is given in the other answer.

The question shouldn't be " How can I reduce the cyclomatic complexity? ", but rather " What is the best way to write this function? ". One answer is return String.format("%02d-%02d-W%2d", year-2000, month+1, weekId); .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM