Files
stack/packages/mosaic/framework/skills/vercel-react-best-practices/rules/js-early-exit.md
T
fargo 1a822493ba format: apply repo prettier (3.8.1) to the folded skills tree
963 markdown files reformatted with the repository's pinned prettier so
pnpm format:check covers the folded tree like every other repo file.

The formatter's embedded-language pass also normalized code fences
(TS semicolons, closed HTML tags in examples, lowercased CSS hex colors,
one renumbered list that skipped an index). Alphanumeric token deltas vs
the fold commit were audited file-by-file; all are formatter-equivalent
markup normalizations plus the four sanitized skills.
2026-08-19 14:37:17 -05:00

1.1 KiB

title, impact, impactDescription, tags
title impact impactDescription tags
Early Return from Functions LOW-MEDIUM avoids unnecessary computation 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):

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):

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 };
}