Skip to main content
rule CX Codex

js-early-exit — Claude Code rule for early returns

The js-early-exit rule recommends returning as soon as a function’s result is clear. It fits checks where continuing after the first error serves no purpose.

MIT ★ 7,191 updated 2026-10-24

Install into Claude Code · Manually: put the files in place All methods and platforms ↓
.claude/rules/js-early-exit.md
Open on GitHub

What js-early-exit does

js-early-exit guides functions to stop once they can return a result. Its user-validation example reports the first error it encounters, or returns a valid result if every check passes. The function does not need to finish the whole loop before deciding what to return.

The rule contrasts a version that tracks errors and decides at the end with one that returns from inside the loop. There’s a trade-off: users after the first error won’t be checked. Add the rule file at .claude/rules/js-early-exit.md.

What it helps with

  • return the first error found while validating users
  • stop a function once its result is clear
  • avoid checking more items after an error
  • simplify functions with multiple validation conditions

Example prompts for the agent

  • Rewrite user validation so it returns the first error it finds.
  • Review this function and return as soon as its result is clear.
  • Change the loop so it stops checking users after the first error.

How to install js-early-exit

Manually: put the files in place

.claude/rules/js-early-exit.md

The paths frontmatter field limits the files

Step by step, with commands for each platform: AGENTS.md vs CLAUDE.md: what they are and which one to use

Questions about js-early-exit

How do I install js-early-exit in Claude Code?

Add the rule file at .claude/rules/js-early-exit.md. Claude Code can then apply the guidance in that file.

What does js-early-exit do?

It recommends returning from a function as soon as its result is known. The example reports the first validation error it finds.

Does the example check every user after an error?

No. It returns at the first error, so later users in the loop aren’t checked.

Source text

js-early-exit.md · MIT
---
title: Early Return from Functions
impact: LOW-MEDIUM
impactDescription: avoids unnecessary computation
tags: javascript, functions, optimization, early-return
---

## 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 }
}
```

© tech-leads-club · MIT · tech-leads-club/agent-skills

Similar IDE rules

All IDE rules for Frontend and UI →

From the same set130

The whole set →
rendering-hoist-jsx rendering-hoist-jsx extracts static JSX elements outside components to prevent element re-creation on every render pass. CX Codex +1 ★ 7,191repo stars rerender-transitions The rerender-transitions rule marks frequent, non-urgent state changes as transitions. It fits React code where updates such as scroll tracking should keep the interface responsive. CX Codex +1 ★ 7,191repo stars advanced-use-latest advanced-use-latest — a Claude rule file on reaching fresh callback values inside a React effect and keeping the dependency array down to state. CX Codex +1 ★ 7,191repo stars async-api-routes async-api-routes prevents sequential waterfall requests in API routes and Server Actions. It instructs the assistant to launch independent promises immediately rather than chaining awaits. CX Codex +1 ★ 7,191repo stars rerender-memo rerender-memo — a rule that guides your agent to extract expensive computations into memoized components, enabling early returns without triggering child logic. Speeds up React re-render paths. CX Codex +1 ★ 7,191repo stars bundle-preload bundle-preload directs the agent to preload heavy JavaScript chunks on user intent like hover or focus. This cuts perceived interface latency. CX Codex +1 ★ 7,191repo stars client-swr-dedup The client-swr-dedup rule recommends SWR to share requests between component instances and reuse cached data. It fits React projects that fetch data on the client. CX Codex +1 ★ 7,191repo stars rerender-memo-with-default-value rerender-memo-with-default-value — a rule that prevents memoization from breaking when React components have default values (functions, objects, arrays) in parameters. It instructs you to extract these defaults into module-level constants. CX Codex +1 ★ 7,191repo stars

Project sponsors

Companies that keep this analytics open