Exit Early

February 25, 2020 ยท View on GitHub

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: