嵌套if预计是为什么被称为“意大利面条式代码”的原因。
这种代码难以阅读,重构,和维护。
以下代码演示了这个规则,默认的阈值为3:
public void process() {
if (condition1) { // Compliant - depth = 1
/* ... */
if (condition2) { // Compliant - depth = 2
/* ... */
if (condition3) { // Compliant - depth = 3, not exceeding the limit
/* ... */
if (condition4) { // Non-Compliant - depth = 4
if (condition5) { // Depth = 5, exceeding the limit, but issues are only reported on depth = 4
/* ... */
}
return;
}
}
}
}
}