Design Patterns · Junior · Question 54 of 75
What does replacing nested ifs with guard clauses give you?
function pay(order) {
if (!order) return;
if (order.paid) return;
charge(order);
}- ✓Correct answer: Edge cases exit early, so the main logic isn't buried in deep nesting
- 2Faster execution
- 3Fewer tests
Explanation
Early returns make the "happy path" read top to bottom and keep the indentation flat.