From the course: Practical Design Patterns in Swift

Unlock the full course today

Join today to access over 22,400 courses taught by industry experts or purchase this course individually.

Coffee machine with nested conditionals

Coffee machine with nested conditionals - Swift Tutorial

From the course: Practical Design Patterns in Swift

Start my 1-month free trial

Coffee machine with nested conditionals

- [Instructor] Here's the simplified model of a coffee machine implemented in Swift. You can find the project in the Exercise Files folder chapter 13 > 13.2 > end. Here's how we could use it. When we press the blue button the machine performs a self-check. It does so by calling the isReadyToBrew method. You can see it at line 38. Now let's check that method. It starts at line 14. What it does it checks whether the water tank is filled, if the capsule bin is empty, and if there is a capsule inserted. The water tank may be empty, the capsule bin may be full, or we may have forgotten to insert a new capsule. Now a coffee machine will need to perform further checks to ensure proper operation. And we've already got three nested Ifs, which is what we call a code smell. As the number of checks grows the conditional logic becomes more and more complicated. Enhancing or maintaining such a code base becomes increasingly painful. So let's get rid of all the nested conditionals.

Contents