Skip to content
Ameen Abozaid
In reviewJNR-003-L07

Demo: refactoring a long method under green tests

  • Lesson 7 of 7
  • 11 min read
  • Junior
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

  1. Run all tests: green.
  2. Rename unclear variables with the IDE.
  3. Extract Method for the discount block; run tests; commit "Extract calculateDiscount".
  4. 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.
  5. Replace double money with the Money record where tests allow.
  6. 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.

Lab and exercises Quiz

Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.

Type to search levels, phases, modules and lessons.