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

Explanation

Early returns make the "happy path" read top to bottom and keep the indentation flat.

Further reading

Problem with a question

Design Patterns · Junior · question 54

Test yourself

Take the Design Patterns · Junior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz