Challenge 3: Guard Clauses — Solution # Before: # def can_checkout?(cart) # if cart # if !cart.items.empty? # if cart.items.all? { |item| item.in_stock? } # true # end # end # end # end # After: def can_checkout?(cart) return false unless cart return false if cart.items.empty? return false unless cart.items.all?(&:in_stock?) true end =begin Notes: - Each guard clause handles exactly one precondition and exits immediately with return false the moment that precondition fails, instead of nesting the rest of the method one level deeper for every additional check. - The final true sits unindented at the bottom — by the time execution reaches it, every guard has already passed, so no further nesting or conditional wrapping is needed to express "everything checked out." - cart.items.all?(&:in_stock?) reuses the Symbol#to_proc shorthand from Course 1, Chapter 8 instead of writing cart.items.all? { |item| item.in_stock? } — both work, and the shorthand fits naturally here since the block only calls one method with no arguments. - This version is trivially scannable top to bottom: read each guard clause as "bail out if this isn't true," with no need to mentally track three levels of nested if/end pairs the original version required. =end