Reviewer · Code Style
Keep code structure flat
Maintain code readability by minimizing nesting depth and using appropriate control structures. Prefer flat code organization over deeply nested conditions. When dealing with multiple conditions:
Maintain code readability by minimizing nesting depth and using appropriate control structures. Prefer flat code organization over deeply nested conditions. When dealing with multiple conditions:
- Invert if statements to handle edge cases early
- Use switch statements for multiple related conditions
- Structure code to make the logic flow obvious
Example - Instead of:
if condition {
if nestedCondition {
for name, s := range items {
if deeplyNested {
// actual logic
}
}
}
}
Prefer:
if !condition {
return
}
if !nestedCondition {
return
}
switch {
case condition1:
// handle case
case condition2:
// handle case
default:
// handle default
}
This approach improves code readability, reduces cognitive load, and makes the code easier to maintain and debug.