# excessive-nesting

Disallow control flow nested beyond four levels

Deeply nested control flow makes every operation depend on a long chain of surrounding branches.
Instead, you SHOULD use guards, early exits, or an extracted function once nesting exceeds four levels.

- Category: style
- Level: warning
- Fix: none
- Scope: module

## Reported

```ds title="main.ds"
function acceptsMail(
    isActive: boolean,
    hasEmail: boolean,
    isSubscribed: boolean,
    isVerified: boolean,
    allowsMail: boolean,
): boolean {
    if (isActive) {
        if (hasEmail) {
            if (isSubscribed) {
                if (isVerified) {
                    if (allowsMail) {
                        return true;
                    }
                }
            }
        }
    }
    return false;
}
```

## Accepted

```ds title="main.ds"
function acceptsMail(
    isActive: boolean,
    hasEmail: boolean,
    isSubscribed: boolean,
    isVerified: boolean,
    allowsMail: boolean,
): boolean {
    if (!isActive || !hasEmail || !isSubscribed || !isVerified || !allowsMail) {
        return false;
    }
    return true;
}
```

## Prior art

- [Clippy · excessive_nesting](https://rust-lang.github.io/rust-clippy/master/index.html#excessive_nesting)

[language/linter/src/rules/style/excessive_nesting.rs:8](https://github.com/destack-sh/destack/blob/main/language/linter/src/rules/style/excessive_nesting.rs#L8)
