Step Back to Refactor the Method as a Whole
Each conditional in greet(time:) is doing an important job. But looking at them all together makes me feel uncomfortable. Code should not only work, it should be easy to change. What happens if the customer asks us to change the starting time of “Good evening” from 5:00 p.m. to 6:00 p.m.? We’d have to change two lines: not only the starting time of “Good evening,” but the ending time of “Good afternoon.”
Even though we followed the three steps to apply TDD to what we have, repeating similar steps can sometimes lead to a narrow focus. So let’s take a step back and expand our focus. What can we do to make this method easier to maintain?
Now we encounter another principle of refactoring. Sometimes ...
Get iOS Unit Testing by Example now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.