Demo: refactoring a long method under green tests
On This Lesson 3 topics
The content for this lesson is written and waiting for review. The outline below shows what it covers. It will be published once it is approved.
In this lesson
shrink a long checkout method step by step. Prepares JNR-003-LAB1.
Draft outline details
Step by step
- Run all tests: green.
- Rename unclear variables with the IDE.
- Extract Method for the discount block; run tests; commit "Extract calculateDiscount".
- Planned mistake: also change the rounding "while we are here"; a test turns red. Fix: undo, keep the commit pure, and do the change later in a separate commit.
- Replace
doublemoney with theMoneyrecord where tests allow. - Show the final method: short, readable, tests green.
Check your understanding
Why undo the rounding change?
Show answer
A refactoring commit must not change behavior.
Practice
Apply this lesson with the module's lab, exercises and quiz.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.