小编Ama*_*gar的帖子

降低切换语句的循环复杂度-Sonar

我想减少开关情况的圈复杂度,我的代码是:

public String getCalenderName() {
        switch (type) {
    case COUNTRY:
        return country == null ? name : country.getName() + HOLIDAY_CALENDAR;
    case CCP:
        return ccp == null ? name : ccp.getName() + " CCP" + HOLIDAY_CALENDAR;
    case EXCHANGE:
        return exchange == null ? name : exchange.getName() + HOLIDAY_CALENDAR;
    case TENANT:
        return tenant == null ? name : tenant.getName() + HOLIDAY_CALENDAR;
    default:
        return name;
    }
}
Run Code Online (Sandbox Code Playgroud)

此代码块的复杂度为16,并希望将其降低到10。country,ccp,exchange和tenant是我的不同对象。基于类型I将调用它们各自的方法。

java cyclomatic-complexity switch-statement sonarqube

6
推荐指数
2
解决办法
9884
查看次数