Preparatory Refactoring

Often, we want to improve our codebase, but refactoring is seen as a non-essential activity that gets postponed. Preparatory refactoring integrates refactoring into feature work: before adding a new feature, we refactor to make the implementation easier. This approach saves time and leads to a cleaner, more maintainable codebase.

Learning Objectives

Prerequisite

This learning hour is based on the Trivia kata, however it assumes tests are already in place and the typo Answer was corrent!!!! has been fixed.

The original Trivia kata is available here and is maintained by J.B.Rainsberger. For this learning hour check out the with-starter-unit-tests branch.

Before starting:

  1. Clone the Trivia repository
  2. Ensure you have all necessary dependencies installed

Since the original kata doesn’t include tests, you’ll need to:

  1. Add unit tests for the existing functionality (For some languages you may find a version with tests here.)
  2. Fix the typo in the “Answer was correct” message
  3. Ensure all tests pass before proceeding with the refactoring exercises

Outline

Connect

  1. Describe the change request: In the Trivia game, players move around a board, each field has a category, and when they land on a category, they answer a question. Initially, there are four categories; the new requirement is to support six categories.
  2. Ask participants: “How hard do you think it is to implement this change if you don’t know the codebase?” Participants share their estimates (e.g., sticky notes on Miro/whiteboard) and discuss.
  3. Reveal the actual code, showing the complexity and scattered category logic. Ask again: “How hard is it now?” and discuss how their estimates change.

Concept

Present Kent Beck’s advice: “Make the change easy, then make the easy change.” Use the swamp vs. highway analogy: Badly designed code is like a swamp; instead of slogging through, preparatory refactoring is like walking to the highway and then driving there.

Concrete (Demo)

Concrete (Participants)

The facilitator can choose the approach:

Reflect

Discuss as a group: