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
.claude/rules/js-early-exit.mdWhat 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.mdThe paths frontmatter field limits the files
AGENTS.md
Codex reads AGENTS.md: paste the rule text there
Manually: put the files in place
.cursor/rules/js-early-exit.mdcFrontmatter: description, globs, alwaysApply
Manually: put the files in place
.github/instructions/js-early-exit.instructions.mdThe applyTo field sets which files it applies to
Manually: put the files in place
.devin/rules/js-early-exit.mdManually: put the files in place
.clinerules/js-early-exit.mdStep 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