Extracting Title Normalization
Introduction
Welcome back to Ship a Reviewable Change. We are now in Lesson 5 of 6, Extract a Behavior-Preserving Refactor. In Lesson 4, we established a clean automated baseline, so we can now improve the code structure with current evidence instead of guessing.
Today, we will separate title normalization from title comparison. The visible result must stay the same: "Reset Password" and " reset password " still match. We will protect that contract with a focused test, remove lookup work from the proposal, and keep the ticket model intact. The goal is a smaller, clearer change, not a new feature.
Establish the Existing Comparison Contract
As we practiced in earlier lessons, a safe refactor begins with a clear behavior contract. Before extraction, the comparison logic can be represented like this:
The application reports:
First, lower() removes differences in letter casing. Then, split() separates the words while ignoring surrounding and repeated whitespace. Finally, " ".join(...) rebuilds each title with one space between words. The important contract is the True result and the existing titles_match(left, right) call. Variable names, expression placement, and helper structure can change as long as that behavior remains.
Set a Regression Safety Net
Before moving the normalization expression, we should record the current result in a focused test. This gives the refactor a clear safety net and helps us distinguish a structural change from an accidental behavior change.
The test imports the public comparison function and checks the result that matters to callers. Its name documents two parts of the contract: casing differences do not matter, and extra whitespace does not matter.
Notice what the test does not inspect. It does not check local variable names, the number of helper calls, or the location of the normalization expression. That keeps the test focused on behavior rather than implementation. We can now reorganize the code while expecting this assertion to remain valid.

