說明
Methods must not exceed a cyclomatic complexity of 10. Classes must not exceed a total complexity of 50.
Cyclomatic complexity counts the number of independent paths through a method. Higher values mean more branches, harder testing, and higher bug probability.
為什麼重要
- Testability — each path needs a test case; complexity 10 = minimum 10 test paths
- Bug density — methods with complexity > 10 have 2-3x more defects
- Readability — deeply nested logic is hard to follow
Enforcement
Rule: java:S1192 — Cyclomatic Complexity
Quality Gate: new_coverage >= 80% AND new_duplicated_lines_density < 3%
正例
public OrderStatus getStatus(Order order) {
if (order.isCancelled()) {
return OrderStatus.CANCELLED;
}
if (order.isDelivered()) {
return OrderStatus.DELIVERED;
}
return OrderStatus.IN_PROGRESS;
}
反例
public OrderStatus getStatus(Order order) {
if (order != null) {
if (!order.isCancelled()) {
if (order.isDelivered()) {
if (order.getDeliveryDate() != null) {
if (order.getDeliveryDate().isBefore(LocalDate.now())) {
// ❌ deeply nested, complexity = 6 just for this block
}
}
}
}
}
}