Simplify Conditional

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

Learning Goals

Generic

Specific for ‘split loop’, ‘slide statement’ and ‘extract method’

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?

This is an Open Question connect.

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 (optional): Assign smells to code samples

Similar to sorting items, show some slides with code smells from Concept part, and let participants assign stickies with names of respective conditional code smells.

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

Generic: What have you learned today?

Specific for the three techniques: How would you spot other situations where you can use ‘split loop’, ‘slide statement’ and ‘extract method’?