如何降低返回月份名称的方法的圈复杂度?
How to reduce cyclomatic complexityfor a method returning the name of the month?
通过计算决策(独立路径)有一个metrics plugin in eclipse that measures cyclomatic complexity,具体来说:
(if, for, while, do, case, catch and the ?: ternary operator, as well as the && and || conditional logic operators in expressions)
例如,这是一个得分为 14 的方法。(1 + 每个决策路径)
String getMonthName (int month) {
switch (month) {
case 0: return "January";
case 1: return "February";
case 2: return "March";
case 3: return "April";
case 4: return "May";
case 5: return "June";
case 6: return "July";
case 7: return "August";
case 8: return "September";
case 9: return "October";
case 10: return "November";
case 11: return "December";
default: throw new IllegalArgumentException();
}
}
我想知道是否有一种方法可以在没有 java 中提到的分支的情况下做出决定,这会破坏评估。基本上,我想做出不同的决定,而无需插件检测到它们。这些指标将显示比实际更低的圈复杂度(独立 paths/decisions)。
为了降低圈复杂度,您应该解耦代码或重构代码以获得更好的实现。
对于提供的示例,您可以使用数组:
static final String[] MONTHS = {"January", "February", ... };
static String getMonthName(int month) {
if (month < 0 || month >= MONTHS.length) {
throw new IllegalArgumentException(String.format("Month %d doesn't exist", month));
}
return MONTHS[month];
}
即使是上面的方法也可以解耦更多:
static final String[] MONTHS = {"January", "February", ... };
static String getMonthName(int month) {
checkBoundaries(month, 0, MONTHS.length - 1, String.format("Month %d doesn't exist", month));
return MONTHS[month];
}
static void checkBoundaries(int n, int lowerBound, int upperBound, String errorMessage) {
if (n < lowerBound || n > upperBound) {
throw new IllegalArgumentException(errorMessage);
}
}
通过计算决策(独立路径)有一个metrics plugin in eclipse that measures cyclomatic complexity,具体来说:
(if, for, while, do, case, catch and the ?: ternary operator, as well as the && and || conditional logic operators in expressions)
例如,这是一个得分为 14 的方法。(1 + 每个决策路径)
String getMonthName (int month) {
switch (month) {
case 0: return "January";
case 1: return "February";
case 2: return "March";
case 3: return "April";
case 4: return "May";
case 5: return "June";
case 6: return "July";
case 7: return "August";
case 8: return "September";
case 9: return "October";
case 10: return "November";
case 11: return "December";
default: throw new IllegalArgumentException();
}
}
我想知道是否有一种方法可以在没有 java 中提到的分支的情况下做出决定,这会破坏评估。基本上,我想做出不同的决定,而无需插件检测到它们。这些指标将显示比实际更低的圈复杂度(独立 paths/decisions)。
为了降低圈复杂度,您应该解耦代码或重构代码以获得更好的实现。
对于提供的示例,您可以使用数组:
static final String[] MONTHS = {"January", "February", ... };
static String getMonthName(int month) {
if (month < 0 || month >= MONTHS.length) {
throw new IllegalArgumentException(String.format("Month %d doesn't exist", month));
}
return MONTHS[month];
}
即使是上面的方法也可以解耦更多:
static final String[] MONTHS = {"January", "February", ... };
static String getMonthName(int month) {
checkBoundaries(month, 0, MONTHS.length - 1, String.format("Month %d doesn't exist", month));
return MONTHS[month];
}
static void checkBoundaries(int n, int lowerBound, int upperBound, String errorMessage) {
if (n < lowerBound || n > upperBound) {
throw new IllegalArgumentException(errorMessage);
}
}