使用地图真的能降低圈复杂度吗? [英] Does using a map truly reduce cyclomatic complexity?

查看:34
本文介绍了使用地图真的能降低圈复杂度吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有下面的原始方法.

public String someMethod(String str) {字符串 returnStr;if("BLAH".equals(str)) {returnStr="ok";} else if ("BLING".equals(str)) {returnStr="不行";} else if ("BONG".equals(str)) {returnStr="ok";}返回 returnStr;}

转换到下面真的能减少 CC 吗?

Map验证器 = new HashMap();validator.put("BLAH","ok");validator.put("BLING","不行");validator.put("BONG","ok");公共字符串 someMethod(String str) {返回validator.get(str);}

解决方案

简短回答:是的,在您的情况下使用 Hashmap 确实降低了 Cyclomatic complexcity.

详细答案:根据维基百科的圈复杂度是

<块引用><块引用>

它是对通过程序源代码的线性独立路径数量的定量度量.

有多种方法可以处理 if-else 情况.If-else 语句使代码可读性较差,难以理解.这些 if-else 也很糟糕,因为在这种情况下,每次添加/删除/修改时,您都需要修改现有的代码文件,而其他业务逻辑保持不变,并且由于这些文件的更改,您需要对它们全部进行测试再次.这会导致维护问题以及我们需要确保处理案例的所有地方.switch 语句也存在同样的问题,尽管它们的可读性稍差.

您使用的方式还减少了执行的不同逻辑路径.另一种替代方法如下.

您可以创建一个接口,例如 IPair.让这个接口定义一个抽象方法 public String getValue(); 让我们为每种情况定义不同的类,让 BlahMatch.java、Bling.java 和 Bong.java 实现IPair 并在getValue() 方法的实现中返回相应的String.

public String someMethod(IPair pair) {返回 pair.getValue();}

上述方法的优点是,如果您以后有一些新对,您仍然可以创建一个新类并轻松传递新类的对象,只需要让您的类提供 IPair.

Suppose I have the original method below.

public String someMethod(String str) {
    String returnStr;
    if("BLAH".equals(str)) { 
       returnStr="ok";
    } else if ("BLING".equals(str)) {
       returnStr="not ok";
    } else if ("BONG".equals(str)) {
       returnStr="ok";
    }
    return returnStr;
}

Does converting to below truly reduce CC?

Map<String, String> validator = new HashMap<String,String>();
validator.put("BLAH","ok");
validator.put("BLING","not ok");
validator.put("BONG","ok");

public String someMethod(String str) {
  return validator.get(str);
}

解决方案

Short answer : yes, the usuage of Hashmap in your case does reduce Cyclomatic complexcity.

Detailed answer : Cyclomatic complexcity as per wikipedia is

It is a quantitative measure of the number of linearly independent paths through a program's source code.

There are various ways to tackle if-else cases. If-else statements makes the code less readable, difficult to understand. These if-else are also bad as each time you have addition / deletion / modification in the cases then you need to modify the existing code files where your other business logic remains same, and due to change in these files you need to test them all over again. This leads to maintainance issues as well as at all the places we need to make sure to handle the cases. Same issues exists with switch statements too, though they are little more readable.

The way you have used also reduces the different logical paths of execution.Another alternative approach is as below.

You can create an interface say IPair. Let this interface define an abstract method public String getValue(); Lets define different classes for each case we have and let BlahMatch.java, Bling.java and Bong.java implement IPair and in the implementation of getValue() method return the appropriate String.

public String someMethod(IPair pair) {
    return pair.getValue();
}

The advantage of above approach is, in case you have some new pair later you can still create a new class and pass the object of your new class easily, just you need to have your class provide implementation for IPair.

这篇关于使用地图真的能降低圈复杂度吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆