use-guard-clauses-early-return
Installation
SKILL.md
Summary
Avoid deep nesting of if/else statements. Instead, check for invalid conditions or edge cases at the beginning of the function and return (or throw) immediately. This keeps the "Happy Path" (the main logic) indented as little as possible.
Rationale
- Cognitive Load: Deep nesting requires the reader to keep multiple contexts in their "mental stack". Flat code is easier to scan.
- Diff Readability: Adding a condition to a nested block often requires re-indenting the entire block, polluting git diffs.
- Focus: Guard clauses handle "distractions" (errors/edge cases) upfront, leaving the rest of the function focused on the core task.
Guidance
- Invert Conditions: Instead of
if (valid) { ... }, useif (!valid) return;. - Fail Fast: Handle specific error cases first.
- No
elseafter return: If anifblock returns, you do not need anelseblock. The rest of the function is implicitly theelse.