Sonar給出以下代碼的主要違規錯誤(「循環複雜度」)。以下方法用於以特殊格式獲得日期,例如, 14-02-3
(年 - 月 - 周)。降低代碼的圈複雜度
我該如何克服這種違規行爲?
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;
}
要求是將日期以特殊格式存入前端(如果是2014年1月25日 - >(年 - 月 - 周) - > 14-01-4。整數參數**月**是0的基數 - (1月-0,2月1日...) –