menu

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.

Reported

1function acceptsMail(2    isActive: boolean,3    hasEmail: boolean,4    isSubscribed: boolean,5    isVerified: boolean,6    allowsMail: boolean,7): boolean {8    if (isActive) {9        if (hasEmail) {10            if (isSubscribed) {11                if (isVerified) {12                    if (allowsMail) {13                        return true;14                    }15                }16            }17        }18    }19    return false;20}

Accepted

1function acceptsMail(2    isActive: boolean,3    hasEmail: boolean,4    isSubscribed: boolean,5    isVerified: boolean,6    allowsMail: boolean,7): boolean {8    if (!isActive || !hasEmail || !isSubscribed || !isVerified || !allowsMail) {9        return false;10    }11    return true;12}