Exit Early
Discourage:
1
if () {
2
...
3
return false;
4
} else {
5
...
6
return true;
7
}
Copied!
Encourage:
1
if () {
2
...
3
return false;
4
}
5
6
...
7
8
return true;
Copied!
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:
Copy link