The refactoring is now published as Slice on sammancoaching.org: https://sammancoaching.org/refactorings/slice.html - git mv ideas/split-loop-with-lambda -> ideas/slice-with-lambda - rename learning-hour file and update frontmatter name - update paths in tools/rehearse scripts and report title - update prose: 'Split Loop with Lambda' -> 'Slice with Lambda' in learning hour, exercise READMEs, storyboard, solution comments - link the published Slice refactoring from the pattern draft
IceCreamForecasting — Slice with Lambda (exercise)
Pre-factored variant of IceCreamForecasting-Refactoring-Kata: the Q1/Q4 dead branch is stripped and the hardcoded dates are a constant now. Everything else — including the noise in the scoring loop — is untouched.
Task
longRangeForecast() is hard to test: it calls scorer, and Scorer
stands for an external weather service you must not call from a unit test.
The rest of the method's inputs are only DATES and ANCHOR_DATE.
Get it under test:
- First get any test running that calls
longRangeForecast()— it will fail or be flaky, because of the dependency onScorer. - Then use the Slice with Lambda mechanics (6 steps, on the handout) so that the weather collection becomes an extracted method that takes the weather-reading as a lambda parameter.
- Stop when you have an extracted method that takes the lambda as a parameter,
and a test that passes without touching
Scorer.
Constraint
Don't change Scorer and don't change the behaviour of
longRangeForecast(). Compile early, compile often.
Debrief questions
- At which step did plain Split Loop stop working, and why? (Hint: what type did your introduced variable end up being?)
- There are two calls to
scorerin the weather loop. What did you do with each one? - Could you delete either call without changing the forecast? Try it. Why did that work?
- Where would you take this next — more extraction, or moving the method
to another class where
Scoreris unknown?