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

  1. Invert Conditions: Instead of if (valid) { ... }, use if (!valid) return;.
  2. Fail Fast: Handle specific error cases first.
  3. No else after return: If an if block returns, you do not need an else block. The rest of the function is implicitly the else.

Examples

Installs
1
First Seen
Apr 5, 2026
use-guard-clauses-early-return — mew-ton/coding-guidelines