Java中的切换的McCabe Cyclomatic Complexity [英] McCabe Cyclomatic Complexity for switch in Java

查看:331
本文介绍了Java中的切换的McCabe Cyclomatic Complexity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了一个switch语句,共有13个案例,每个案例只有一行的返回值。

I am using a switch statement with 13 cases, each case only has an one line return value.

McCabe用红色描绘。有一个更容易的方式来编写一个大的switch语句吗?它看起来不复杂,但我不喜欢默认设置变成红色。如果其他人在我的代码使用相同的工具,看到红色的东西,他们可能会认为我是愚蠢的:-)

McCabe paints this in red. Is there an easier way to write a big switch statement? It doesn't seem complex to read, but I don't like the default setting turning red. If other people use the same tool on my code and see red stuff they might think I'm stupid :-)

编辑:我映射不同的SQL类型我自己更抽象的类型,因此减少类型的总数。

I'm mapping different SQL-Types to my own more abstract types, therefore reducing the total amount of types.

case Types.TIME:
    return AbstractDataType.TIME;
case Types.TIMESTAMP:
    return AbstractDataType.TIME;
case Types.DATE:
    return AbstractDataType.TIME;
case Types.BIGINT:
    return AbstractDataType.NUMERIC;
case Types.DECIMAL:
    return AbstractDataType.NUMERIC;

等等...

推荐答案

我不知道那么多McCabe工具。循环复杂性考虑的事情之一是多个退出点。

I don't know that much about McCabe tools. One of the things Cyclomatic complexity considers is multiple exit points.

我喜欢EnumMap的想法。

I like the EnumMap idea.

开关将被使用,你可以有一个结果变量,并废除所有的return语句。您还可以折叠具有相同结果类型的所有源值:

If a switch is going to be used, you could have a result variable and do away with all the return statements. You can also collapse all the source values that have the same result type:

result = null;

case Types.TIME:
case Types.DATE:
case Types.TIMESTAMP: result = AbstractDataType.TIME

// etc.

return result;

我认为这可以减少循环复杂性,无论任何人认为它是什么样式。这是一个不同的方式写的语句,虽然是否更容易你应该判断。

I think this reduces the cyclomatic complexity, regardless of what anyone thinks about it as style. And it is a different way to write the statement, though whether it is easier you should judge.

这篇关于Java中的切换的McCabe Cyclomatic Complexity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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