S01E07: [TDD πŸ… 6] Powerful Refactorings in Swift

In this episode, we refactor the QuestionViewController class and its tests, and we introduce a way for notifying a delegate when the user selects an answer.

We start the test refactoring by making use of a factory method with default values, so, in the event of introducing a new parameter to the QuestionViewController initializer, we only have to change the test factory method, leaving the tests free from change. As a result, the tests become lighter, less fragile and more readable.

In the view controller side, we refactor the creation of the table view cell. We introduce a private method responsible for dequeuing and creating the cell, resulting in a much cleaner UITableViewDataSource implementation.

In the second half of the episode, we add a closure property to the QuestionViewController, which is called every time an option is selected, signifying the user's answer.

Taking time to refactor and improve our code is an essential part of the test-driven discipline. As software professionals, we should strive to keep all our code, including the tests, in a clean state. By doing so, we end up with a codebase that is easy to read and follow, backed up by clean and reliable tests, enabling developers (including ourselves) to maintain, remove and add new functionalities with ease and confidence.

Subscribe today to our Youtube channel and catch free new episodes every week and follow along the project's progress on GitHub.