# Early Return From Functions

> Use early returns to reduce nesting and clarify control flow. Use when simplifying conditionals or guard clauses.

- Skill: `ihj04982/early-return-from-functions` (Agent Skill)
- Install (CLI): `npx skillmds@latest add ihj04982/early-return-from-functions`
- Raw SKILL.md: https://api.skillmd.com/api/skills/ihj04982/early-return-from-functions/raw
- Safety review: pending (external: skill-scanner PASS, skillspector PASS)
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: ihj04982 (https://skillmd.com/u/ihj04982)
- Updated: 2026-09-22
- Page: https://skillmd.com/skills/ihj04982/early-return-from-functions

---


## Early Return from Functions

Return early when result is determined to skip unnecessary processing.

**Incorrect (processes all items even after finding answer):**

```typescript
function validateUsers(users: User[]) {
  let hasError = false;
  let errorMessage = '';

  for (const user of users) {
    if (!user.email) {
      hasError = true;
      errorMessage = 'Email required';
    }
    if (!user.name) {
      hasError = true;
      errorMessage = 'Name required';
    }
    // Continues checking all users even after error found
  }

  return hasError ? { valid: false, error: errorMessage } : { valid: true };
}
```

**Correct (returns immediately on first error):**

```typescript
function validateUsers(users: User[]) {
  for (const user of users) {
    if (!user.email) {
      return { valid: false, error: 'Email required' };
    }
    if (!user.name) {
      return { valid: false, error: 'Name required' };
    }
  }

  return { valid: true };
}
```

