Back to all reviewers

Minimize code nesting depth

temporalio/temporal
Based on 3 comments
Go

Reduce cognitive load and improve code readability by minimizing nested code blocks. Prefer early returns and flattened logic over deeply nested conditions. This makes the code easier to read, understand, and maintain.

Code Style Go

Reviewer Prompt

Reduce cognitive load and improve code readability by minimizing nested code blocks. Prefer early returns and flattened logic over deeply nested conditions. This makes the code easier to read, understand, and maintain.

Example - Instead of nested conditions:

func foo() {
    result, err = tryA()
    if err != nil { 
        result, err = tryB()
        if err != nil { 
            result, err = tryC() 
            if err != nil {
                return nil, custom_error
            }
        }
    } 
    return result, err
}

Prefer flattened logic:

func foo() {
    result, err = tryA()
    if err == nil { 
        return result, nil
    }
    
    result, err = tryB()
    if err == nil { 
        return result, nil
    }

    result, err = tryC()
    if err == nil { 
        return result, nil
    }

    return nil, custom_error
}

Key benefits:

  • Reduces cognitive load when reading code
  • Makes the logical flow more obvious
  • Easier to add or modify conditions
  • Improves code maintainability

Note: While reducing nesting is generally beneficial, consider maintaining clear logical grouping when it helps tell the code’s story. The goal is to balance readability with logical clarity.

3
Comments Analyzed
Go
Primary Language
Code Style
Category

Source Discussions