Exit Early

Discourage:

if () {
  ...
  return false;
} else {
  ...
  return true;
}

Encourage:

if () {
  ...
  return false;
}

...

return true;

Reasons to exit early:

  • Process of elimination

  • Reduces cognitive load

  • Reduces indents

  • We know success will always be at the end

  • Structures your functions:

    • Validation/error handling at the beginning

    • Successful at the end

Resources

Arguments against:

Last updated