Simplify Conditional

Lean on the good tests. Improve the code in ShoppingCart.

Session Outline

Connect: What is the goal of refactoring?

What are you hoping to achieve, when you sit down to refactor some code? What is your motivation?

Concept: Refactoring conditionals

Examples of behaviour-preserving transformations for conditionals. I started a repo for some of these.

Demo: show the patterns and the shape of the refactoring

Working on SupermarketReceipt. You have several if statements that assign a value to ‘x’ and several that assign a value to ‘discount’. One assigns both. You’d like to do ‘split loop’ on that if statement. Then you can use ‘slide statement’ and ‘extract method’ so you have one method to calculate ‘x’ and one to calculate the discount.

Explain this plan and note it on the whiteboard.

Concrete: refactor into simpler conditional structure

Set the group loose on the refactoring. Remind them of the plan, and remind them to use their tools.

Conclusions: note down learnings

How would you spot other situations where you can use ‘split loop’, ‘slide statement’ and ‘extract method’?