nestingdepth
About 229 wordsLess than 1 minute
2025-01-16
Enforces shallow nesting with early returns.
Category
Safety
What It Checks
This analyzer detects deeply nested code that should use early returns instead.
Why It Matters
Deep nesting is hard to read and reason about:
func Process(x int) error {
if x > 0 {
if x < 100 {
if isValid(x) {
if hasPermission() {
return doWork(x) // Where am I?
}
}
}
}
return nil
}Examples
Bad: Deep Nesting
func Process(user *User) error {
if user != nil {
if user.Active {
if user.HasPermission("write") {
if user.Quota > 0 {
return performAction(user)
} else {
return ErrQuotaExceeded
}
} else {
return ErrNoPermission
}
} else {
return ErrUserInactive
}
} else {
return ErrNilUser
}
}Good: Early Returns
func Process(user *User) error {
if user == nil {
return ErrNilUser
}
if !user.Active {
return ErrUserInactive
}
if !user.HasPermission("write") {
return ErrNoPermission
}
if user.Quota <= 0 {
return ErrQuotaExceeded
}
return performAction(user)
}The Pattern
- Check error conditions first
- Return early on failure
- Happy path flows straight down
func Process(input Input) (Output, error) {
// Validation - early returns
if input.A == "" {
return Output{}, errors.New("A is required")
}
if input.B < 0 {
return Output{}, errors.New("B must be positive")
}
// Happy path - no nesting
result := compute(input.A, input.B)
return Output{Value: result}, nil
}Configuration
# .golint-sl.yaml
analyzers:
nestingdepth: true # enabled by defaultWhen to Disable
- Complex algorithms where nesting is unavoidable
- Generated code
analyzers:
nestingdepth: falseRelated Analyzers
- functionsize - Function complexity
