Deep nesting
This is how the comment reads inline, under the flagged line.
Why it matters
Every nested if or loop is another condition to keep in mind. Past three or four, readers lose track of which case they're in.
What to do
Return early for the edge cases, and pull inner loops into named functions.
Examples
if (a) {
for (x of xs) {
if (b) {
while (c) {
if (d) {}
}
}
}
}if (!a) return; for (const x of xs) handle(x);
What it catches
It reads the shape of the code: functions, blocks, loops and calls. Languages: TypeScript and JavaScript, Python. Scope: All files. These cases are the rule's own tests: the first set must be flagged and the second must not.
Flagged
function f() {
if (a) {
for (const x of xs) {
if (b) {
while (c) {
if (d) {
go();
}
}
}
}
}
}
src/app.py
def f():
if a:
for x in xs:
if b:
while c:
if d:
go()
Left alone
function f() {
if (a) {
go();
}
}
Research
Deeply nested blocks. Each level adds a condition the reader must track.
Turn it off or change it
In a config file for the CLI, or in the API's rules field:
{
"rules": {
"disable": [
"deep-nesting"
],
"severity": {
"deep-nesting": "low"
}
}
}
f. Each level is one more condition to hold in your head.