0

Sonar gives a major violation error ("Cyclomatic Complexity") for the following code. Following method is used to get the date in a special format, e.g. 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;
}
Amila Iddamalgoda
  • 4,166
  • 11
  • 46
  • 85
  • Requirement is to get the date in a special format to the front end.(If it is 25 th of January 2014 -->(Year-Month-WeekId) --> 14-01-4. Integer parameter **month** is 0 based number - (January-0,February-1 ...) – Amila Iddamalgoda Feb 11 '14 at 07:22

3 Answers3

2

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);
Ivan Mushketyk
  • 8,107
  • 7
  • 50
  • 67
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.

William Gaul
  • 3,181
  • 2
  • 15
  • 21
0

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);.

Ross Patterson
  • 9,527
  • 33
  • 48