Aller au contenu

Deep Nesting (ARCH4184)

Ce contenu n’est pas encore disponible dans votre langue.

Key java.quality.deep-nesting · Severity warning · Category quality

Deeply nested if/for/while/try blocks hurt readability and raise the chance of bugs. Flatten conditions with early returns (guard clauses).

void handle(Order o) {
if (o != null) {
if (o.isPaid()) {
if (o.items() != null) {
for (Item i : o.items()) {
ship(i);
}
}
}
}
}
void handle(Order o) {
if (o == null || !o.isPaid() || o.items() == null) return;
for (Item i : o.items()) {
ship(i);
}
}

Flatten nesting with guard clauses and early returns; extract the inner block into a method if needed.