McCabe cyclomatic complexity for switching in Java

I use the switch statement for 13 cases, and each case has only one line of return value

McCabe daubed it in red Is there a simpler way to write a large switch statement? It doesn't seem complicated, but I don't like the default setting turning red If others use the same tool on my code and see something red, they may think I'm stupid: -)

Edit: I map different SQL types to my own more abstract types, reducing the total number 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;

Wait

Solution

I don't know much about McCabe tools One of the things considered for ring complexity is multiple exit points

I like the idea of enummap

If you want to use a switch, you can use a result variable and cancel all return statements You can also collapse all source values with the same result type:

result = null;

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

// etc.

return result;

I think this will reduce the complexity of the circle, no matter what anyone thinks it is style And this writing is a different way, although you should make it easier to judge

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>