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.
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:
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.
Enter the URL of a public GitHub repository