简体   繁体   English

降低代码的循环复杂性

[英]Reducing the Cyclomatic Complexity of the code

Sonar gives a major violation error ("Cyclomatic Complexity") for the following code. 声纳给出了以下代码的重大违规错误(“ Cyclomatic Complexity”)。 Following method is used to get the date in a special format, eg 14-02-3 (Year-month-weekid). 使用以下方法以特殊格式获取日期,例如14-02-3 (年月周)。

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. 我看到的降低循环复杂度的一种方法是替换switch语句。 Just create an array or HashMap that will map month index to number; 只需创建一个将月份索引映射到数字的数组或HashMap即可;

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 . 解决此问题的另一种方法是使用String.format将整数转换为String来替换数字到字符串的映射。 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). 因此,在您的switch语句中,您有很多分支(实际上,如果我没记错的话,实际上有13个分支)。 The switch statement can be replaced with this: switch语句可以替换为:

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 ? 注意,它仍然具有分支,即if / else和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); 一个答案是return String.format("%02d-%02d-W%2d", year-2000, month+1, weekId); .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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