2016-11-15 81 views
1

我想减少我的开关罩的圈复杂度 我的代码是:减少圈复杂的switch语句 - 声纳

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; 
    } 
} 

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

+0

“这个代码的复杂度是16,并且想把它降低到10”为什么不把它降低到9?或8?还是11?为什么16有问题? –

+0

根据我的声纳规则,我希望它低于10,如果我们可以进一步降低它将会很好。 @AndyTurner –

+0

@AmarMagar你忘了在每种情况下添加break语句还是故意的?我不确定添加break语句是否有助于减少圈复杂度。 –

回答

2

据我所知,不要在switch语句中使用return语句。在switch语句之后使用变量应用该逻辑。 创建一个检查空值的方法,并从开关调用该方法,那么你将能够减少圆环复杂性

0

我相信这是一个Sonar警告。我认为Sonar警告不是必须做的规则,而只是指南。您的代码块是READABLEMAINTAINABLE原样。它已经很简单了,但如果你真的想改变它,你可以尝试下面的两种方法,看看复杂度是否变低:

注意:我现在没有编译器,所以可能会出现错误,对不起关于那个提前。

第一种方法:

Map<String, String> multipliers = new HashMap<String, Float>(); 
    map.put("country", country); 
    map.put("exchange", exchange); 
    map.put("ccp", ccp); 
    map.put("tenant", tenant); 

然后我们就可以使用地图抢权元素

return map.get(type) == null ? name : map.get(type).getName() + HOLIDAY_CALENDAR; 

第二个办法:

你所有的对象都具有相同的方法,这样你就可以在其中添加一个InterfacegetName()方法,并更改您的方法签名,如:

getCalendarName(YourInterface yourObject){ 
    return yourObject == null ? name : yourObject.getName() + HOLIDAY_CALENDAR; 
} 
+0

已尝试第一种方法,但无法在地图上调用getName值 –

+0

然后使用第二种方法,在其中引入一个带有getName()方法的新接口。如果你有一个共同的行为/方法,你应该使用多态的好处。 – halil

+0

以不同的方式找到答案,反正谢谢 –

0

您可以删除所有空比较,并在切换大小写前检查它。在这种情况下,复杂性会减少4或更多。

+0

尽量还是不能减少 –

0

如果您的对象:country,cpp,exchange和tenant共享相同的界面,例如ObjectWithGetName你可以重构你的代码如下:

public String getCalenderName() { 
    ObjectWithGetNameMethod calendarType = null; 
    switch (type) { 
     case COUNTRY: 
      calendarType = country; 
      break; 
     case CCP: 
      calendarType = cpp; 
      break; 
     case EXCHANGE: 
      calendarType = exchange; 
      break; 
     case TENANT: 
      calendarType = tenant; 
      break; 
     default: 
      calendarType = null; 
    } 
    return (calendarType != null ? (calendarType.getName() + HOLIDAY_CALENDAR) : name); 
    } 

而且我认为这将是很好的开关移动到不同的方法,因为它看上去就像巫婆会在许多不同的地方使用。

+0

这个解决方案不起作用,因为国家,ccp,exchange和tenant是不同的实体。所以在调用getName方法时,我必须使用它们各自的对象来调用方法。 –

-1
public String getName() { 
    if (type == null) { 
     return name; 
    } 
    if (type == BusinessCalendarType.COUNTRY) { 
     return country == null ? name : country.getName() + HOLIDAY_CALENDAR; 
    } else if (type == BusinessCalendarType.CCP) { 
     return ccp == null ? name : ccp.getName() + " CCP" + HOLIDAY_CALENDAR; 
    } else if (type == BusinessCalendarType.EXCHANGE) { 
     return exchange == null ? name : exchange.getName() + HOLIDAY_CALENDAR; 
    } else if (type == BusinessCalendarType.TENANT) { 
     return tenant == null ? name : tenant.getName() + HOLIDAY_CALENDAR; 
    } else { 
     return name; 
    } 
} 

这个工作对我来说

+0

这在功能上是错误的。 swicht语句中的缺省值不对应于类型== null –

+0

其实type是一个Enum,并且肯定会返回其中的一个值,或者只返回null。在我的问题中,我写了默认声明,这是因为声纳警告。反正你是对的。根据你的建议我已经改变了这个答案。 –

0

如果你的第一个目标,是唯一减少圈复杂度,你应该创建一些方法来获取的名称,如下面的各种方式。在具体的例子

public String getCalenderName() { 
    switch (type) { 
    case COUNTRY: 
     return getCountryName(); 
    case CCP: 
     return getCcpName(); 
    case EXCHANGE: 
     return getExchangeName(); 
    case TENANT: 
     return getTenantName(); 
    default: 
     return name; 
    } 
} 

private String getCountryName() { 
    return country == null ? name : country.getName() + HOLIDAY_CALENDAR; 
} 

private String getCcpName() { 
    return ccp == null ? name : ccp.getName() + " CCP" + HOLIDAY_CALENDAR; 
} 

private String getExchangeName() { 
    return exchange == null ? name : getName.toString() + HOLIDAY_CALENDAR; 
} 

private String getTenantName() { 
    return tenant == null ? name : getName.toString() + HOLIDAY_CALENDAR; 
} 

注意,我假设你有1类,聚(至少)4点十分相似的行为。重构肯定会更有意义,例如为了实现一个基本实现(抽象与否)和4个其他继承类。

+0

当然,如果您不希望声纳投诉,请添加javadoc;) –