Compare commits
1
Commits
main
..
1415257890
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1415257890 |
@@ -1,4 +0,0 @@
|
||||
website/
|
||||
.yaks
|
||||
bin/
|
||||
obj/
|
||||
@@ -1,210 +0,0 @@
|
||||
---
|
||||
name: learning-hour-design
|
||||
description: Design a 60-minute Samman-style Learning Hour. Use when creating, reviewing, or refining learning hours with the 4 C's (Connect, Concepts, Concrete Practice, Conclusions). Guides the user through learning goals, timing, and all four C's.
|
||||
---
|
||||
|
||||
# Learning Hour Design Skill
|
||||
|
||||
This skill helps Willem design learning hours following the Samman coaching format and the "4 C's" from *Training from the Back of the Room*.
|
||||
|
||||
## References
|
||||
|
||||
- Samman Learning Hour definition: https://sammancoaching.org/reference/learning_hour_definition.html
|
||||
- Samman 4C model page: https://sammancoaching.org/activities/4C_model.html
|
||||
- Samman Connect activities: https://sammancoaching.org/activities/connect.html
|
||||
- Samman Concept activities: https://sammancoaching.org/activities/concept.html
|
||||
- Samman Concrete Practice activities: https://sammancoaching.org/activities/concrete.html
|
||||
- Samman Conclusions activities: https://sammancoaching.org/activities/conclusions.html
|
||||
- Example learning hour (Testable Design – Naming): https://sammancoaching.org/learning_hours/testable_design/naming.html
|
||||
- All learning hours: https://sammancoaching.org/learning_hours/
|
||||
- 4 C's quick guide: https://bowperson.com/images/resources/quick-guide-to-four-c-map.pdf
|
||||
|
||||
## Process
|
||||
|
||||
Follow these steps in order. Be conversational — grill Willem with pointed questions, don't just lecture.
|
||||
|
||||
### Phase 1: Learning Goals
|
||||
|
||||
Start by helping Willem identify 1–3 clear learning goals. For each goal:
|
||||
|
||||
1. Ask: "What should participants be able to DO differently after this hour?"
|
||||
2. Push for observable, testable outcomes — not vague intentions.
|
||||
3. Challenge scope: "Can someone realistically achieve this in 60 minutes?"
|
||||
4. Help narrow broad topics into specific, actionable goals.
|
||||
|
||||
**Techniques for grilling:**
|
||||
- "How would you know someone learned this?"
|
||||
- "What's the smallest version of this that still matters?"
|
||||
- "If they could only walk away with one thing, what should it be?"
|
||||
|
||||
### Phase 2: Timetable
|
||||
|
||||
Between the learning goals and the 4 C's, design a timing plan. Analyze of 124 Samman learning hours shows these patterns.
|
||||
|
||||
#### Average times per block type (from the library)
|
||||
|
||||
| Block | Average | Range |
|
||||
|-------|---------|-------|
|
||||
| Connect | **6.6 min** | 2–15 min |
|
||||
| Concept | **9.2 min** | 2–25 min |
|
||||
| Concrete/Do | **25.3 min** | 5–45 min (per block) |
|
||||
| Conclusion/Reflect | **6.2 min** | 2–15 min |
|
||||
| Demo (optional) | **10.6 min** | 2–45 min |
|
||||
|
||||
#### Most common structure: 4 blocks (65% of hours)
|
||||
|
||||
| Block | Time | Description |
|
||||
|-------|------|-------------|
|
||||
| Connect | 5-10 min | Hook attention, connect to prior knowledge |
|
||||
| Concept | 10-15 min | Introduce key ideas |
|
||||
| Concrete Practice | 30-35 min | Single extended practice block |
|
||||
| Conclusion | 5-10 min | Wrap-up, key takeaways |
|
||||
|
||||
#### Two-practice-round structure (23% of hours)
|
||||
|
||||
| Block | Time | Description |
|
||||
|-------|------|-------------|
|
||||
| Connect | 5-10 min | Hook attention, connect to prior knowledge |
|
||||
| Concept | 5-10 min | Introduce key ideas before first practice |
|
||||
| Concrete Practice 1 | 15-20 min | First practice round |
|
||||
| Concept (deepening) | 5-10 min | Additional concepts after practice |
|
||||
| Concrete Practice 2 | 15-20 min | Second practice round |
|
||||
| Conclusion | 5-10 min | Wrap-up, key takeaways |
|
||||
|
||||
#### With a demo block (28% of hours)
|
||||
|
||||
| Block | Time | Description |
|
||||
|-------|------|-------------|
|
||||
| Connect | 5-10 min | Hook attention, connect to prior knowledge |
|
||||
| Concept | 5-10 min | Introduce key ideas |
|
||||
| Demo | 10-15 min | Demonstrate the technique |
|
||||
| Concrete Practice | 25-30 min | Participants practice |
|
||||
| Conclusion | 5-10 min | Wrap-up, key takeaways |
|
||||
|
||||
**Rules for the timetable:**
|
||||
- Total is usually 50-55 min of explicit blocks, leaving 5-10 min buffer for transitions.
|
||||
- **Single 35-min practice is more common** than two 15-min rounds — ask Willem which fits.
|
||||
- Connect is almost always first (99%). Conclusion is always last (99%).
|
||||
- Concept can come after practice (21% of hours) — used for deepening or explaining what they just experienced.
|
||||
- Demo is a distinct block from concept when present.
|
||||
- If Willem has more concepts than fit, help cut scope — 60 minutes is tight.
|
||||
|
||||
Present the timetable and ask Willem if it feels right before moving on.
|
||||
|
||||
### Phase 3: The 4 C's
|
||||
|
||||
Work through each C. For each one, ask questions and co-create content.
|
||||
|
||||
#### C1: Connect
|
||||
|
||||
"Connect it to what they already know." Connect activities get learners into a state of mind ready to learn — connecting to what they know, what they'll learn, what they want to learn, and to each other.
|
||||
|
||||
- What prior experience or knowledge can you hook into?
|
||||
- What analogy, story, or provocative question grabs attention?
|
||||
- How does this relate to the participants' real work?
|
||||
- Reference the Samman Connect page for activity ideas: https://sammancoaching.org/activities/connect.html
|
||||
|
||||
#### C2: Concepts
|
||||
|
||||
What are the key ideas, frameworks, or principles?
|
||||
|
||||
- List 2–4 concepts maximum. More than that won't fit in ~9 min average.
|
||||
- For each concept: what's the simplest way to explain it?
|
||||
- What visual, diagram, or example makes it click?
|
||||
- Consider: do some concepts go before practice and some after? (21% of hours interleave concept after practice)
|
||||
- Reference the Samman Concept page for activity ideas: https://sammancoaching.org/activities/concept.html
|
||||
|
||||
#### C3: Concrete Practice
|
||||
|
||||
This is where participants actively apply the new concept. Usually a coding exercise, but can be other active tasks (sorting, evaluating, puzzles).
|
||||
|
||||
- Willem often has a practice idea that's too big. **Actively help descope:**
|
||||
- "What's the minimum viable version of this exercise?"
|
||||
- "What can you cut and still hit the learning goal?"
|
||||
- "Can this be done in pairs in 15-35 minutes?"
|
||||
- "What tools do they need? Keep it simple — a screenshot on paper works, don't need editors."
|
||||
- Structure each practice round:
|
||||
- Clear instructions (what to do, with whom, for how long)
|
||||
- A specific task or prompt
|
||||
- A share-back or debrief question
|
||||
- List of tools/materials needed
|
||||
- Ensure the practice directly tests the learning goal.
|
||||
- Reference the Samman Concrete Practice page for activity ideas: https://sammancoaching.org/activities/concrete.html
|
||||
|
||||
#### C4: Conclusions
|
||||
|
||||
How do participants consolidate what they learned? Conclusions help learners summarize, evaluate, celebrate, and create action plans.
|
||||
|
||||
- What's the one-sentence takeaway?
|
||||
- How do participants share back from practice?
|
||||
- What commitment or next step do they leave with?
|
||||
- Reference the Samman Conclusions page for activity ideas: https://sammancoaching.org/activities/conclusions.html
|
||||
|
||||
### Phase 4: Review and Output
|
||||
|
||||
Once all phases are done:
|
||||
|
||||
1. Present the complete learning hour as a Markdown file matching the Samman format.
|
||||
2. Verify: does every C map back to a learning goal?
|
||||
3. Check the math: does the timetable add to ~55 min (leaving buffer)?
|
||||
4. Flag any gaps or scope creep.
|
||||
|
||||
## Output Format
|
||||
|
||||
When producing the final learning hour, use this Markdown structure matching the Samman convention:
|
||||
|
||||
```markdown
|
||||
---
|
||||
theme: [topic]
|
||||
title: [Learning Hour Title]
|
||||
name: [slug-name]
|
||||
kata: [kata-name if applicable]
|
||||
difficulty: [1-5]
|
||||
author: [author]
|
||||
affiliation: [affiliation]
|
||||
tags: [tag1 tag2]
|
||||
---
|
||||
|
||||
# [Learning Hour Title]
|
||||
|
||||
[Opening paragraph: why this matters, what problem it solves]
|
||||
|
||||
## Learning Goals
|
||||
|
||||
* [Goal 1 – observable outcome]
|
||||
* [Goal 2 – if applicable]
|
||||
|
||||
## Session Outline
|
||||
|
||||
* [X] min connect: [brief description]
|
||||
* [X] min concept: [brief description]
|
||||
* [X] min concrete: [brief description]
|
||||
* [X] min conclusion: [brief description]
|
||||
|
||||
## Connect - [Connect Activity Name]
|
||||
|
||||
[Content, instructions, examples]
|
||||
|
||||
## Concept - [Concept Name]
|
||||
|
||||
[Content, explanations, examples]
|
||||
|
||||
## Concrete Practice - [Practice Name]
|
||||
|
||||
[Instructions, task, tools, debrief]
|
||||
|
||||
## Conclusions - [Conclusion Activity Name]
|
||||
|
||||
[Wrap-up, takeaways, next steps]
|
||||
```
|
||||
|
||||
## Tips
|
||||
|
||||
- Always push back on scope creep — 60 minutes is tight; explicit blocks usually total 50-55 min.
|
||||
- If Willem has multiple learning goals, suggest splitting into separate hours.
|
||||
- Pair practice is the default format; suggest alternatives only if justified.
|
||||
- A single 35-min practice block is the most common pattern — two rounds is less common but valid.
|
||||
- Demo is a distinct block from concept; suggest it when the technique benefits from seeing it first.
|
||||
- When in doubt, reference the Samman example: https://sammancoaching.org/learning_hours/testable_design/naming.html
|
||||
- Browse the full library for inspiration: https://sammancoaching.org/learning_hours/
|
||||
- Check `website/_learning_hours/` in this repo for local copies of all learning hours.
|
||||
@@ -0,0 +1,9 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2026 learning-hours
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
@@ -0,0 +1,3 @@
|
||||
# learning-hour-maker
|
||||
|
||||
a coding agent skill to make learning hours, and learning hours in development
|
||||
-56
@@ -1,56 +0,0 @@
|
||||
#+title: Learning Hour Maker
|
||||
|
||||
This is a project to help me (Willem) create Learning Hours.
|
||||
|
||||
A learning hour is a short training session (~60 minutes) for software developers,
|
||||
led by a technical coach. It follows the [[https://sammancoaching.org/activities/4C_model.html][4 C's]] model from
|
||||
Sharon Bowman's "Training from the BACK of the Room".
|
||||
|
||||
See the [[https://sammancoaching.org/reference/learning_hour_definition.html][Samman Learning Hour definition]] for the official description.
|
||||
|
||||
* 4 C's
|
||||
|
||||
The 4 C's from [[https://bowperson.com/][Sharon Bowman]]'s "Training from the BACK of the Room":
|
||||
see the [[https://bowperson.com/images/resources/quick-guide-to-four-c-map.pdf][quick guide to the 4 C map]].
|
||||
|
||||
Samman has dedicated pages for each:
|
||||
|
||||
- [[https://sammancoaching.org/activities/connect.html][Connect]] — get learners warmed up and thinking about the topic
|
||||
- [[https://sammancoaching.org/activities/concept.html][Concept]] — explain new ideas with words, pictures, examples, stories
|
||||
- [[https://sammancoaching.org/activities/concrete.html][Concrete Practice]] — hands-on exercise where learners actively apply the new skill
|
||||
- [[https://sammancoaching.org/activities/conclusions.html][Conclusions]] — summarize, evaluate, celebrate, and plan next steps
|
||||
|
||||
* Samman Resources
|
||||
|
||||
- [[https://sammancoaching.org/learning_hours/][All Learning Hours]] — browse the full library
|
||||
- [[https://sammancoaching.org/learning_hours/testable_design/naming.html][Example: Reading by Renaming]] — a complete learning hour on testable design naming
|
||||
- [[https://sammancoaching.org/activities/][Activities]] — activity catalog by category
|
||||
- [[https://sammancoaching.org/kata_descriptions/][Katas]] — code kata descriptions used in learning hours
|
||||
|
||||
* Typical Timetable
|
||||
|
||||
Analysis of 124 Samman learning hours shows these average times:
|
||||
|
||||
| Block | Average | Range |
|
||||
|-------+---------+-------|
|
||||
| Connect | 6.6 min | 2–15 min |
|
||||
| Concept | 9.2 min | 2–25 min |
|
||||
| Concrete/Do | 25.3 min | 5–45 min |
|
||||
| Conclusion/Reflect | 6.2 min | 2–15 min |
|
||||
| Demo (optional) | 10.6 min | 2–45 min |
|
||||
|
||||
Most common structure (65%): 4 blocks — Connect, Concept, Concrete Practice, Conclusion.
|
||||
Two-practice-round structure (23%): Connect, Concept, Practice 1, Concept, Practice 2, Conclusion.
|
||||
With demo (28%): Connect, Concept, Demo, Practice, Conclusion.
|
||||
|
||||
Total explicit time is usually 50-55 min, leaving buffer for transitions.
|
||||
|
||||
* How to Use This Project
|
||||
|
||||
Use the ~learning-hour-design~ skill to walk through creating a learning hour:
|
||||
|
||||
- The agent will grill you on learning goals (finding 1-3 clear, testable outcomes)
|
||||
- Help design a timetable to pace the 60 minutes
|
||||
- Work through each of the 4 C's with pointed questions
|
||||
- Help descope concrete practice ideas that are too big
|
||||
- Produce a complete org-mode outline
|
||||
@@ -1,20 +0,0 @@
|
||||
## NuGet
|
||||
*.nupkg
|
||||
**/packages/
|
||||
|
||||
## Build
|
||||
bin/
|
||||
obj/
|
||||
|
||||
## IDE
|
||||
*.user
|
||||
*.suo
|
||||
*.userosscache
|
||||
*.sln.docstates
|
||||
.idea/
|
||||
*.slnx
|
||||
*.slnx launches
|
||||
|
||||
## NuGet local config (workaround for locked ~/.nuget)
|
||||
nuget.config
|
||||
*.log
|
||||
@@ -1,45 +0,0 @@
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.0.31903.59
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OrderDomain", "src/OrderDomain/OrderDomain.csproj", "{A1B2C3D4-E5F6-7890-ABCD-EF1234567890}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OrderTests", "tests/OrderTests/OrderTests.csproj", "{B2C3D4E5-F6A7-8901-BCDE-F12345678901}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OrderBookDomain", "src/OrderBookDomain/OrderBookDomain.csproj", "{C3D4E5F6-A7B8-9012-CDEF-123456789012}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OrderBookTests", "tests/OrderBookTests/OrderBookTests.csproj", "{D4E5F6A7-B8C9-0123-DEFA-234567890123}"
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Integration", "Integration", "{83A6B86A-4CD0-44BE-9FE0-408AC31E69F8}"
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Warmup", "Warmup", "{55470487-3CE4-4FD7-BADF-35F7D1473CA5}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{A1B2C3D4-E5F6-7890-ABCD-EF1234567890}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{A1B2C3D4-E5F6-7890-ABCD-EF1234567890}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{A1B2C3D4-E5F6-7890-ABCD-EF1234567890}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
|
||||
{C3D4E5F6-A7B8-9012-CDEF-123456789012}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{C3D4E5F6-A7B8-9012-CDEF-123456789012}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{C3D4E5F6-A7B8-9012-CDEF-123456789012}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
|
||||
{D4E5F6A7-B8C9-0123-DEFA-234567890123}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{D4E5F6A7-B8C9-0123-DEFA-234567890123}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{D4E5F6A7-B8C9-0123-DEFA-234567890123}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
|
||||
{B2C3D4E5-F6A7-8901-BCDE-F12345678901}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{B2C3D4E5-F6A7-8901-BCDE-F12345678901}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{B2C3D4E5-F6A7-8901-BCDE-F12345678901}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(NestedProjects) = preSolution
|
||||
{C3D4E5F6-A7B8-9012-CDEF-123456789012} = {83A6B86A-4CD0-44BE-9FE0-408AC31E69F8}
|
||||
{D4E5F6A7-B8C9-0123-DEFA-234567890123} = {83A6B86A-4CD0-44BE-9FE0-408AC31E69F8}
|
||||
{A1B2C3D4-E5F6-7890-ABCD-EF1234567890} = {55470487-3CE4-4FD7-BADF-35F7D1473CA5}
|
||||
{B2C3D4E5-F6A7-8901-BCDE-F12345678901} = {55470487-3CE4-4FD7-BADF-35F7D1473CA5}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
@@ -1,6 +0,0 @@
|
||||
{
|
||||
"sdk": {
|
||||
"version": "10.0.400",
|
||||
"rollForward": "latestFeature"
|
||||
}
|
||||
}
|
||||
@@ -1,197 +0,0 @@
|
||||
---
|
||||
theme: agentic_engineering
|
||||
title: Show, Don't Tell — Using Examples with Coding Agents
|
||||
name: show_dont_tell
|
||||
difficulty: 2
|
||||
author: willem
|
||||
affiliation:
|
||||
tags: agentic copilot refactoring tests examples
|
||||
---
|
||||
|
||||
# Show, Don't Tell — Using Examples with Coding Agents
|
||||
|
||||
You've described what you want to a coding agent, and it didn't get it right. Or it took so many rounds of corrections that you'd have just done it yourself. This learning hour explores a simpler approach: instead of telling the agent what style you want, show it.
|
||||
|
||||
## Learning Goals
|
||||
|
||||
* Use a concrete code example (show, don't tell) to guide a coding agent in propagating a refactoring pattern across a test suite.
|
||||
* Produce your own example before delegating: refactor a multi-step test into focused tests yourself, then show the result to the agent as the pattern.
|
||||
|
||||
## Session Outline
|
||||
|
||||
* 5 min connect: Frustrating agent conversations
|
||||
* 5 min concept: LLMs are pattern matchers
|
||||
* 20 min concrete practice: Round 1 — refactor tests using a custom matcher example
|
||||
* 15 min concrete practice: Round 2 — split multi-step tests using an example you create
|
||||
* 5 min conclusion: Share back and recognize the pattern in your own code
|
||||
|
||||
## Connect — Frustrating Agent Conversations
|
||||
|
||||
Pair up and share a time when you had to iterate multiple times with a coding agent just to get it to match the style you wanted. What did you try? How many rounds did it take?
|
||||
|
||||
This is a [Pair Share]({% link _activities/connect/pair_share.md %}) connect.
|
||||
|
||||
## Concept — LLMs Are Pattern Matchers
|
||||
|
||||
LLMs are pattern matchers and translation machines. They are better at recognizing a pattern in concrete code than interpreting abstract instructions.
|
||||
|
||||
### The "Tell" Approach (doesn't work well)
|
||||
|
||||
> Refactor these tests to encapsulate the total price check. Instead of asserting on `order.Total`, create a method `IsFullyPaid()` and test for that.
|
||||
|
||||
The agent might:
|
||||
- Create the method but not use it consistently
|
||||
- Misinterpret "encapsulate" and change production code instead of test code
|
||||
- Apply the pattern incorrectly or only partially
|
||||
|
||||
### The "Show" Approach (works better)
|
||||
|
||||
Refactor one test yourself — encapsulate the attribute check behind a behavior method:
|
||||
|
||||
```csharp
|
||||
// Before — checking an attribute
|
||||
[Test]
|
||||
public void CompletedOrder_TotalIsCorrect()
|
||||
{
|
||||
var order = new Order();
|
||||
order.AddLine(100m, 1);
|
||||
order.Complete();
|
||||
|
||||
Assert.That(order.Total, Is.EqualTo(100m));
|
||||
}
|
||||
|
||||
// After — encapsulating behavior
|
||||
[Test]
|
||||
public void CompletedOrder_TotalIsCorrect()
|
||||
{
|
||||
var order = new Order();
|
||||
order.AddLine(100m, 1);
|
||||
order.Complete();
|
||||
|
||||
Assert.That(order.IsFullyPaid(100m), Is.True);
|
||||
}
|
||||
```
|
||||
|
||||
Then ask the agent:
|
||||
|
||||
> The first test was refactored to use `order.IsFullyPaid(expected)` instead of asserting on `order.Total`. Apply the same pattern to the remaining tests.
|
||||
|
||||
The agent sees the concrete pattern and replicates it. No description needed.
|
||||
|
||||
### Key Insight
|
||||
|
||||
**Examples beat descriptions.** One concrete example is worth a paragraph of instructions. This works for any refactoring pattern — splitting tests, introducing custom matchers, extracting methods, renaming for clarity. Show the agent what "done" looks like.
|
||||
|
||||
## Concrete Practice — Round 1 — Refactor Tests with a Custom Matcher (20 min)
|
||||
|
||||
### Setup
|
||||
|
||||
Open the project in `exercises/show-dont-tell/` in Rider with GitHub Copilot Agent mode.
|
||||
|
||||
The project contains an `Order` domain and a test file `OrderTotalTests.cs` with 6 tests.
|
||||
|
||||
### Step 1 — Identify the Smell (5 min)
|
||||
|
||||
Open `OrderTotalTests.cs`. Look at the tests. What's the pattern you see?
|
||||
|
||||
- The first test (`DiscountOnFirstLine_TotalAndLineValues`) was already refactored — notice how it uses `Has.OrderState()` with a single assertion.
|
||||
- The remaining 5 tests each have multiple `Assert.That()` calls checking `order.Total`, `order.Lines[0].Value`, `order.Lines[1].Value`, etc.
|
||||
|
||||
These multiple asserts are checking attributes (state) rather than expressing intention. They're also hard to read and fragile — if the order structure changes, every test needs updating.
|
||||
|
||||
### Step 2 — Examine the Example (5 min)
|
||||
|
||||
Look at the first test and the custom matcher file `OrderStateConstraint.cs`. You don't need to understand how the matcher is implemented — just recognize the pattern:
|
||||
|
||||
```csharp
|
||||
// One assertion that bundles all checks
|
||||
Assert.That(order, Has.OrderState(
|
||||
expectedTotal: 210m,
|
||||
expectedLineValues: new[] { 160m, 50m }));
|
||||
```
|
||||
|
||||
vs.
|
||||
|
||||
```csharp
|
||||
// Three separate assertions
|
||||
Assert.That(order.Total, Is.EqualTo(240m));
|
||||
Assert.That(order.Lines[0].Value, Is.EqualTo(200m));
|
||||
Assert.That(order.Lines[1].Value, Is.EqualTo(40m));
|
||||
```
|
||||
|
||||
### Step 3 — Use the Agent (20-25 min)
|
||||
|
||||
Use GitHub Copilot Agent mode to refactor the remaining 5 tests. Prompt:
|
||||
|
||||
> The first test uses a custom matcher `Has.OrderState()`. Refactor the remaining tests to use the same pattern — one assertion with `Has.OrderState()` instead of multiple individual asserts.
|
||||
|
||||
Run the tests after the agent makes changes to verify nothing broke.
|
||||
|
||||
**If the agent doesn't get it right on the first try:**
|
||||
- Check that the example test is visible in the same file
|
||||
- Be more specific: "Look at `DiscountOnFirstLine_TotalAndLineValues` as the example"
|
||||
- Point out what went wrong and ask it to retry
|
||||
|
||||
### Tools Needed
|
||||
|
||||
- Rider with GitHub Copilot Agent mode
|
||||
- The `exercises/show-dont-tell/` project (included in this repo)
|
||||
- .NET 10 SDK
|
||||
|
||||
## Concrete Practice — Round 2 — Split Multi-Step Tests (15 min)
|
||||
|
||||
This round has a twist: **no refactored example is provided.** Creating the example is the exercise.
|
||||
|
||||
### Setup
|
||||
|
||||
In the same solution, open the `OrderBookTests` project. The domain is an `OrderBook` with a small state machine:
|
||||
|
||||
* `OpenOrder(customer)` creates a `Pending` order
|
||||
* `FinalizeOrder(id, at)` → `WaitingForPayment` (the timestamp is passed in — no global clock)
|
||||
* `PayOrder(id, paidAt)` → `Fulfilling`, unless payment arrives more than 14 days after finalization: the order is `Cancelled` and the payment is rejected
|
||||
* `ShipOrder(id)` → `Delivered`
|
||||
* `CancelOrder(id)` works from `Pending` and `WaitingForPayment` only; invalid transitions throw
|
||||
|
||||
`OrderBookLifecycleTests.cs` contains 7 integration tests in an anti-pattern style: **given/when/then/when/then**. Each test walks through several transitions with assertions in between, e.g. `PayThenShip_DeliversTheOrder` finalizes, asserts, pays, ships, and asserts again. The tests pass — they're just testing three behaviors each under a name that describes one.
|
||||
|
||||
### Step 1 — Identify the Smell (3 min)
|
||||
|
||||
Look at the tests. What makes them hard to work with?
|
||||
|
||||
* Multiple act/assert pairs: when one assertion fails, you don't know which transition misbehaved
|
||||
* Test names describe one behavior while the test body verifies three
|
||||
* Setup for a later transition is entangled with assertions for an earlier one
|
||||
|
||||
### Step 2 — Create the Example Yourself (7 min)
|
||||
|
||||
Pick **one** multi-step test and refactor it by hand into focused tests: one transition per test, each test setting up only the state it needs. Keep the exception paths (`Assert.Throws`) in their own tests. Run the tests — the split versions must cover the same behavior as the original.
|
||||
|
||||
You cannot show the agent what "done" looks like until you have built it. Notice what decisions you had to make: how to name the split tests, what setup helper to extract, where a transition deserves its own test.
|
||||
|
||||
### Step 3 — Use Your Example with the Agent (5 min)
|
||||
|
||||
Now show your refactored test to the agent and ask it to apply the same pattern to the remaining tests. Verify with the test run afterward.
|
||||
|
||||
**If the agent doesn't get it right:**
|
||||
|
||||
* Check whether your example is unambiguous — could it be read another way?
|
||||
* Compare with round 1: what did the provided example communicate that yours doesn't?
|
||||
|
||||
### Why No Example Was Provided
|
||||
|
||||
Round 1 showed that examples guide agents better than descriptions. Round 2 practices the harder half of that skill: producing the example. In real work, the pattern you want to propagate usually doesn't exist yet — someone has to write it first, and that someone is you.
|
||||
|
||||
## Conclusions — Share Back
|
||||
|
||||
Go around and ask:
|
||||
|
||||
1. **Did the agent get it right on the first try? What happened if it didn't?**
|
||||
2. **What made the example work — what would have made it fail?**
|
||||
3. **Do you recognize this pattern in your own tests?** Where could you use "show, don't tell" in your real work?
|
||||
4. **What was different in round 2, when you had to create the example yourself?** What made that harder — and what did you learn about writing a good example?
|
||||
|
||||
This is an [Explain the Main Idea]({% link _activities/conclusions/explain_main_idea.md %}) conclusion.
|
||||
|
||||
### One-Sentence Takeaway
|
||||
|
||||
When working with coding agents, show them what you want with a concrete example instead of describing it in words — LLMs are pattern matchers, and examples are patterns.
|
||||
@@ -1,2 +0,0 @@
|
||||
[tools]
|
||||
dotnet = "10"
|
||||
@@ -1,25 +0,0 @@
|
||||
namespace OrderBookDomain;
|
||||
|
||||
public enum OrderStatus
|
||||
{
|
||||
Pending,
|
||||
WaitingForPayment,
|
||||
Fulfilling,
|
||||
Delivered,
|
||||
Cancelled
|
||||
}
|
||||
|
||||
public class Order
|
||||
{
|
||||
public int Id { get; }
|
||||
public string Customer { get; }
|
||||
public OrderStatus Status { get; internal set; }
|
||||
public DateTimeOffset? FinalizedAt { get; internal set; }
|
||||
|
||||
internal Order(int id, string customer)
|
||||
{
|
||||
Id = id;
|
||||
Customer = customer;
|
||||
Status = OrderStatus.Pending;
|
||||
}
|
||||
}
|
||||
@@ -1,69 +0,0 @@
|
||||
namespace OrderBookDomain;
|
||||
|
||||
public class OrderBook
|
||||
{
|
||||
private readonly TimeSpan _paymentWindow;
|
||||
private readonly Dictionary<int, Order> _ordersById = new();
|
||||
private int _nextId = 1;
|
||||
|
||||
public OrderBook(TimeSpan? paymentWindow = null)
|
||||
=> _paymentWindow = paymentWindow ?? TimeSpan.FromDays(14);
|
||||
|
||||
public IReadOnlyList<Order> Orders => _ordersById.Values.ToList();
|
||||
|
||||
public Order OpenOrder(string customer)
|
||||
{
|
||||
var order = new Order(_nextId++, customer);
|
||||
_ordersById[order.Id] = order;
|
||||
return order;
|
||||
}
|
||||
|
||||
public Order GetOrder(int orderId)
|
||||
=> _ordersById.TryGetValue(orderId, out var order)
|
||||
? order
|
||||
: throw new InvalidOperationException($"Unknown order id {orderId}.");
|
||||
|
||||
public void FinalizeOrder(int orderId, DateTimeOffset finalizedAt)
|
||||
{
|
||||
var order = GetOrder(orderId);
|
||||
RequireStatus(order, OrderStatus.Pending, "finalize");
|
||||
order.Status = OrderStatus.WaitingForPayment;
|
||||
order.FinalizedAt = finalizedAt;
|
||||
}
|
||||
|
||||
public void PayOrder(int orderId, DateTimeOffset paidAt)
|
||||
{
|
||||
var order = GetOrder(orderId);
|
||||
RequireStatus(order, OrderStatus.WaitingForPayment, "pay");
|
||||
if (paidAt - order.FinalizedAt > _paymentWindow)
|
||||
{
|
||||
order.Status = OrderStatus.Cancelled;
|
||||
throw new InvalidOperationException(
|
||||
$"Payment for order {orderId} arrived after the payment window; the order was cancelled.");
|
||||
}
|
||||
order.Status = OrderStatus.Fulfilling;
|
||||
}
|
||||
|
||||
public void ShipOrder(int orderId)
|
||||
{
|
||||
var order = GetOrder(orderId);
|
||||
RequireStatus(order, OrderStatus.Fulfilling, "ship");
|
||||
order.Status = OrderStatus.Delivered;
|
||||
}
|
||||
|
||||
public void CancelOrder(int orderId)
|
||||
{
|
||||
var order = GetOrder(orderId);
|
||||
if (order.Status is not (OrderStatus.Pending or OrderStatus.WaitingForPayment))
|
||||
throw new InvalidOperationException(
|
||||
$"Cannot cancel order {orderId} in status {order.Status}.");
|
||||
order.Status = OrderStatus.Cancelled;
|
||||
}
|
||||
|
||||
private static void RequireStatus(Order order, OrderStatus expected, string action)
|
||||
{
|
||||
if (order.Status != expected)
|
||||
throw new InvalidOperationException(
|
||||
$"Cannot {action} order {order.Id} in status {order.Status} (expected {expected}).");
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
@@ -1,14 +0,0 @@
|
||||
namespace OrderDomain;
|
||||
|
||||
public class Order
|
||||
{
|
||||
public List<OrderLine> Lines { get; } = new();
|
||||
|
||||
public decimal Total => Lines.Sum(l => l.Value);
|
||||
|
||||
public void AddLine(decimal unitPrice, int quantity)
|
||||
=> Lines.Add(new OrderLine(unitPrice, quantity));
|
||||
|
||||
public void ApplyDiscount(int lineIndex)
|
||||
=> Lines[lineIndex].ApplyDiscount();
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
@@ -1,19 +0,0 @@
|
||||
namespace OrderDomain;
|
||||
|
||||
public class OrderLine
|
||||
{
|
||||
public decimal UnitPrice { get; }
|
||||
public int Quantity { get; }
|
||||
public bool HasDiscount { get; private set; }
|
||||
|
||||
public decimal Value =>
|
||||
HasDiscount ? UnitPrice * Quantity * 0.8m : UnitPrice * Quantity;
|
||||
|
||||
public OrderLine(decimal unitPrice, int quantity)
|
||||
{
|
||||
UnitPrice = unitPrice;
|
||||
Quantity = quantity;
|
||||
}
|
||||
|
||||
public void ApplyDiscount() => HasDiscount = true;
|
||||
}
|
||||
@@ -1,114 +0,0 @@
|
||||
using NUnit.Framework;
|
||||
using OrderBookDomain;
|
||||
|
||||
namespace OrderBookTests;
|
||||
|
||||
public class OrderBookLifecycleTests
|
||||
{
|
||||
private static readonly DateTimeOffset Day0 = new(2025, 6, 1, 0, 0, 0, TimeSpan.Zero);
|
||||
|
||||
private static DateTimeOffset At(int day) => Day0.AddDays(day);
|
||||
|
||||
[Test]
|
||||
public void FinalizeThenPay_MovesOrderThroughTwoStates()
|
||||
{
|
||||
var book = new OrderBook();
|
||||
var order = book.OpenOrder("anna");
|
||||
|
||||
book.FinalizeOrder(order.Id, At(1));
|
||||
Assert.That(book.GetOrder(order.Id).Status, Is.EqualTo(OrderStatus.WaitingForPayment));
|
||||
|
||||
book.PayOrder(order.Id, At(2));
|
||||
Assert.That(book.GetOrder(order.Id).Status, Is.EqualTo(OrderStatus.Fulfilling));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PayThenShip_DeliversTheOrder()
|
||||
{
|
||||
var book = new OrderBook();
|
||||
var order = book.OpenOrder("anna");
|
||||
|
||||
book.FinalizeOrder(order.Id, At(1));
|
||||
Assert.That(book.GetOrder(order.Id).Status, Is.EqualTo(OrderStatus.WaitingForPayment));
|
||||
|
||||
book.PayOrder(order.Id, At(2));
|
||||
book.ShipOrder(order.Id);
|
||||
Assert.That(book.GetOrder(order.Id).Status, Is.EqualTo(OrderStatus.Delivered));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PayWithinWindow_ThenLatePayOnSecondOrder_CancelsOnlyThatOrder()
|
||||
{
|
||||
var book = new OrderBook();
|
||||
var first = book.OpenOrder("anna");
|
||||
var second = book.OpenOrder("bram");
|
||||
|
||||
book.FinalizeOrder(first.Id, At(1));
|
||||
book.FinalizeOrder(second.Id, At(1));
|
||||
Assert.That(book.Orders, Has.Count.EqualTo(2));
|
||||
|
||||
book.PayOrder(first.Id, At(5));
|
||||
Assert.That(book.GetOrder(first.Id).Status, Is.EqualTo(OrderStatus.Fulfilling));
|
||||
|
||||
Assert.Throws<InvalidOperationException>(() => book.PayOrder(second.Id, At(30)));
|
||||
Assert.That(book.GetOrder(second.Id).Status, Is.EqualTo(OrderStatus.Cancelled));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PayBeforeFinalize_Throws_ThenFinalizeStillWorks()
|
||||
{
|
||||
var book = new OrderBook();
|
||||
var order = book.OpenOrder("anna");
|
||||
|
||||
Assert.Throws<InvalidOperationException>(() => book.PayOrder(order.Id, At(1)));
|
||||
Assert.That(book.GetOrder(order.Id).Status, Is.EqualTo(OrderStatus.Pending));
|
||||
|
||||
book.FinalizeOrder(order.Id, At(2));
|
||||
Assert.That(book.GetOrder(order.Id).Status, Is.EqualTo(OrderStatus.WaitingForPayment));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CancelPendingOrder_ThenPayAfterCancel_ThrowsAgain()
|
||||
{
|
||||
var book = new OrderBook();
|
||||
var order = book.OpenOrder("anna");
|
||||
|
||||
book.CancelOrder(order.Id);
|
||||
Assert.That(book.GetOrder(order.Id).Status, Is.EqualTo(OrderStatus.Cancelled));
|
||||
|
||||
Assert.Throws<InvalidOperationException>(() => book.PayOrder(order.Id, At(1)));
|
||||
Assert.That(book.GetOrder(order.Id).Status, Is.EqualTo(OrderStatus.Cancelled));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ShippedOrder_CannotBeCancelled()
|
||||
{
|
||||
var book = new OrderBook();
|
||||
var order = book.OpenOrder("anna");
|
||||
|
||||
book.FinalizeOrder(order.Id, At(1));
|
||||
book.PayOrder(order.Id, At(2));
|
||||
book.ShipOrder(order.Id);
|
||||
Assert.That(book.GetOrder(order.Id).Status, Is.EqualTo(OrderStatus.Delivered));
|
||||
|
||||
Assert.Throws<InvalidOperationException>(() => book.CancelOrder(order.Id));
|
||||
Assert.That(book.GetOrder(order.Id).Status, Is.EqualTo(OrderStatus.Delivered));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TwoCustomers_InterleaveTransitions_Independently()
|
||||
{
|
||||
var book = new OrderBook();
|
||||
var annas = book.OpenOrder("anna");
|
||||
var brams = book.OpenOrder("bram");
|
||||
|
||||
book.FinalizeOrder(brams.Id, At(1));
|
||||
Assert.That(book.GetOrder(annas.Id).Status, Is.EqualTo(OrderStatus.Pending));
|
||||
Assert.That(book.GetOrder(brams.Id).Status, Is.EqualTo(OrderStatus.WaitingForPayment));
|
||||
|
||||
book.PayOrder(brams.Id, At(2));
|
||||
book.CancelOrder(annas.Id);
|
||||
Assert.That(book.GetOrder(annas.Id).Status, Is.EqualTo(OrderStatus.Cancelled));
|
||||
Assert.That(book.GetOrder(brams.Id).Status, Is.EqualTo(OrderStatus.Fulfilling));
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<IsTestProject>true</IsTestProject>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
|
||||
<PackageReference Include="NUnit" Version="4.6.1" />
|
||||
<PackageReference Include="NUnit3TestAdapter" Version="6.3.0" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\OrderBookDomain\OrderBookDomain.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -1,74 +0,0 @@
|
||||
using System.Linq;
|
||||
using NUnit.Framework.Constraints;
|
||||
using OrderDomain;
|
||||
|
||||
namespace OrderTests;
|
||||
|
||||
/// <summary>
|
||||
/// Custom NUnit matcher that bundles multiple order assertions into one.
|
||||
/// Participants: you do not need to understand how this works —
|
||||
/// just recognize the pattern and use it as an example for the agent.
|
||||
/// </summary>
|
||||
public static class Has
|
||||
{
|
||||
public static IResolveConstraint OrderState(decimal expectedTotal, decimal[] expectedLineValues)
|
||||
=> new OrderStateConstraint(expectedTotal, expectedLineValues);
|
||||
}
|
||||
|
||||
public class OrderStateConstraint : Constraint
|
||||
{
|
||||
private readonly decimal _expectedTotal;
|
||||
private readonly decimal[] _expectedLineValues;
|
||||
|
||||
public OrderStateConstraint(decimal expectedTotal, decimal[] expectedLineValues)
|
||||
{
|
||||
_expectedTotal = expectedTotal;
|
||||
_expectedLineValues = expectedLineValues;
|
||||
}
|
||||
|
||||
public override string Description =>
|
||||
$"order with total {_expectedTotal} and line values [{string.Join(", ", _expectedLineValues)}]";
|
||||
|
||||
public override ConstraintResult ApplyTo<TActual>(TActual actual)
|
||||
{
|
||||
var order = (Order)(object)actual!;
|
||||
var actualTotal = order.Total;
|
||||
var actualLineValues = order.Lines.Select(l => l.Value).ToArray();
|
||||
|
||||
var details = new List<string>();
|
||||
if (actualTotal != _expectedTotal)
|
||||
details.Add($"Total: expected {_expectedTotal}, but was {actualTotal}");
|
||||
if (!actualLineValues.SequenceEqual(_expectedLineValues))
|
||||
details.Add(
|
||||
$"Line values: expected [{string.Join(", ", _expectedLineValues)}], " +
|
||||
$"but was [{string.Join(", ", actualLineValues)}]");
|
||||
|
||||
return new OrderStateResult(this, actual!, details.Count == 0, details);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A ConstraintResult that carries the per-field mismatch details and
|
||||
/// writes them as additional lines in the failure message, e.g.
|
||||
///
|
||||
/// Expected: order with total 210 and line values [160, 50]
|
||||
/// But was: <Order>
|
||||
/// Total: expected 210, but was 250
|
||||
/// Line values: expected [160, 50], but was [200, 50]
|
||||
/// </summary>
|
||||
private sealed class OrderStateResult : ConstraintResult
|
||||
{
|
||||
private readonly IReadOnlyList<string> _details;
|
||||
|
||||
public OrderStateResult(Constraint constraint, object? actual, bool isSuccess, IReadOnlyList<string> details)
|
||||
: base(constraint, actual, isSuccess)
|
||||
{
|
||||
_details = details;
|
||||
}
|
||||
|
||||
public override void WriteAdditionalLinesTo(MessageWriter writer)
|
||||
{
|
||||
foreach (var detail in _details)
|
||||
writer.WriteMessageLine(detail);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<IsTestProject>true</IsTestProject>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
|
||||
<PackageReference Include="NUnit" Version="4.6.1" />
|
||||
<PackageReference Include="NUnit3TestAdapter" Version="6.3.0" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\OrderDomain\OrderDomain.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -1,92 +0,0 @@
|
||||
using NUnit.Framework;
|
||||
using OrderDomain;
|
||||
|
||||
namespace OrderTests;
|
||||
|
||||
public class OrderTotalTests
|
||||
{
|
||||
// This test was refactored — notice the custom matcher.
|
||||
// The other tests still need the same treatment.
|
||||
[Test]
|
||||
public void DiscountOnFirstLine_TotalAndLineValues()
|
||||
{
|
||||
var order = new Order();
|
||||
order.AddLine(100m, 2); // line 0: 200 → 160 with discount
|
||||
order.AddLine(50m, 1); // line 1: 50
|
||||
|
||||
order.ApplyDiscount(0);
|
||||
|
||||
Assert.That(order, Has.OrderState(
|
||||
expectedTotal: 210m,
|
||||
expectedLineValues: [160m, 50m]));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DiscountOnSecondLine_TotalAndLineValues()
|
||||
{
|
||||
var order = new Order();
|
||||
order.AddLine(100m, 2); // line 0: 200
|
||||
order.AddLine(50m, 1); // line 1: 50 → 40 with discount
|
||||
|
||||
order.ApplyDiscount(1);
|
||||
|
||||
Assert.That(order.Total, Is.EqualTo(240m));
|
||||
Assert.That(order.Lines[0].Value, Is.EqualTo(200m));
|
||||
Assert.That(order.Lines[1].Value, Is.EqualTo(40m));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DiscountOnBothLines_TotalAndLineValues()
|
||||
{
|
||||
var order = new Order();
|
||||
order.AddLine(100m, 2); // line 0: 200 → 160
|
||||
order.AddLine(50m, 1); // line 1: 50 → 40
|
||||
|
||||
order.ApplyDiscount(0);
|
||||
order.ApplyDiscount(1);
|
||||
|
||||
Assert.That(order.Total, Is.EqualTo(200m));
|
||||
Assert.That(order.Lines[0].Value, Is.EqualTo(160m));
|
||||
Assert.That(order.Lines[1].Value, Is.EqualTo(40m));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void NoDiscount_TotalAndLineValues()
|
||||
{
|
||||
var order = new Order();
|
||||
order.AddLine(100m, 2); // line 0: 200
|
||||
order.AddLine(50m, 1); // line 1: 50
|
||||
|
||||
Assert.That(order.Total, Is.EqualTo(250m));
|
||||
Assert.That(order.Lines[0].Value, Is.EqualTo(200m));
|
||||
Assert.That(order.Lines[1].Value, Is.EqualTo(50m));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SingleLineWithDiscount_TotalAndValue()
|
||||
{
|
||||
var order = new Order();
|
||||
order.AddLine(75m, 4); // line 0: 300 → 240
|
||||
|
||||
order.ApplyDiscount(0);
|
||||
|
||||
Assert.That(order.Total, Is.EqualTo(240m));
|
||||
Assert.That(order.Lines[0].Value, Is.EqualTo(240m));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ThreeLinesOneDiscount_TotalAndLineValues()
|
||||
{
|
||||
var order = new Order();
|
||||
order.AddLine(20m, 1); // line 0: 20
|
||||
order.AddLine(30m, 2); // line 1: 60 → 48
|
||||
order.AddLine(10m, 3); // line 2: 30
|
||||
|
||||
order.ApplyDiscount(1);
|
||||
|
||||
Assert.That(order.Total, Is.EqualTo(98m));
|
||||
Assert.That(order.Lines[0].Value, Is.EqualTo(20m));
|
||||
Assert.That(order.Lines[1].Value, Is.EqualTo(48m));
|
||||
Assert.That(order.Lines[2].Value, Is.EqualTo(30m));
|
||||
}
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
bin/
|
||||
obj/
|
||||
*.user
|
||||
.idea/
|
||||
@@ -1,80 +0,0 @@
|
||||
# Slice with Lambda — C# exercises
|
||||
|
||||
Exercises for the **Slice with Lambda** learning hour (C# variant).
|
||||
|
||||
- Condensed mechanics: <https://www.sammancoaching.org/refactorings/slice.html>
|
||||
(where that page says mock, we use a lambda instead).
|
||||
- Full Java walkthrough: `../../ideas/slice-with-lambda/storyboard/storyboard.md`.
|
||||
|
||||
Open `SliceWithLambda.sln` in Rider (or run `dotnet test` — see below).
|
||||
Requires the .NET 10 SDK (pinned by `global.json`).
|
||||
|
||||
## What's in the box
|
||||
|
||||
| folder | what | start state |
|
||||
|---|---|---|
|
||||
| `src/SliceWithLambda/` | three demo examples (pristine) | the refactoring **not** done yet |
|
||||
| `src/Buyers/` | the pair exercise (pristine) | one loop, one lambda-able expression |
|
||||
| `solution/` | reference end states (facilitator + fast pairs) | mechanics done, tests stub through the seam |
|
||||
| `tests/` | test projects | see the red/green table |
|
||||
|
||||
## The three demo examples (`src/SliceWithLambda/`)
|
||||
|
||||
| file | guard (client state) | unwanted dependency | lambda | tail |
|
||||
|---|---|---|---|---|
|
||||
| `MetaphorFree.cs` | `if (a < 0) return 0;` | `_someDependency.mightThrowWhenPreconditionNotSatisfied(a)` | `Func<int, int> readB = a => ...` | `int c = a + b; return c * 2;` |
|
||||
| `WeatherStation.cs` | `if (_wateredToday)` | `_lightSensor.Read()` (byte) | `Func<byte> readLight = () => ...` | sunny -> `WateringDuration.Medium`, shady -> `Short` (reading > 128 => `SunExposure.Sunny`) |
|
||||
| `IceCreamShop.cs` | `if (_thawedToday)` | `_thermometer.Read()` (byte) | `Func<byte> readTemperature = () => ...` | warm -> `ThawAmount.Medium`, cold -> `Low` (temperature > 25 => `Warmth.Warm`) |
|
||||
|
||||
In all three, the guard reads client state (a parameter or a flag the client
|
||||
owns), so it stays in the extracted method. Only the dependency call moves
|
||||
into the lambda.
|
||||
|
||||
`MetaphorFree.cs` is the smallest possible shape: no loop, one dependency
|
||||
call, a tail of one arithmetic step. It carries the capture-to-parameter
|
||||
lesson: the lambda takes `a` as its parameter because a lambda moved up into
|
||||
the function head cannot capture method parameters. `WeatherStation.cs` and
|
||||
`IceCreamShop.cs` are same-shape domain repetitions, so participants can
|
||||
practice extracting the lambda twice — the C# delegate syntax (`Func<byte>`)
|
||||
is worth the repetition.
|
||||
|
||||
## The pair exercise (`src/Buyers/`)
|
||||
|
||||
`Buyers.ShoppingListTotal(buyerName)` computes a buyer's shopping-list
|
||||
total: one loop, and exactly **one** lambda-able expression —
|
||||
`priceList.PriceFor(flavour)`. `PriceList` adds random surge pricing, so the
|
||||
test in `tests/BuyersTests/` is red or flaky by design: it stands for the
|
||||
external pricing service.
|
||||
|
||||
Do the mechanics on the whole method:
|
||||
|
||||
1. Extract the dependency-using code into a lambda and slide it to the top
|
||||
(`Func<IceCream, int> priceFor = flavour => priceList.PriceFor(flavour);`
|
||||
— the invocation in the loop stays put).
|
||||
2. Extract the loop into a method taking the lambda as a parameter.
|
||||
3. Keep a no-arg overload for callers (pass the method group
|
||||
`priceList.PriceFor` as the "real" pricing function) and make the test
|
||||
pass with a pure lambda (`_ => 2`): 2 flavours x 2 scoops x 2 = 8.
|
||||
|
||||
Reference end state: `solution/Buyers/` (green test in
|
||||
`tests/BuyersSolutionTests/`).
|
||||
|
||||
## Tests: red/green map
|
||||
|
||||
| test project | against | expected at start | after the refactoring |
|
||||
|---|---|---|---|
|
||||
| `tests/BuyersTests/` | `src/Buyers/` | red or **flaky** (surge pricing) — that is the point | green once you stub with a pure lambda |
|
||||
| `tests/BuyersSolutionTests/` | `solution/Buyers/` | green | — |
|
||||
| `tests/SliceWithLambdaTests/` | `solution/SliceWithLambda/` | green | — (the demo end states, with pure-lambda stubs, incl. a throwing stub) |
|
||||
|
||||
## Run
|
||||
|
||||
```sh
|
||||
dotnet test # everything
|
||||
dotnet test tests/BuyersTests # the pair exercise only
|
||||
```
|
||||
|
||||
## Next move (debrief)
|
||||
|
||||
Add `DiscountFor(IceCream)` to `PriceList` and feel why one lambda is no
|
||||
longer enough — extract a role interface (`IPricing`) instead of two lambdas.
|
||||
@@ -1,57 +0,0 @@
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.0.31903.59
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SliceWithLambda", "src\SliceWithLambda\SliceWithLambda.csproj", "{A1B2C3D4-E5F6-7890-ABCD-EF1234567890}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Buyers", "src\Buyers\Buyers.csproj", "{B2C3D4E5-F6A7-8901-BCDE-F12345678901}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SliceWithLambdaSolution", "solution\SliceWithLambda\SliceWithLambda.csproj", "{C3D4E5F6-A7B8-9012-CDEF-123456789012}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BuyersSolution", "solution\Buyers\Buyers.csproj", "{D4E5F6A7-B8C9-0123-DEFA-234567890123}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SliceWithLambdaTests", "tests\SliceWithLambdaTests\SliceWithLambdaTests.csproj", "{E5F6A7B8-C9D0-1234-DEFA-234567890123}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BuyersTests", "tests\BuyersTests\BuyersTests.csproj", "{F6A7B8C9-D0E1-2345-EFAB-345678901234}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BuyersSolutionTests", "tests\BuyersSolutionTests\BuyersTests.csproj", "{A7B8C9D0-E1F2-3456-FABC-456789012345}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{A1B2C3D4-E5F6-7890-ABCD-EF1234567890}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{A1B2C3D4-E5F6-7890-ABCD-EF1234567890}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{A1B2C3D4-E5F6-7890-ABCD-EF1234567890}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{A1B2C3D4-E5F6-7890-ABCD-EF1234567890}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{B2C3D4E5-F6A7-8901-BCDE-F12345678901}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{B2C3D4E5-F6A7-8901-BCDE-F12345678901}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{B2C3D4E5-F6A7-8901-BCDE-F12345678901}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{B2C3D4E5-F6A7-8901-BCDE-F12345678901}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{C3D4E5F6-A7B8-9012-CDEF-123456789012}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{C3D4E5F6-A7B8-9012-CDEF-123456789012}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{C3D4E5F6-A7B8-9012-CDEF-123456789012}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{C3D4E5F6-A7B8-9012-CDEF-123456789012}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{D4E5F6A7-B8C9-0123-DEFA-234567890123}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{D4E5F6A7-B8C9-0123-DEFA-234567890123}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{D4E5F6A7-B8C9-0123-DEFA-234567890123}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{D4E5F6A7-B8C9-0123-DEFA-234567890123}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{E5F6A7B8-C9D0-1234-DEFA-234567890123}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{E5F6A7B8-C9D0-1234-DEFA-234567890123}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{E5F6A7B8-C9D0-1234-DEFA-234567890123}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{E5F6A7B8-C9D0-1234-DEFA-234567890123}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{F6A7B8C9-D0E1-2345-EFAB-345678901234}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{F6A7B8C9-D0E1-2345-EFAB-345678901234}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{F6A7B8C9-D0E1-2345-EFAB-345678901234}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{F6A7B8C9-D0E1-2345-EFAB-345678901234}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{A7B8C9D0-E1F2-3456-FABC-456789012345}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{A7B8C9D0-E1F2-3456-FABC-456789012345}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{A7B8C9D0-E1F2-3456-FABC-456789012345}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{A7B8C9D0-E1F2-3456-FABC-456789012345}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
@@ -1,6 +0,0 @@
|
||||
{
|
||||
"sdk": {
|
||||
"version": "10.0.400",
|
||||
"rollForward": "latestFeature"
|
||||
}
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace IceCreamBuyers
|
||||
{
|
||||
public class Buyer
|
||||
{
|
||||
public Buyer(string name, IReadOnlyList<IceCream> favourites)
|
||||
{
|
||||
Name = name;
|
||||
Favourites = favourites;
|
||||
}
|
||||
|
||||
public string Name { get; }
|
||||
public IReadOnlyList<IceCream> Favourites { get; }
|
||||
|
||||
public int ScoopsOf(IceCream flavour)
|
||||
{
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace IceCreamBuyers
|
||||
{
|
||||
/// <summary>
|
||||
/// Reference end state of exercise 3 (extract lambda, slide, extract method
|
||||
/// with a function parameter — the pure rep of Slice with Lambda).
|
||||
/// </summary>
|
||||
public class Buyers
|
||||
{
|
||||
private readonly PriceList priceList = new();
|
||||
private readonly List<Buyer> buyers = new()
|
||||
{
|
||||
new("Anke", new List<IceCream> { IceCream.Strawberry, IceCream.Vanilla }),
|
||||
new("Bram", new List<IceCream> { IceCream.Chocolate }),
|
||||
new("Celine", new List<IceCream> { IceCream.Vanilla, IceCream.Chocolate }),
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// No-arg overload: callers unchanged, the "real" pricing function baked in.
|
||||
/// </summary>
|
||||
public int ShoppingListTotal(string buyerName)
|
||||
{
|
||||
return ShoppingListTotal(ByName(buyerName), priceList.PriceFor);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The seam: the pricing source is injected as a function.
|
||||
/// Testable: pass any pure function instead of calling the pricing service.
|
||||
/// </summary>
|
||||
public int ShoppingListTotal(Buyer buyer, Func<IceCream, int> priceFor)
|
||||
{
|
||||
var total = 0;
|
||||
foreach (var flavour in buyer.Favourites)
|
||||
{
|
||||
total += buyer.ScoopsOf(flavour) * priceFor(flavour);
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
private Buyer ByName(string name)
|
||||
{
|
||||
return buyers.Find(b => b.Name == name)
|
||||
?? throw new InvalidOperationException($"Unknown buyer {name}.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -1,9 +0,0 @@
|
||||
namespace IceCreamBuyers
|
||||
{
|
||||
public enum IceCream
|
||||
{
|
||||
Strawberry,
|
||||
Chocolate,
|
||||
Vanilla
|
||||
}
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace IceCreamBuyers
|
||||
{
|
||||
public class PriceList
|
||||
{
|
||||
// placeholder implementation - real version would call the pricing service
|
||||
private static readonly Dictionary<IceCream, int> BasePrices =
|
||||
new()
|
||||
{
|
||||
{ IceCream.Strawberry, 3 },
|
||||
{ IceCream.Vanilla, 2 },
|
||||
{ IceCream.Chocolate, 4 },
|
||||
};
|
||||
|
||||
public int PriceFor(IceCream flavour)
|
||||
{
|
||||
return BasePrices[flavour] + SurgePricing();
|
||||
}
|
||||
|
||||
private int SurgePricing()
|
||||
{
|
||||
return new Random().Next(3);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,72 +0,0 @@
|
||||
// Example 3 solution — Ice cream shop demo of Slice with Lambda (C#), after
|
||||
//
|
||||
// Lambda extracted (step 1), slid to the top (step 2), method extracted
|
||||
// below the guard (step 3). The constructor now takes the lambda directly,
|
||||
// so tests construct with new IceCreamShop(() => 30).
|
||||
//
|
||||
// Condensed mechanics: https://sammancoaching.org/refactorings/slice.html
|
||||
// (where that page says mock, we use a lambda instead).
|
||||
|
||||
using System;
|
||||
|
||||
public enum Warmth
|
||||
{
|
||||
Cold,
|
||||
Warm
|
||||
}
|
||||
|
||||
public enum ThawAmount
|
||||
{
|
||||
None,
|
||||
Medium,
|
||||
Low
|
||||
}
|
||||
|
||||
public class Thermometer
|
||||
{
|
||||
private readonly Random _rng = new Random(); // stand-in for the hardware
|
||||
|
||||
public byte Read()
|
||||
{
|
||||
return (byte)_rng.Next(50); // raw sensor value
|
||||
}
|
||||
}
|
||||
|
||||
public class IceCreamShop
|
||||
{
|
||||
private readonly Func<byte> _readTemperature; // the thermometer, as a lambda
|
||||
private bool _thawedToday; // event-style client state
|
||||
|
||||
public IceCreamShop(Func<byte> readTemperature)
|
||||
{
|
||||
_readTemperature = readTemperature;
|
||||
}
|
||||
|
||||
public ThawAmount ThawStock()
|
||||
{
|
||||
Func<byte> readTemperature = () => _readTemperature(); // lambda at the top
|
||||
return ThawStockTail(readTemperature); // pass the lambda on
|
||||
}
|
||||
|
||||
private ThawAmount ThawStockTail(Func<byte> readTemperature)
|
||||
{
|
||||
if (_thawedToday) // guard: client state, stays
|
||||
{
|
||||
return ThawAmount.None;
|
||||
}
|
||||
|
||||
_thawedToday = true; // set only after thawing
|
||||
byte temperature = readTemperature(); // middle: the stubbed lambda
|
||||
Warmth warmth = ToWarmth(temperature);
|
||||
if (warmth == Warmth.Warm) // tail: complex behaviour below
|
||||
{ // the extraction point
|
||||
return ThawAmount.Medium;
|
||||
}
|
||||
return ThawAmount.Low;
|
||||
}
|
||||
|
||||
private static Warmth ToWarmth(byte temperature)
|
||||
{
|
||||
return temperature > 25 ? Warmth.Warm : Warmth.Cold;
|
||||
}
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
// Example 1 solution — Metaphor-free demo of Slice with Lambda (C#), after
|
||||
//
|
||||
// Lambda extracted (step 1), slid to the top (step 2), method extracted
|
||||
// below the guard (step 3), overload seam added (step 3b), b inlined
|
||||
// (final cleanup).
|
||||
//
|
||||
// Condensed mechanics: https://sammancoaching.org/refactorings/slice.html
|
||||
// (where that page says mock, we use a lambda instead).
|
||||
|
||||
using System;
|
||||
|
||||
public class Calculator
|
||||
{
|
||||
private readonly SomeDependency _someDependency;
|
||||
|
||||
public Calculator(SomeDependency someDependency)
|
||||
{
|
||||
_someDependency = someDependency;
|
||||
}
|
||||
|
||||
public int Calculate(int a) // overload: callers unchanged
|
||||
{
|
||||
return Calculate(a, a =>
|
||||
_someDependency.mightThrowWhenPreconditionNotSatisfied(a));
|
||||
} // the real λ, baked into the head
|
||||
|
||||
public int Calculate(int a, Func<int, int> readB) // the seam: λ is a parameter
|
||||
{
|
||||
if (a < 0) return 0; // guard clause: stays
|
||||
return CalculateTail(a, readB);
|
||||
}
|
||||
|
||||
private int CalculateTail(int a, Func<int, int> readB)
|
||||
{
|
||||
int c = a + readB(a); // b inlined
|
||||
return c * 2;
|
||||
}
|
||||
}
|
||||
|
||||
public class SomeDependency
|
||||
{
|
||||
private readonly Random _rng = new Random(); // stand-in for unpredictable computation
|
||||
|
||||
public int mightThrowWhenPreconditionNotSatisfied(int a)
|
||||
{
|
||||
if (a > 99)
|
||||
{
|
||||
throw new InvalidOperationException("precondition not satisfied");
|
||||
}
|
||||
return _rng.Next(100);
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -1,72 +0,0 @@
|
||||
// Example 2 solution — Weather Station domain demo of Slice with Lambda (C#), after
|
||||
//
|
||||
// Lambda extracted (step 1), slid to the top (step 2), method extracted
|
||||
// below the guard (step 3). The constructor now takes the lambda directly,
|
||||
// so tests construct with new SprinklerController(() => 200).
|
||||
//
|
||||
// Condensed mechanics: https://sammancoaching.org/refactorings/slice.html
|
||||
// (where that page says mock, we use a lambda instead).
|
||||
|
||||
using System;
|
||||
|
||||
public enum SunExposure
|
||||
{
|
||||
Shady,
|
||||
Sunny
|
||||
}
|
||||
|
||||
public enum WateringDuration
|
||||
{
|
||||
None,
|
||||
Medium,
|
||||
Short
|
||||
}
|
||||
|
||||
public class LightSensor
|
||||
{
|
||||
private readonly Random _rng = new Random(); // stand-in for hardware noise
|
||||
|
||||
public byte Read()
|
||||
{
|
||||
return (byte)_rng.Next(256);
|
||||
}
|
||||
}
|
||||
|
||||
public class SprinklerController
|
||||
{
|
||||
private readonly Func<byte> _readLight; // the sensor, as a lambda
|
||||
private bool _wateredToday; // event-style client state
|
||||
|
||||
public SprinklerController(Func<byte> readLight)
|
||||
{
|
||||
_readLight = readLight;
|
||||
}
|
||||
|
||||
public WateringDuration ScheduleWatering()
|
||||
{
|
||||
Func<byte> readLight = () => _readLight(); // lambda definition at the top
|
||||
return ScheduleWateringTail(readLight); // pass the lambda on
|
||||
}
|
||||
|
||||
private WateringDuration ScheduleWateringTail(Func<byte> readLight)
|
||||
{
|
||||
if (_wateredToday) // guard: client state, stays
|
||||
{
|
||||
return WateringDuration.None;
|
||||
}
|
||||
|
||||
_wateredToday = true; // set only after watering
|
||||
byte reading = readLight(); // middle: the stubbed lambda
|
||||
SunExposure exposure = ToSunExposure(reading);
|
||||
if (exposure == SunExposure.Sunny) // tail: complex behaviour below
|
||||
{ // the extraction point
|
||||
return WateringDuration.Medium;
|
||||
}
|
||||
return WateringDuration.Short;
|
||||
}
|
||||
|
||||
private static SunExposure ToSunExposure(byte reading)
|
||||
{
|
||||
return reading > 128 ? SunExposure.Sunny : SunExposure.Shady;
|
||||
}
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace IceCreamBuyers
|
||||
{
|
||||
public class Buyer
|
||||
{
|
||||
public Buyer(string name, IReadOnlyList<IceCream> favourites)
|
||||
{
|
||||
Name = name;
|
||||
Favourites = favourites;
|
||||
}
|
||||
|
||||
public string Name { get; }
|
||||
public IReadOnlyList<IceCream> Favourites { get; }
|
||||
|
||||
public int ScoopsOf(IceCream flavour)
|
||||
{
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace IceCreamBuyers
|
||||
{
|
||||
public class Buyers
|
||||
{
|
||||
private readonly PriceList priceList = new();
|
||||
private readonly List<Buyer> buyers = new()
|
||||
{
|
||||
new("Anke", new List<IceCream> { IceCream.Strawberry, IceCream.Vanilla }),
|
||||
new("Bram", new List<IceCream> { IceCream.Chocolate }),
|
||||
new("Celine", new List<IceCream> { IceCream.Vanilla, IceCream.Chocolate }),
|
||||
};
|
||||
|
||||
public int ShoppingListTotal(string buyerName)
|
||||
{
|
||||
var buyer = ByName(buyerName);
|
||||
var total = 0;
|
||||
foreach (var flavour in buyer.Favourites)
|
||||
{
|
||||
total += buyer.ScoopsOf(flavour) * priceList.PriceFor(flavour);
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
private Buyer ByName(string name)
|
||||
{
|
||||
return buyers.Find(b => b.Name == name)
|
||||
?? throw new InvalidOperationException($"Unknown buyer {name}.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -1,9 +0,0 @@
|
||||
namespace IceCreamBuyers
|
||||
{
|
||||
public enum IceCream
|
||||
{
|
||||
Strawberry,
|
||||
Chocolate,
|
||||
Vanilla
|
||||
}
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace IceCreamBuyers
|
||||
{
|
||||
public class PriceList
|
||||
{
|
||||
// placeholder implementation - real version would call the pricing service
|
||||
private static readonly Dictionary<IceCream, int> BasePrices =
|
||||
new()
|
||||
{
|
||||
{ IceCream.Strawberry, 3 },
|
||||
{ IceCream.Vanilla, 2 },
|
||||
{ IceCream.Chocolate, 4 },
|
||||
};
|
||||
|
||||
public int PriceFor(IceCream flavour)
|
||||
{
|
||||
return BasePrices[flavour] + SurgePricing();
|
||||
}
|
||||
|
||||
private int SurgePricing()
|
||||
{
|
||||
return new Random().Next(3);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,79 +0,0 @@
|
||||
// Example 3 — Ice cream shop demo of Slice with Lambda (C#)
|
||||
//
|
||||
// Condensed mechanics: https://sammancoaching.org/refactorings/slice.html
|
||||
// (where that page says mock, we use a lambda instead).
|
||||
//
|
||||
// Third repetition, same complexity as ScheduleWatering in WeatherStation.cs
|
||||
// (guard -> one dependency call -> map to enum -> constant tail), so
|
||||
// participants can practice extracting a lambda a second time. The C#
|
||||
// delegate syntax is worth the repetition:
|
||||
//
|
||||
// Func<byte> readTemperature = () => _thermometer.Read();
|
||||
//
|
||||
// Cast:
|
||||
// Thermometer — hardware-ish dependency. Read() returns a raw
|
||||
// byte value (not calibrated, the client decides
|
||||
// what it means).
|
||||
// Warmth — enum. Reading > 25 => Warm, otherwise Cold.
|
||||
// (Sunny/Shady's cousin from WeatherStation.cs.)
|
||||
// ThawAmount — enum. None / Medium / Low.
|
||||
// IceCreamShop — the client. Owns the _thawedToday flag, so the
|
||||
// guard is about client state, not sensor state.
|
||||
|
||||
using System;
|
||||
|
||||
public enum Warmth
|
||||
{
|
||||
Cold,
|
||||
Warm
|
||||
}
|
||||
|
||||
public enum ThawAmount
|
||||
{
|
||||
None,
|
||||
Medium,
|
||||
Low
|
||||
}
|
||||
|
||||
public class Thermometer
|
||||
{
|
||||
private readonly Random _rng = new Random(); // stand-in for the hardware
|
||||
|
||||
public byte Read()
|
||||
{
|
||||
return (byte)_rng.Next(50); // raw sensor value
|
||||
}
|
||||
}
|
||||
|
||||
public class IceCreamShop
|
||||
{
|
||||
private readonly Thermometer _thermometer;
|
||||
private bool _thawedToday; // event-style client state
|
||||
|
||||
public IceCreamShop(Thermometer thermometer)
|
||||
{
|
||||
_thermometer = thermometer;
|
||||
}
|
||||
|
||||
public ThawAmount ThawStock()
|
||||
{
|
||||
if (_thawedToday) // guard: client state, stays
|
||||
{
|
||||
return ThawAmount.None;
|
||||
}
|
||||
|
||||
_thawedToday = true; // set only after thawing
|
||||
byte temperature = _thermometer.Read(); // middle: unwanted dependency
|
||||
Warmth warmth = ToWarmth(temperature);
|
||||
if (warmth == Warmth.Warm) // tail: complex behaviour below
|
||||
{ // the extraction point
|
||||
return ThawAmount.Medium;
|
||||
}
|
||||
return ThawAmount.Low;
|
||||
}
|
||||
|
||||
private static Warmth ToWarmth(byte temperature)
|
||||
{
|
||||
return temperature > 25 ? Warmth.Warm : Warmth.Cold;
|
||||
}
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
// Example 1 — Metaphor-free demo of Slice with Lambda (C#)
|
||||
//
|
||||
// Condensed mechanics: https://sammancoaching.org/refactorings/slice.html
|
||||
// (where that page says mock, we use a lambda instead).
|
||||
//
|
||||
// Shape of the method (per zeroth-draft-of-refactoring-pattern.md):
|
||||
//
|
||||
// guard clauses — must run first
|
||||
// middle: dependency — one call on an object we don't want in the
|
||||
// extracted method
|
||||
// tail: depends on b — uses the value the dependency produced
|
||||
//
|
||||
// Deliberately NOT a loop: this is the smallest method that shows the
|
||||
// mechanics. The dependency both validates its precondition (using the
|
||||
// parameter a) and produces b — one expression, one thing to move into
|
||||
// the lambda.
|
||||
//
|
||||
// Unwanted dependency: SomeDependency — might throw when its precondition
|
||||
// on a is not satisfied, and returns an unpredictable value. An approval
|
||||
// test on Calculate is flaky until the lambda is stubbed.
|
||||
|
||||
using System;
|
||||
|
||||
public class Calculator
|
||||
{
|
||||
private readonly SomeDependency _someDependency;
|
||||
|
||||
public Calculator(SomeDependency someDependency)
|
||||
{
|
||||
_someDependency = someDependency;
|
||||
}
|
||||
|
||||
public int Calculate(int a)
|
||||
{
|
||||
if (a < 0) return 0; // guard clause: stays
|
||||
int b = _someDependency.mightThrowWhenPreconditionNotSatisfied(a); // middle: unwanted dependency
|
||||
int c = a + b; // tail: depends on b
|
||||
return c * 2;
|
||||
}
|
||||
}
|
||||
|
||||
public class SomeDependency
|
||||
{
|
||||
private readonly Random _rng = new Random(); // stand-in for unpredictable computation
|
||||
|
||||
public int mightThrowWhenPreconditionNotSatisfied(int a)
|
||||
{
|
||||
if (a > 99)
|
||||
{
|
||||
throw new InvalidOperationException("precondition not satisfied");
|
||||
}
|
||||
return _rng.Next(100);
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -1,71 +0,0 @@
|
||||
// Example 2 — Weather Station domain demo of Slice with Lambda (C#)
|
||||
//
|
||||
// Condensed mechanics: https://sammancoaching.org/refactorings/slice.html
|
||||
// (where that page says mock, we use a lambda instead).
|
||||
//
|
||||
// Cast (Head First Design Patterns Weather Station, reduced):
|
||||
// LightSensor — hardware-ish dependency. Read() returns a byte.
|
||||
// SunExposure — enum. Reading > 128 => Sunny, otherwise Shady.
|
||||
// WateringDuration — enum. None / Medium / Short.
|
||||
// (Long is YAGNI until the for-loop version of this
|
||||
// exercise exists.)
|
||||
// SprinklerController — the client. Owns the _wateredToday flag, so the
|
||||
// guard is about client state, not sensor state.
|
||||
|
||||
using System;
|
||||
|
||||
public enum SunExposure
|
||||
{
|
||||
Shady,
|
||||
Sunny
|
||||
}
|
||||
|
||||
public enum WateringDuration
|
||||
{
|
||||
None,
|
||||
Medium,
|
||||
Short
|
||||
}
|
||||
|
||||
public class LightSensor
|
||||
{
|
||||
private readonly Random _rng = new Random(); // stand-in for hardware noise
|
||||
|
||||
public byte Read()
|
||||
{
|
||||
return (byte)_rng.Next(256);
|
||||
}
|
||||
}
|
||||
|
||||
public class SprinklerController
|
||||
{
|
||||
private readonly LightSensor _lightSensor;
|
||||
private bool _wateredToday; // event-style client state
|
||||
|
||||
public SprinklerController(LightSensor lightSensor)
|
||||
{
|
||||
_lightSensor = lightSensor;
|
||||
}
|
||||
|
||||
public WateringDuration ScheduleWatering()
|
||||
{
|
||||
if (_wateredToday) // guard: client state, stays
|
||||
{
|
||||
return WateringDuration.None;
|
||||
}
|
||||
|
||||
_wateredToday = true; // set only after watering
|
||||
byte reading = _lightSensor.Read(); // middle: unwanted dependency
|
||||
SunExposure exposure = ToSunExposure(reading);
|
||||
if (exposure == SunExposure.Sunny) // tail: complex behaviour below
|
||||
{ // the extraction point
|
||||
return WateringDuration.Medium;
|
||||
}
|
||||
return WateringDuration.Short;
|
||||
}
|
||||
|
||||
private static SunExposure ToSunExposure(byte reading)
|
||||
{
|
||||
return reading > 128 ? SunExposure.Sunny : SunExposure.Shady;
|
||||
}
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
using IceCreamBuyers;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace IceCreamBuyers.Tests
|
||||
{
|
||||
[TestClass]
|
||||
public class BuyersTests
|
||||
{
|
||||
[TestMethod]
|
||||
public void ShoppingListTotalUsesThePricingWePassIn()
|
||||
{
|
||||
// pure pricing function: everything costs 2.
|
||||
// Anke: 2 flavours x 2 scoops x 2 = 8 — deterministic, glanceable.
|
||||
var buyers = new Buyers();
|
||||
var anke = new Buyer("Anke", new List<IceCream> { IceCream.Strawberry, IceCream.Vanilla });
|
||||
Assert.AreEqual(8, buyers.ShoppingListTotal(anke, _ => 2));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
|
||||
<PackageReference Include="MSTest.TestAdapter" Version="3.6.4" />
|
||||
<PackageReference Include="MSTest.TestFramework" Version="3.6.4" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\solution\Buyers\Buyers.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -1,19 +0,0 @@
|
||||
using IceCreamBuyers;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
|
||||
namespace IceCreamBuyers.Tests
|
||||
{
|
||||
[TestClass]
|
||||
public class BuyersTests
|
||||
{
|
||||
[TestMethod]
|
||||
public void ShoppingListTotal()
|
||||
{
|
||||
// Anke: 2 flavours x 2 scoops x base prices (3 + 2) = 10.
|
||||
// Red or flaky — why? (PriceList adds random surge pricing;
|
||||
// it stands for the external pricing service.)
|
||||
var buyers = new Buyers();
|
||||
Assert.AreEqual(10, buyers.ShoppingListTotal("Anke"));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
|
||||
<PackageReference Include="MSTest.TestAdapter" Version="3.6.4" />
|
||||
<PackageReference Include="MSTest.TestFramework" Version="3.6.4" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\Buyers\Buyers.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -1,82 +0,0 @@
|
||||
// Tests for the three Slice-with-Lambda solutions — step 4: the lambda as a stub.
|
||||
//
|
||||
// Condensed mechanics: https://sammancoaching.org/refactorings/slice.html
|
||||
// (where that page says mock, we use a lambda instead).
|
||||
|
||||
using System;
|
||||
using Xunit;
|
||||
|
||||
public class SliceWithLambdaTests
|
||||
{
|
||||
// --- Example 1: metaphor-free (Calculator) ---
|
||||
|
||||
[Fact]
|
||||
public void Guards_against_negative_input()
|
||||
{
|
||||
var calc = new Calculator(new SomeDependency()); // the real, flaky one...
|
||||
Assert.Equal(0, calc.Calculate(-1, a => 42)); // ...never asked: stub not invoked
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Throws_when_precondition_not_satisfied()
|
||||
{
|
||||
var calc = new Calculator(new SomeDependency());
|
||||
Assert.Throws<InvalidOperationException>(
|
||||
() => calc.Calculate(7, a => throw new InvalidOperationException()));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Combines_input_and_the_stubbed_value()
|
||||
{
|
||||
var calc = new Calculator(new SomeDependency());
|
||||
Assert.Equal(84, calc.Calculate(0, a => 42)); // stub through the seam
|
||||
}
|
||||
|
||||
// --- Example 2: Weather Station (SprinklerController) ---
|
||||
|
||||
[Fact]
|
||||
public void Skips_watering_when_already_watered()
|
||||
{
|
||||
var controller = new SprinklerController(() => 200); // pure lambda: no sensor
|
||||
controller.ScheduleWatering(); // water once...
|
||||
Assert.Equal(WateringDuration.None, controller.ScheduleWatering());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Waters_longer_in_full_sun()
|
||||
{
|
||||
var controller = new SprinklerController(() => 200); // sunny: > 128
|
||||
Assert.Equal(WateringDuration.Medium, controller.ScheduleWatering());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Waters_less_in_the_shade()
|
||||
{
|
||||
var controller = new SprinklerController(() => 10); // shady: ≤ 128
|
||||
Assert.Equal(WateringDuration.Short, controller.ScheduleWatering());
|
||||
}
|
||||
|
||||
// --- Example 3: Ice Cream Shop (IceCreamShop) ---
|
||||
|
||||
[Fact]
|
||||
public void Skips_thawing_when_already_thawed()
|
||||
{
|
||||
var shop = new IceCreamShop(() => 30); // pure lambda: no freezer
|
||||
shop.ThawStock(); // thaw once...
|
||||
Assert.Equal(ThawAmount.None, shop.ThawStock());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Thaws_more_when_warm()
|
||||
{
|
||||
var shop = new IceCreamShop(() => 30); // warm: > 25
|
||||
Assert.Equal(ThawAmount.Medium, shop.ThawStock());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Thaws_less_when_cold()
|
||||
{
|
||||
var shop = new IceCreamShop(() => 5); // cold: ≤ 25
|
||||
Assert.Equal(ThawAmount.Low, shop.ThawStock());
|
||||
}
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
|
||||
<PackageReference Include="xunit" Version="2.9.2" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\solution\SliceWithLambda\SliceWithLambda.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Submodule export/slice-with-lambda deleted from 17e5d25b39
@@ -1,50 +0,0 @@
|
||||
# Exercise folder — Slice with Lambda (learning hour)
|
||||
|
||||
Contents:
|
||||
|
||||
- `java/` — pre-factored exercise repo (prefactored-longrange; runs with `mvn test`)
|
||||
- `solution/` — reference end state after step 3 (extracted method taking the lambda)
|
||||
- `rehearse-with-agent.md` — how Willem rehearses this with qwen-code + java-refactoring-mcp
|
||||
|
||||
## Reference solution
|
||||
|
||||
See `solution/`. End state of the production code (simplified):
|
||||
|
||||
```java
|
||||
public Map<IceCream, Integer> longRangeForecast() {
|
||||
return longRangeForecast(daysForward -> {
|
||||
scorer.updateSelection();
|
||||
return scorer.lookupWeather(daysForward);
|
||||
});
|
||||
}
|
||||
|
||||
public Map<IceCream, Integer> longRangeForecast(
|
||||
LongFunction<Boolean> dayOffsetToWeather) {
|
||||
var expectedWeather = collectExpectedWeather(dayOffsetToWeather);
|
||||
// ... reset loop + scoring loop, unchanged ...
|
||||
}
|
||||
|
||||
private static List<Boolean> collectExpectedWeather(
|
||||
LongFunction<Boolean> dayOffsetToWeather) {
|
||||
var expectedWeather = new ArrayList<Boolean>();
|
||||
for (Instant interestingDate : DATES) {
|
||||
long daysForward = ChronoUnit.DAYS.between(ANCHOR_DATE, interestingDate);
|
||||
expectedWeather.add(dayOffsetToWeather.apply(daysForward));
|
||||
}
|
||||
return expectedWeather;
|
||||
}
|
||||
```
|
||||
|
||||
Notes on the end state:
|
||||
|
||||
- Key move: `scorer` is used **only inside the lambda**; the extracted method
|
||||
`collectExpectedWeather` has no dependency on `Scorer` at all — all its
|
||||
inputs are parameters.
|
||||
- The public no-arg overload keeps callers unchanged; it injects the
|
||||
"real" weather lambda. The test calls the overload with a pure
|
||||
`daysForward -> true` instead.
|
||||
- The test is pure and glanceable: `alwaysSunny` weather gives
|
||||
`{Strawberry:12, Chocolate:12, Vanilla:17, }` (approved file included).
|
||||
- `scorer.updateSelection()` turned out to be deletable (it only mutates
|
||||
static `Scorer.flavour` state that this method never reads). Leaving it
|
||||
in the lambda is fine for step 3; deleting it is a debrief aha.
|
||||
@@ -1,79 +0,0 @@
|
||||
// Example 3 — Ice cream shop demo of Slice with Lambda (C#)
|
||||
//
|
||||
// Condensed mechanics: https://sammancoaching.org/refactorings/slice.html
|
||||
// (where that page says mock, we use a lambda instead).
|
||||
//
|
||||
// Third repetition, same complexity as ScheduleWatering in WeatherStation.cs
|
||||
// (guard -> one dependency call -> map to enum -> constant tail), so
|
||||
// participants can practice extracting a lambda a second time. The C#
|
||||
// delegate syntax is worth the repetition:
|
||||
//
|
||||
// Func<byte> readTemperature = () => _thermometer.Read();
|
||||
//
|
||||
// Cast:
|
||||
// Thermometer — hardware-ish dependency. Read() returns a raw
|
||||
// byte value (not calibrated, the client decides
|
||||
// what it means).
|
||||
// Warmth — enum. Reading > 25 => Warm, otherwise Cold.
|
||||
// (Sunny/Shady's cousin from WeatherStation.cs.)
|
||||
// ThawAmount — enum. None / Medium / Low.
|
||||
// IceCreamShop — the client. Owns the _thawedToday flag, so the
|
||||
// guard is about client state, not sensor state.
|
||||
|
||||
using System;
|
||||
|
||||
public enum Warmth
|
||||
{
|
||||
Cold,
|
||||
Warm
|
||||
}
|
||||
|
||||
public enum ThawAmount
|
||||
{
|
||||
None,
|
||||
Medium,
|
||||
Low
|
||||
}
|
||||
|
||||
public class Thermometer
|
||||
{
|
||||
private readonly Random _rng = new Random(); // stand-in for the hardware
|
||||
|
||||
public byte Read()
|
||||
{
|
||||
return (byte)_rng.Next(50); // raw sensor value
|
||||
}
|
||||
}
|
||||
|
||||
public class IceCreamShop
|
||||
{
|
||||
private readonly Thermometer _thermometer;
|
||||
private bool _thawedToday; // event-style client state
|
||||
|
||||
public IceCreamShop(Thermometer thermometer)
|
||||
{
|
||||
_thermometer = thermometer;
|
||||
}
|
||||
|
||||
public ThawAmount ThawStock()
|
||||
{
|
||||
if (_thawedToday) // guard: client state, stays
|
||||
{
|
||||
return ThawAmount.None;
|
||||
}
|
||||
|
||||
_thawedToday = true; // set only after thawing
|
||||
byte temperature = _thermometer.Read(); // middle: unwanted dependency
|
||||
Warmth warmth = ToWarmth(temperature);
|
||||
if (warmth == Warmth.Warm) // tail: complex behaviour below
|
||||
{ // the extraction point
|
||||
return ThawAmount.Medium;
|
||||
}
|
||||
return ThawAmount.Low;
|
||||
}
|
||||
|
||||
private static Warmth ToWarmth(byte temperature)
|
||||
{
|
||||
return temperature > 25 ? Warmth.Warm : Warmth.Cold;
|
||||
}
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
// Example 1 — Metaphor-free demo of Slice with Lambda (C#)
|
||||
//
|
||||
// Condensed mechanics: https://sammancoaching.org/refactorings/slice.html
|
||||
// (where that page says mock, we use a lambda instead).
|
||||
//
|
||||
// Shape of the method (per zeroth-draft-of-refactoring-pattern.md):
|
||||
//
|
||||
// guard clauses — must run first
|
||||
// middle: dependency — one call on an object we don't want in the
|
||||
// extracted method
|
||||
// tail: depends on b — uses the value the dependency produced
|
||||
//
|
||||
// Deliberately NOT a loop: this is the smallest method that shows the
|
||||
// mechanics. The dependency both validates its precondition (using the
|
||||
// parameter a) and produces b — one expression, one thing to move into
|
||||
// the lambda.
|
||||
//
|
||||
// Unwanted dependency: SomeDependency — might throw when its precondition
|
||||
// on a is not satisfied, and returns an unpredictable value. An approval
|
||||
// test on Calculate is flaky until the lambda is stubbed.
|
||||
|
||||
using System;
|
||||
|
||||
public class Calculator
|
||||
{
|
||||
private readonly SomeDependency _someDependency;
|
||||
|
||||
public Calculator(SomeDependency someDependency)
|
||||
{
|
||||
_someDependency = someDependency;
|
||||
}
|
||||
|
||||
public int Calculate(int a)
|
||||
{
|
||||
if (a < 0) return 0; // guard clause: stays
|
||||
int b = _someDependency.mightThrowWhenPreconditionNotSatisfied(a); // middle: unwanted dependency
|
||||
int c = a + b; // tail: depends on b
|
||||
return c * 2;
|
||||
}
|
||||
}
|
||||
|
||||
public class SomeDependency
|
||||
{
|
||||
private readonly Random _rng = new Random(); // stand-in for unpredictable computation
|
||||
|
||||
public int mightThrowWhenPreconditionNotSatisfied(int a)
|
||||
{
|
||||
if (a > 99)
|
||||
{
|
||||
throw new InvalidOperationException("precondition not satisfied");
|
||||
}
|
||||
return _rng.Next(100);
|
||||
}
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
# Slice with Lambda — C# demo examples
|
||||
|
||||
Three C# demo examples for the **Slice with Lambda** refactoring.
|
||||
Condensed mechanics: https://www.sammancoaching.org/refactorings/slice.html
|
||||
(where that page says mock, we use a lambda instead).
|
||||
Full Java walkthrough: `../../storyboard/storyboard.md`.
|
||||
Slides: `~/qwan/slides-rjs/decks/refactoring/slice-with-lambda/slides.md`, served at http://localhost:9001/#/refactoring/slice-with-lambda.
|
||||
|
||||
## The three examples
|
||||
|
||||
| file | guard (client state) | unwanted dependency | lambda | tail |
|
||||
|---|---|---|---|---|
|
||||
| `MetaphorFree.cs` | `if (a < 0) return 0;` | `_someDependency.mightThrowWhenPreconditionNotSatisfied(a)` | `Func<int, int> readB = a => ...` | `int c = a + b; return c * 2;` |
|
||||
| `WeatherStation.cs` | `if (_wateredToday)` | `_lightSensor.Read()` (byte) | `Func<byte> readLight = () => ...` | sunny -> `WateringDuration.Medium`, shady -> `Short` (reading > 128 => `SunExposure.Sunny`) |
|
||||
| `IceCreamShop.cs` | `if (_thawedToday)` | `_thermometer.Read()` (byte) | `Func<byte> readTemperature = () => ...` | warm -> `ThawAmount.Medium`, cold -> `Low` (temperature > 25 => `Warmth.Warm`) |
|
||||
|
||||
In all three, the guard reads client state (a parameter or a flag the client
|
||||
owns), so it stays in the extracted method. Only the dependency call moves
|
||||
into the lambda.
|
||||
|
||||
## The ramp
|
||||
|
||||
`MetaphorFree.cs` is the smallest possible shape: no loop, one dependency
|
||||
call, a tail of one arithmetic step. It carries the capture-to-parameter
|
||||
lesson: the lambda takes `a` as its parameter (`readB(a)`) because a lambda
|
||||
moved up into the function head cannot capture method parameters. Its
|
||||
step-4 stub tests include a throwing stub.
|
||||
|
||||
`WeatherStation.cs` and `IceCreamShop.cs` are same-shape domain
|
||||
repetitions — guard, one dependency call, map to enum, constant tail — so
|
||||
participants can practice extracting the lambda twice. The C# delegate
|
||||
syntax (`Func<byte>`) is worth the repetition.
|
||||
|
||||
## Same three regions every time
|
||||
|
||||
Each example deliberately has the same three regions:
|
||||
|
||||
- **before**: guard clauses
|
||||
- **middle**: one dependency call
|
||||
- **after**: tail that depends on the value the dependency produced
|
||||
|
||||
Matching the pattern: extract the dependency-using code into a lambda,
|
||||
slide the definition to the top, extract the method below it, pass the
|
||||
lambda as parameter, test with pure lambda stubs.
|
||||
@@ -1,12 +0,0 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<!-- solution/ has its own project (same class names) -->
|
||||
<Compile Remove="solution/**/*.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -1,6 +0,0 @@
|
||||
<Solution>
|
||||
<Folder Name="/solution/">
|
||||
<Project Path="solution/SliceWithLambdaSolution.csproj" />
|
||||
</Folder>
|
||||
<Project Path="SliceWithLambda.csproj" />
|
||||
</Solution>
|
||||
@@ -1,71 +0,0 @@
|
||||
// Example 2 — Weather Station domain demo of Slice with Lambda (C#)
|
||||
//
|
||||
// Condensed mechanics: https://sammancoaching.org/refactorings/slice.html
|
||||
// (where that page says mock, we use a lambda instead).
|
||||
//
|
||||
// Cast (Head First Design Patterns Weather Station, reduced):
|
||||
// LightSensor — hardware-ish dependency. Read() returns a byte.
|
||||
// SunExposure — enum. Reading > 128 => Sunny, otherwise Shady.
|
||||
// WateringDuration — enum. None / Medium / Short.
|
||||
// (Long is YAGNI until the for-loop version of this
|
||||
// exercise exists.)
|
||||
// SprinklerController — the client. Owns the _wateredToday flag, so the
|
||||
// guard is about client state, not sensor state.
|
||||
|
||||
using System;
|
||||
|
||||
public enum SunExposure
|
||||
{
|
||||
Shady,
|
||||
Sunny
|
||||
}
|
||||
|
||||
public enum WateringDuration
|
||||
{
|
||||
None,
|
||||
Medium,
|
||||
Short
|
||||
}
|
||||
|
||||
public class LightSensor
|
||||
{
|
||||
private readonly Random _rng = new Random(); // stand-in for hardware noise
|
||||
|
||||
public byte Read()
|
||||
{
|
||||
return (byte)_rng.Next(256);
|
||||
}
|
||||
}
|
||||
|
||||
public class SprinklerController
|
||||
{
|
||||
private readonly LightSensor _lightSensor;
|
||||
private bool _wateredToday; // event-style client state
|
||||
|
||||
public SprinklerController(LightSensor lightSensor)
|
||||
{
|
||||
_lightSensor = lightSensor;
|
||||
}
|
||||
|
||||
public WateringDuration ScheduleWatering()
|
||||
{
|
||||
if (_wateredToday) // guard: client state, stays
|
||||
{
|
||||
return WateringDuration.None;
|
||||
}
|
||||
|
||||
_wateredToday = true; // set only after watering
|
||||
byte reading = _lightSensor.Read(); // middle: unwanted dependency
|
||||
SunExposure exposure = ToSunExposure(reading);
|
||||
if (exposure == SunExposure.Sunny) // tail: complex behaviour below
|
||||
{ // the extraction point
|
||||
return WateringDuration.Medium;
|
||||
}
|
||||
return WateringDuration.Short;
|
||||
}
|
||||
|
||||
private static SunExposure ToSunExposure(byte reading)
|
||||
{
|
||||
return reading > 128 ? SunExposure.Sunny : SunExposure.Shady;
|
||||
}
|
||||
}
|
||||
@@ -1,72 +0,0 @@
|
||||
// Example 3 solution — Ice cream shop demo of Slice with Lambda (C#), after
|
||||
//
|
||||
// Lambda extracted (step 1), slid to the top (step 2), method extracted
|
||||
// below the guard (step 3). The constructor now takes the lambda directly,
|
||||
// so tests construct with new IceCreamShop(() => 30).
|
||||
//
|
||||
// Condensed mechanics: https://sammancoaching.org/refactorings/slice.html
|
||||
// (where that page says mock, we use a lambda instead).
|
||||
|
||||
using System;
|
||||
|
||||
public enum Warmth
|
||||
{
|
||||
Cold,
|
||||
Warm
|
||||
}
|
||||
|
||||
public enum ThawAmount
|
||||
{
|
||||
None,
|
||||
Medium,
|
||||
Low
|
||||
}
|
||||
|
||||
public class Thermometer
|
||||
{
|
||||
private readonly Random _rng = new Random(); // stand-in for the hardware
|
||||
|
||||
public byte Read()
|
||||
{
|
||||
return (byte)_rng.Next(50); // raw sensor value
|
||||
}
|
||||
}
|
||||
|
||||
public class IceCreamShop
|
||||
{
|
||||
private readonly Func<byte> _readTemperature; // the thermometer, as a lambda
|
||||
private bool _thawedToday; // event-style client state
|
||||
|
||||
public IceCreamShop(Func<byte> readTemperature)
|
||||
{
|
||||
_readTemperature = readTemperature;
|
||||
}
|
||||
|
||||
public ThawAmount ThawStock()
|
||||
{
|
||||
Func<byte> readTemperature = () => _readTemperature(); // lambda at the top
|
||||
return ThawStockTail(readTemperature); // pass the lambda on
|
||||
}
|
||||
|
||||
private ThawAmount ThawStockTail(Func<byte> readTemperature)
|
||||
{
|
||||
if (_thawedToday) // guard: client state, stays
|
||||
{
|
||||
return ThawAmount.None;
|
||||
}
|
||||
|
||||
_thawedToday = true; // set only after thawing
|
||||
byte temperature = readTemperature(); // middle: the stubbed lambda
|
||||
Warmth warmth = ToWarmth(temperature);
|
||||
if (warmth == Warmth.Warm) // tail: complex behaviour below
|
||||
{ // the extraction point
|
||||
return ThawAmount.Medium;
|
||||
}
|
||||
return ThawAmount.Low;
|
||||
}
|
||||
|
||||
private static Warmth ToWarmth(byte temperature)
|
||||
{
|
||||
return temperature > 25 ? Warmth.Warm : Warmth.Cold;
|
||||
}
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
// Example 1 solution — Metaphor-free demo of Slice with Lambda (C#), after
|
||||
//
|
||||
// Lambda extracted (step 1), slid to the top (step 2), method extracted
|
||||
// below the guard (step 3), overload seam added (step 3b), b inlined
|
||||
// (final cleanup).
|
||||
//
|
||||
// Condensed mechanics: https://sammancoaching.org/refactorings/slice.html
|
||||
// (where that page says mock, we use a lambda instead).
|
||||
|
||||
using System;
|
||||
|
||||
public class Calculator
|
||||
{
|
||||
private readonly SomeDependency _someDependency;
|
||||
|
||||
public Calculator(SomeDependency someDependency)
|
||||
{
|
||||
_someDependency = someDependency;
|
||||
}
|
||||
|
||||
public int Calculate(int a) // overload: callers unchanged
|
||||
{
|
||||
return Calculate(a, a =>
|
||||
_someDependency.mightThrowWhenPreconditionNotSatisfied(a));
|
||||
} // the real λ, baked into the head
|
||||
|
||||
public int Calculate(int a, Func<int, int> readB) // the seam: λ is a parameter
|
||||
{
|
||||
if (a < 0) return 0; // guard clause: stays
|
||||
return CalculateTail(a, readB);
|
||||
}
|
||||
|
||||
private int CalculateTail(int a, Func<int, int> readB)
|
||||
{
|
||||
int c = a + readB(a); // b inlined
|
||||
return c * 2;
|
||||
}
|
||||
}
|
||||
|
||||
public class SomeDependency
|
||||
{
|
||||
private readonly Random _rng = new Random(); // stand-in for unpredictable computation
|
||||
|
||||
public int mightThrowWhenPreconditionNotSatisfied(int a)
|
||||
{
|
||||
if (a > 99)
|
||||
{
|
||||
throw new InvalidOperationException("precondition not satisfied");
|
||||
}
|
||||
return _rng.Next(100);
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
|
||||
<PackageReference Include="xunit" Version="2.9.2" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -1,82 +0,0 @@
|
||||
// Tests for the three Slice-with-Lambda solutions — step 4: the lambda as a stub.
|
||||
//
|
||||
// Condensed mechanics: https://sammancoaching.org/refactorings/slice.html
|
||||
// (where that page says mock, we use a lambda instead).
|
||||
|
||||
using System;
|
||||
using Xunit;
|
||||
|
||||
public class SliceWithLambdaTests
|
||||
{
|
||||
// --- Example 1: metaphor-free (Calculator) ---
|
||||
|
||||
[Fact]
|
||||
public void Guards_against_negative_input()
|
||||
{
|
||||
var calc = new Calculator(new SomeDependency()); // the real, flaky one...
|
||||
Assert.Equal(0, calc.Calculate(-1, a => 42)); // ...never asked: stub not invoked
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Throws_when_precondition_not_satisfied()
|
||||
{
|
||||
var calc = new Calculator(new SomeDependency());
|
||||
Assert.Throws<InvalidOperationException>(
|
||||
() => calc.Calculate(7, a => throw new InvalidOperationException()));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Combines_input_and_the_stubbed_value()
|
||||
{
|
||||
var calc = new Calculator(new SomeDependency());
|
||||
Assert.Equal(84, calc.Calculate(0, a => 42)); // stub through the seam
|
||||
}
|
||||
|
||||
// --- Example 2: Weather Station (SprinklerController) ---
|
||||
|
||||
[Fact]
|
||||
public void Skips_watering_when_already_watered()
|
||||
{
|
||||
var controller = new SprinklerController(() => 200); // pure lambda: no sensor
|
||||
controller.ScheduleWatering(); // water once...
|
||||
Assert.Equal(WateringDuration.None, controller.ScheduleWatering());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Waters_longer_in_full_sun()
|
||||
{
|
||||
var controller = new SprinklerController(() => 200); // sunny: > 128
|
||||
Assert.Equal(WateringDuration.Medium, controller.ScheduleWatering());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Waters_less_in_the_shade()
|
||||
{
|
||||
var controller = new SprinklerController(() => 10); // shady: ≤ 128
|
||||
Assert.Equal(WateringDuration.Short, controller.ScheduleWatering());
|
||||
}
|
||||
|
||||
// --- Example 3: Ice Cream Shop (IceCreamShop) ---
|
||||
|
||||
[Fact]
|
||||
public void Skips_thawing_when_already_thawed()
|
||||
{
|
||||
var shop = new IceCreamShop(() => 30); // pure lambda: no freezer
|
||||
shop.ThawStock(); // thaw once...
|
||||
Assert.Equal(ThawAmount.None, shop.ThawStock());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Thaws_more_when_warm()
|
||||
{
|
||||
var shop = new IceCreamShop(() => 30); // warm: > 25
|
||||
Assert.Equal(ThawAmount.Medium, shop.ThawStock());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Thaws_less_when_cold()
|
||||
{
|
||||
var shop = new IceCreamShop(() => 5); // cold: ≤ 25
|
||||
Assert.Equal(ThawAmount.Low, shop.ThawStock());
|
||||
}
|
||||
}
|
||||
@@ -1,72 +0,0 @@
|
||||
// Example 2 solution — Weather Station domain demo of Slice with Lambda (C#), after
|
||||
//
|
||||
// Lambda extracted (step 1), slid to the top (step 2), method extracted
|
||||
// below the guard (step 3). The constructor now takes the lambda directly,
|
||||
// so tests construct with new SprinklerController(() => 200).
|
||||
//
|
||||
// Condensed mechanics: https://sammancoaching.org/refactorings/slice.html
|
||||
// (where that page says mock, we use a lambda instead).
|
||||
|
||||
using System;
|
||||
|
||||
public enum SunExposure
|
||||
{
|
||||
Shady,
|
||||
Sunny
|
||||
}
|
||||
|
||||
public enum WateringDuration
|
||||
{
|
||||
None,
|
||||
Medium,
|
||||
Short
|
||||
}
|
||||
|
||||
public class LightSensor
|
||||
{
|
||||
private readonly Random _rng = new Random(); // stand-in for hardware noise
|
||||
|
||||
public byte Read()
|
||||
{
|
||||
return (byte)_rng.Next(256);
|
||||
}
|
||||
}
|
||||
|
||||
public class SprinklerController
|
||||
{
|
||||
private readonly Func<byte> _readLight; // the sensor, as a lambda
|
||||
private bool _wateredToday; // event-style client state
|
||||
|
||||
public SprinklerController(Func<byte> readLight)
|
||||
{
|
||||
_readLight = readLight;
|
||||
}
|
||||
|
||||
public WateringDuration ScheduleWatering()
|
||||
{
|
||||
Func<byte> readLight = () => _readLight(); // lambda definition at the top
|
||||
return ScheduleWateringTail(readLight); // pass the lambda on
|
||||
}
|
||||
|
||||
private WateringDuration ScheduleWateringTail(Func<byte> readLight)
|
||||
{
|
||||
if (_wateredToday) // guard: client state, stays
|
||||
{
|
||||
return WateringDuration.None;
|
||||
}
|
||||
|
||||
_wateredToday = true; // set only after watering
|
||||
byte reading = readLight(); // middle: the stubbed lambda
|
||||
SunExposure exposure = ToSunExposure(reading);
|
||||
if (exposure == SunExposure.Sunny) // tail: complex behaviour below
|
||||
{ // the extraction point
|
||||
return WateringDuration.Medium;
|
||||
}
|
||||
return WateringDuration.Short;
|
||||
}
|
||||
|
||||
private static SunExposure ToSunExposure(byte reading)
|
||||
{
|
||||
return reading > 128 ? SunExposure.Sunny : SunExposure.Shady;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
target/
|
||||
*.received.txt
|
||||
*.approved.txt
|
||||
.idea/
|
||||
*.iml
|
||||
@@ -1,37 +0,0 @@
|
||||
# IceCreamForecasting — Slice with Lambda (exercise)
|
||||
|
||||
Pre-factored variant of [IceCreamForecasting-Refactoring-Kata](https://github.com/emilybache/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:
|
||||
|
||||
1. First get any test running that calls `longRangeForecast()` —
|
||||
it will fail or be flaky, because of the dependency on `Scorer`.
|
||||
2. 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**.
|
||||
3. 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 `scorer` in 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 `Scorer` is unknown?
|
||||
@@ -1,31 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>codingdojo</groupId>
|
||||
<artifactId>icecream-split-loop-lambda-exercise</artifactId>
|
||||
<version>1.0.0</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>5.10.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.approvaltests</groupId>
|
||||
<artifactId>approvaltests</artifactId>
|
||||
<version>18.6.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -1,7 +0,0 @@
|
||||
package codingdojo;
|
||||
|
||||
public enum IceCream {
|
||||
Strawberry,
|
||||
Chocolate,
|
||||
Vanilla;
|
||||
}
|
||||
-59
@@ -1,59 +0,0 @@
|
||||
package codingdojo;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Pre-factored variant of LongRangeForecasts from
|
||||
* https://github.com/emilybache/IceCreamForecasting-Refactoring-Kata
|
||||
*
|
||||
* Differences from the kata original (deliberate, for the learning hour):
|
||||
* - The Q1/Q4 branch was removed. It was dead code anyway: the unconditional
|
||||
* loop right after it overwrites every value it sets. (Splitting loops
|
||||
* sometimes reveals things like this.)
|
||||
* - The hardcoded list of interesting dates is now a constant, DATES, and the
|
||||
* anchor instant is ANCHOR_DATE. (Turning the dates into a parameter is a
|
||||
* *later* refactoring — not part of this exercise.)
|
||||
*
|
||||
* Your task: see README.md
|
||||
*/
|
||||
public class LongRangeForecasts {
|
||||
|
||||
private static final Instant ANCHOR_DATE = Instant.parse("2023-04-26T14:00:00.000-07:00");
|
||||
private static final List<Instant> DATES = List.of(
|
||||
Instant.parse("2023-05-01T14:00:00.000-07:00"),
|
||||
Instant.parse("2023-05-18T14:00:00.000-07:00"),
|
||||
Instant.parse("2023-06-05T14:00:00.000-07:00"),
|
||||
Instant.parse("2023-06-23T14:00:00.000-07:00")
|
||||
);
|
||||
|
||||
private final Scorer scorer = new Scorer();
|
||||
|
||||
public Map<IceCream, Integer> longRangeForecast() {
|
||||
|
||||
var result = new HashMap<IceCream, Integer>();
|
||||
for (IceCream flavour : IceCream.values()) {
|
||||
result.put(flavour, 0);
|
||||
}
|
||||
List<Boolean> expectedWeather = new ArrayList<>();
|
||||
for (Instant interestingDate : DATES) {
|
||||
scorer.updateSelection();
|
||||
long daysForward = ChronoUnit.DAYS.between(ANCHOR_DATE, interestingDate);
|
||||
Boolean weather = scorer.lookupWeather(daysForward);
|
||||
expectedWeather.add(weather);
|
||||
}
|
||||
|
||||
for (IceCream flavour : result.keySet()) {
|
||||
result.put(flavour, result.get(flavour) + 10);
|
||||
var sunnyHolidays = expectedWeather.stream().filter(s -> s).count();
|
||||
if (sunnyHolidays > 2 && flavour == IceCream.Vanilla) {
|
||||
result.put(flavour, result.get(flavour) + 5);
|
||||
}
|
||||
if (sunnyHolidays > 1 && expectedWeather.get(1)) {
|
||||
result.put(flavour, result.get(flavour) + 2);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
package codingdojo;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* Trimmed version of the kata's Scorer: only the parts LongRangeForecasts uses.
|
||||
* lookupWeather stands in for an API call to a weather service — in the real
|
||||
* system this is the "unwanted dependency".
|
||||
*/
|
||||
public class Scorer {
|
||||
|
||||
public static IceCream flavour = null;
|
||||
|
||||
public int getScore() {
|
||||
boolean sunnyToday = lookupWeather();
|
||||
if (flavour == null) {
|
||||
return -1;
|
||||
}
|
||||
switch (flavour) {
|
||||
case Strawberry:
|
||||
return sunnyToday ? 10 : 5;
|
||||
case Chocolate:
|
||||
return 6;
|
||||
case Vanilla:
|
||||
return sunnyToday ? 7 : 5;
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean lookupWeather() {
|
||||
return new Random().nextBoolean();
|
||||
}
|
||||
|
||||
public boolean lookupWeather(long daysForward) {
|
||||
// placeholder implementation - real version would make API call to weather service
|
||||
return new Random().nextBoolean();
|
||||
}
|
||||
|
||||
public void updateSelection() {
|
||||
// placeholder implementation - real version would use machine learning to predict sales
|
||||
var score = getScore();
|
||||
if (score > 5) {
|
||||
var randomizer = new Random();
|
||||
IceCream[] allFlavours = IceCream.values();
|
||||
flavour = allFlavours[randomizer.nextInt(allFlavours.length)];
|
||||
}
|
||||
}
|
||||
}
|
||||
-32
@@ -1,32 +0,0 @@
|
||||
package codingdojo;
|
||||
|
||||
import org.approvaltests.Approvals;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
public class LongRangeForecastsTest {
|
||||
|
||||
@Test
|
||||
void longRangeForecast() {
|
||||
var actual = new LongRangeForecasts().longRangeForecast();
|
||||
Approvals.verify(printIceCreamForecast(actual));
|
||||
}
|
||||
|
||||
private static String printIceCreamForecast(Map<IceCream, Integer> actual) {
|
||||
var printedResult = new StringBuilder();
|
||||
printedResult.append("{");
|
||||
var sortedKeys = new ArrayList<>(actual.keySet());
|
||||
Collections.sort(sortedKeys);
|
||||
for (IceCream key : sortedKeys) {
|
||||
printedResult.append(key);
|
||||
printedResult.append(":");
|
||||
printedResult.append(actual.get(key));
|
||||
printedResult.append(", ");
|
||||
}
|
||||
printedResult.append("}");
|
||||
return printedResult.toString();
|
||||
}
|
||||
}
|
||||
@@ -1,253 +0,0 @@
|
||||
# Rehearsal of the exercise with coding agents — full summary
|
||||
|
||||
**Date:** 2026-09-27 · **Harness:** `tools/rehearse/` · **Raw per-run artifacts:** [`rehearsal-runs/`](./)
|
||||
· **Exercise:** Slice with Lambda (IceCreamForecasting kata, `LongRangeForecasts`)
|
||||
|
||||
Six scripted sub-agent runs did the refactoring under different tool rules and
|
||||
two models. Each run got a fresh workspace, a shared AGENTS.md (identical
|
||||
design instructions), a 10-minute wall-clock cap, then measurement from the pi
|
||||
session log and a deterministic gate: the reference solution's test + approved
|
||||
file (`{Strawberry:12, Chocolate:12, Vanilla:17, }` with an `alwaysSunny`
|
||||
lambda) dropped in and `mvn test` executed.
|
||||
|
||||
Variants:
|
||||
|
||||
- **A** — `java-refactor` CLI mandated (text edits forbidden except the
|
||||
introduce-lambda step and a fallback for step 3)
|
||||
- **B** — plain pi tools (read/bash/edit), CLI absent from PATH
|
||||
- **C** — CLI available, use *optional*, with a 6-line intro to the relevant
|
||||
subcommands
|
||||
- **+Q** — same rules on entrim's Qwen/Qwen3.6-35B-A3B instead of
|
||||
openrouter's z-ai/glm-5.3-flash
|
||||
|
||||
## Results
|
||||
|
||||
| Run | Model | Rules | Wall clock | Turns | Tool calls | Tokens (sum) | Peak context | Gate |
|
||||
|-----|-------|-------|-----------:|------:|-----------:|-------------:|-------------:|------|
|
||||
| A | glm-5.3-flash | CLI mandated | 4:06 | 27 | 27 (19 bash, 4 read, 4 edit) | 418,718 | 23.0 K | **PASS** |
|
||||
| B | glm-5.3-flash | plain tools | 0:39 | 10 | 10 (5 bash, 1 read, 4 edit) | 62,196 | 7.6 K | **PASS** |
|
||||
| C | glm-5.3-flash | CLI optional | 2:37 | 23 | 23 (15 bash, 4 read, 4 edit) | 150,805 | 13.5 K | **PASS** |
|
||||
| A+Q | Qwen3.6-35B | CLI mandated | 2:07 | 27 | 28 (17 bash, 8 read, 3 edit) | 352,812 | 22.6 K | **FAIL** |
|
||||
| B+Q | Qwen3.6-35B | plain tools | 1:13 | 17 | 17 (7 bash, 5 read, 5 edit) | 260,052 | 29.8 K | **PASS** |
|
||||
| C+Q | Qwen3.6-35B | CLI optional | 1:12 | 17 | 17 (7 bash, 5 read, 5 edit) | 149,663 | 16.1 K | **PASS** |
|
||||
|
||||
Matrix (n=1 per cell — indicative, not statistical):
|
||||
|
||||
| | glm-5.3-flash (strong) | Qwen3.6-35B (weak) |
|
||||
|---|---|---|
|
||||
| **CLI mandated (A)** | PASS | **FAIL** |
|
||||
| **CLI optional (C)** | PASS — used it surgically | PASS — ignored it entirely |
|
||||
| **Plain tools (B)** | PASS | PASS |
|
||||
|
||||
Measurement notes: token sums are per-request usage added over the whole
|
||||
session; entrim reports no prompt-cache reads, so +Q runs re-send full context
|
||||
every turn (their sums are inflated relative to openrouter's cached runs —
|
||||
e.g. B+Q's 260 K is 17 requests growing 2.3 K → 29.5 K; peak single request
|
||||
~30 K, far under the 262 K limit). Peak context = largest single request.
|
||||
Cross-provider, compare wall clock / turns / tool calls, not raw token sums.
|
||||
|
||||
## Headlines
|
||||
|
||||
1. **Mandating the tool tripled the strong model's cost** (A vs B: 4:06 vs
|
||||
0:39, 419 K vs 62 K tokens) for an equally-passing result. The cost is not
|
||||
the refactoring operations — it is the ceremony around them plus one
|
||||
genuine coverage gap (see "Why the CLI regime needs so many turns").
|
||||
2. **"Available, introduced, not mandated" is the safe default.** C never did
|
||||
worse than the alternative on either model: the strong model used the CLI
|
||||
surgically (extract dry-run → apply) and edited manually elsewhere; the
|
||||
weak model ignored the CLI entirely and simply edited.
|
||||
3. **The weak model failed only in the mandated regime.** A+Q produced broken
|
||||
code and rationalized the failing tests as "expected". The same model
|
||||
passed in B+Q and C+Q with materially correct end states.
|
||||
|
||||
## The drift, documented
|
||||
|
||||
The exercise's step 1 says: *introduce a lambda for the scorer-dependent code
|
||||
and move its definition to the top of the method, leaving the invocation where
|
||||
it is.* The scorer-dependent code is **both** scorer calls — `updateSelection()`
|
||||
and `lookupWeather()`:
|
||||
|
||||
```java
|
||||
// PRISTINE (exercise start) — LongRangeForecasts.java, lines 28–35
|
||||
List<Boolean> expectedWeather = new ArrayList<>();
|
||||
for (Instant interestingDate : interestingDates) {
|
||||
scorer.updateSelection(); // ← scorer-dependent, void side effect
|
||||
var forecastDate = Instant.parse("2023-04-26T14:00:00.000-07:00");
|
||||
long daysForward = ChronoUnit.DAYS.between(forecastDate, interestingDate);
|
||||
Boolean lookupWeather = scorer.lookupWeather(daysForward); // ← scorer-dependent, value
|
||||
expectedWeather.add(lookupWeather);
|
||||
}
|
||||
```
|
||||
|
||||
After a correct step 1+2, the extracted method is **static and pure** — its
|
||||
only "dependency" is the lambda parameter, and the scorer calls live inside
|
||||
the lambda definition in the no-arg overload (reference solution):
|
||||
|
||||
```java
|
||||
// CORRECT — reference solution
|
||||
public Map<IceCream, Integer> longRangeForecast() {
|
||||
return longRangeForecast(daysForward -> {
|
||||
scorer.updateSelection(); // scorer-dependent code, inside the lambda
|
||||
return scorer.lookupWeather(daysForward);
|
||||
});
|
||||
}
|
||||
|
||||
private static List<Boolean> collectExpectedWeather(LongFunction<Boolean> dayOffsetToWeather) {
|
||||
var expectedWeather = new ArrayList<Boolean>();
|
||||
for (Instant interestingDate : DATES) {
|
||||
long daysForward = ChronoUnit.DAYS.between(ANCHOR_DATE, interestingDate);
|
||||
expectedWeather.add(dayOffsetToWeather.apply(daysForward)); // loop is scorer-free
|
||||
}
|
||||
return expectedWeather;
|
||||
}
|
||||
```
|
||||
|
||||
The **drift** (A+Q and C+Q both): lambda-izing only the value-producing call
|
||||
and leaving `updateSelection()` as loop machinery, so extraction carried the
|
||||
`scorer` field dependency into the new method:
|
||||
|
||||
```java
|
||||
// DRIFTED — A+Q / C+Q end state
|
||||
private List<Boolean> collectExpectedWeather(List<Instant> dates, LongFunction<Boolean> dayOffsetToWeather) {
|
||||
List<Boolean> expectedWeather = new ArrayList<>();
|
||||
for (Instant date : dates) {
|
||||
scorer.updateSelection(); // ← LEFT BEHIND: method still depends on the field
|
||||
var forecastDate = Instant.parse("2023-04-26T14:00:00.000-07:00");
|
||||
long daysForward = ChronoUnit.DAYS.between(forecastDate, date);
|
||||
expectedWeather.add(dayOffsetToWeather.apply(daysForward));
|
||||
}
|
||||
return expectedWeather;
|
||||
}
|
||||
```
|
||||
|
||||
Consequences of the drift: `collectExpectedWeather` is neither static nor
|
||||
pure, step 4 (moving it to another class) would be blocked, and the "unwanted
|
||||
external dependency" was never actually severed — the split is cosmetic.
|
||||
|
||||
Why the gate still passed: the test injects its own pure lambda, and the stray
|
||||
`updateSelection()` only mutates the static `Scorer.flavour` field, which
|
||||
nothing in the forecast reads (`lookupWeather(daysForward)` ignores it; the
|
||||
field's real consumer is the unrelated `DailyForecasts.salesForecast()`).
|
||||
**PASS ≠ drift-free** — the deterministic gate verifies behavior, not design
|
||||
intent. Catch it mechanically: `grep -n scorer <extracted-method>` must return
|
||||
nothing, or simply try to make the extracted method `static` (it won't
|
||||
compile if the field reference remains).
|
||||
|
||||
### The debrief aha hiding in this line
|
||||
|
||||
`updateSelection()` randomly re-rolls `Scorer.flavour` (see `Scorer.java:51–59`)
|
||||
— state that belongs to the *daily* forecast, which is the only real consumer
|
||||
(`DailyForecasts.java:28–30`, copy-paste residue into the long-range loop).
|
||||
Once the loop is split and the lambda extracted, it becomes visible that
|
||||
`updateSelection()`'s result is never used — **it is deletable outright**, and
|
||||
the approved file does not change (listed in `solution/README.md`). The
|
||||
refactoring does not just hide the dependency behind a lambda; it exposes that
|
||||
one of the two scorer calls was dead all along.
|
||||
|
||||
## A+Q's failure anatomy (the mandated-CLI run)
|
||||
|
||||
Three stacked problems, all verified from the session log:
|
||||
|
||||
1. **Over-deletion (the actual gate failure).** Removing `String quarter`, it
|
||||
deleted BOTH the dead Q1/Q4 branch AND the flavour-reset loop in one edit.
|
||||
The reset loop is what populates the result map → forecast returns `{}` →
|
||||
`expected {Strawberry:12, Chocolate:12, Vanilla:17} but was <{}>`. The
|
||||
reference solution drops the Q1/Q4 branch too — but keeps the reset loop.
|
||||
Silent failure: compiles and runs, just semantically empty.
|
||||
2. **It never saw its final test behavior.** Its last in-run `mvn -q test`
|
||||
(piped through `head -30`) hit a stale-bytecode artifact — incremental
|
||||
compilation kept the old test class calling the removed
|
||||
`longRangeForecast(String)` — throwing an `Error` at the call site whose
|
||||
exception name scrolled off the pipe. It concluded: *"The test fails
|
||||
because the old longRangeForecast(String) signature no longer exists —
|
||||
which is expected per the task instructions."* The AGENTS.md's "tests may
|
||||
fail — that is expected: the code must still compile" gave it a ready-made
|
||||
rationalization to stop on red.
|
||||
3. **The flaky pristine test camouflaged everything.** `Scorer` is random, so
|
||||
mid-run assertion failures looked like expected noise; at one point its
|
||||
`received.txt` even showed `{Strawberry:12, Chocolate:12, Vanilla:17}` — a
|
||||
lucky random roll coincidentally equal to the solution's approved output,
|
||||
sitting in the tree while the real behavior was `{}`.
|
||||
|
||||
## Why the mandated CLI regime needs so many turns (Run A trace)
|
||||
|
||||
Of A's 27 turns: ~8 discovery (reads, `--help` ×2, backup, coordinate
|
||||
computation via `cat -n`), ~8 CLI attempts (2× `extract`, 2×
|
||||
`decompose-conditional`, `remove-param`, `introduce-param` — the last four all
|
||||
hunting in vain for a subcommand that can turn `longRangeForecast(String)` into
|
||||
`longRangeForecast(LongFunction<Boolean>)` + overload), 3 manual edits, ~6
|
||||
verify rounds (`mvn` ×5 + final diff). The point-based API forces a
|
||||
read → compute-coordinates → invoke → confirm cycle per operation, and every
|
||||
operation is a bash round-trip. Run B does it in 10 turns: 4 edits, 3 `mvn`,
|
||||
1 read, 2 housekeeping.
|
||||
|
||||
C shows the fix: with the tool merely *available* plus a 6-line intro, the
|
||||
agent used `extract` (dry-run → one rejected selection → corrected → apply)
|
||||
and edited manually for steps 1 and 3, with no shopping detour — 23 turns,
|
||||
2:37, and the closest-to-reference diff of all six runs (dates hoisted into
|
||||
`collectExpectedWeather`).
|
||||
|
||||
## Tool limitations discovered (worth reflecting into the exercise)
|
||||
|
||||
1. **Lambdas are not first-class.** `introduce-param` cannot introduce a
|
||||
lambda expression as a parameter: applied to the step-3 lambda it *inlined
|
||||
the expression into the call site* — referencing the private `scorer`
|
||||
field from outside the class, non-compiling (observed in A and A+Q; C and
|
||||
C+Q avoided it after reading `--help`). The CLI's lambda support is the
|
||||
reverse direction: method *references* are converted to lambdas when
|
||||
rewriting call sites for parameter changes (golden-master approved), but a
|
||||
lambda expression is never the *subject* of an operation. The original
|
||||
cheat sheet's claim that "add lambda parameter" maps to
|
||||
`introduce-param`/`change-method-signature` was wrong and is corrected in
|
||||
`rehearse-with-agent.md`.
|
||||
2. **`extract` rejects non-statement selections** with a diagnostic
|
||||
("Selection does not contain any complete statements") — correct, safe
|
||||
behavior; C recovered from its one mis-selection in a single retry.
|
||||
3. **`--dry-run` preview → apply is mandatory hygiene.** Both A and A+Q had
|
||||
their broken `introduce-param` results caught by the dry-run diff — the
|
||||
only reason they didn't ship non-compiling code. It also doubles every CLI
|
||||
round trip, part of the mandated-regime cost.
|
||||
4. **Stale-bytecode mvn artifacts can mask real test results** when a public
|
||||
signature is removed without `clean` (the old test class still runs and
|
||||
throws `NoSuchMethodError`-style errors). Agents doing verification-by-mvn
|
||||
should prefer reading the assertion message, not just the exit status.
|
||||
|
||||
## Prompt corrections fed back into the exercise
|
||||
|
||||
- The rehearsal prompt originally omitted the overload step; the reference
|
||||
test (`longRangeForecast(LongFunction<Boolean>)`) cannot compile without it.
|
||||
The prompt now includes step 3 with the **exact target signature** ("do not
|
||||
keep the `String quarter` parameter") — vaguer wording produced
|
||||
`longRangeForecast(String, LongFunction)` in run A's first attempt.
|
||||
- AGENTS.md must say "tests may fail, must still compile" up front — otherwise
|
||||
agents burn turns fighting the flaky pristine test (and A+Q ignored even
|
||||
that, see above).
|
||||
|
||||
## Implications for the learning hour
|
||||
|
||||
- Expect the **tool-availability seam** to be where participants lose time:
|
||||
the CLI covers extract/rename/move, but the *interesting* mechanics of this
|
||||
kata (introduce lambda, slide statement, function-typed parameter) are
|
||||
manual. Make that explicit up front rather than letting participants
|
||||
discover it by trial.
|
||||
- **Time estimate from this rehearsal:** a strong pair with plain editing
|
||||
lands ~40 s of agent effort; with the CLI mandated, ~4 minutes. Humans will
|
||||
be slower on both, but the *ratio* (tool ceremony ≈ 3–6× on a 55-line file)
|
||||
is the honest expectation-setter.
|
||||
- The drift (scorer left in the extracted method) is the canonical mistake to
|
||||
seed the debrief: ask participants to run `grep scorer` on their extracted
|
||||
method. It happened in 2 of 6 runs, on both models, only under manual-edit
|
||||
conditions — humans will hit it too.
|
||||
|
||||
## Reproducing
|
||||
|
||||
```bash
|
||||
bash tools/rehearse/setup.sh # build + install + pristine + smoke test
|
||||
bash tools/rehearse/run-rehearsal.sh A B C # strong model, three regimes
|
||||
bash tools/rehearse/run-rehearsal.sh A+Q B+Q C+Q # weak model (entrim Qwen3.6-35B-A3B)
|
||||
```
|
||||
|
||||
Raw sessions (jsonl, full transcripts) live in `/tmp/rehearsal/runs/<run>/`
|
||||
and are not committed; this directory carries the curated evidence
|
||||
(`measure.txt`, `verify.txt`, `diff.patch`, the exact AGENTS.md and prompt per
|
||||
run, the agent's final report, and the mvn failure tails).
|
||||
@@ -1,31 +0,0 @@
|
||||
# Rehearsal workspace — refactoring task
|
||||
|
||||
You are refactoring `src/main/java/codingdojo/LongRangeForecasts.java` in this
|
||||
Maven project. Run `mvn -q test` after each change (mvn is on your PATH). It may
|
||||
fail — that is expected: the dependency on `Scorer` makes the method untestable —
|
||||
but the code must still *compile*.
|
||||
|
||||
## Goal
|
||||
|
||||
In `LongRangeForecasts.longRangeForecast()`, the loop over `interestingDates`
|
||||
collects weather by calling `scorer` (an unwanted external dependency).
|
||||
|
||||
1. Introduce a lambda for the scorer-dependent code and move its definition to
|
||||
the top of the method, leaving the invocation where it is. (No refactoring
|
||||
tool supports this — do it as a minimal manual edit.)
|
||||
2. Extract the weather-collection loop into a new method `collectExpectedWeather`
|
||||
that takes the lambda as a parameter (suggested type `LongFunction<Boolean>`).
|
||||
3. Add the lambda as a parameter to `longRangeForecast` itself: the signature
|
||||
must become `longRangeForecast(LongFunction<Boolean> dayOffsetToWeather)`,
|
||||
with a public no-arg overload `longRangeForecast()` that delegates and wraps
|
||||
the `scorer` field in a lambda. Do not keep the `String quarter` parameter.
|
||||
(`java-refactor change-method-signature` or `java-refactor introduce-param`
|
||||
may help; fall back to a minimal manual edit if no subcommand can do it.)
|
||||
4. Stop. Report the final diff.
|
||||
|
||||
## Tool rules (variant A)
|
||||
You may only change code by invoking `java-refactor` CLI commands
|
||||
(`java-refactor --help` lists them; it is on your PATH). Never edit `.java`
|
||||
files with text edits — the ONLY exceptions are the introduce-lambda edit
|
||||
(step 1) and, if no `java-refactor` subcommand can accomplish it, a minimal
|
||||
manual edit for step 3.
|
||||
@@ -1,83 +0,0 @@
|
||||
Only in /tmp/rehearsal/runs/A+Q/workspace: AGENTS.md
|
||||
diff -ru '--exclude=target' /tmp/rehearsal/pristine/src/main/java/codingdojo/LongRangeForecasts.java /tmp/rehearsal/runs/A+Q/workspace/src/main/java/codingdojo/LongRangeForecasts.java
|
||||
--- /tmp/rehearsal/pristine/src/main/java/codingdojo/LongRangeForecasts.java 2026-09-27 14:39:01.304467310 +0000
|
||||
+++ /tmp/rehearsal/runs/A+Q/workspace/src/main/java/codingdojo/LongRangeForecasts.java 2026-09-27 14:55:44.276896385 +0000
|
||||
@@ -3,36 +3,27 @@
|
||||
import java.time.Instant;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.*;
|
||||
+import java.util.function.LongFunction;
|
||||
|
||||
public class LongRangeForecasts {
|
||||
|
||||
private final Scorer scorer = new Scorer();
|
||||
|
||||
- public Map<IceCream, Integer> longRangeForecast(String quarter) {
|
||||
+ public Map<IceCream, Integer> longRangeForecast() {
|
||||
+ return longRangeForecast(dayOffset -> scorer.lookupWeather(dayOffset));
|
||||
+ }
|
||||
+
|
||||
+ public Map<IceCream, Integer> longRangeForecast(LongFunction<Boolean> dayOffsetToWeather) {
|
||||
|
||||
var result = new HashMap<IceCream, Integer>();
|
||||
- if (Objects.equals(quarter, "Q1") || Objects.equals(quarter, "Q4")) {
|
||||
- for (IceCream flavour : IceCream.values()) {
|
||||
- result.put(flavour, 5);
|
||||
- }
|
||||
- }
|
||||
- for (IceCream flavour : IceCream.values()) {
|
||||
- result.put(flavour, 0);
|
||||
- }
|
||||
+
|
||||
List<Instant> interestingDates = List.of(
|
||||
Instant.parse("2023-05-01T14:00:00.000-07:00"),
|
||||
Instant.parse("2023-05-18T14:00:00.000-07:00"),
|
||||
Instant.parse("2023-06-05T14:00:00.000-07:00"),
|
||||
Instant.parse("2023-06-23T14:00:00.000-07:00")
|
||||
);
|
||||
- List<Boolean> expectedWeather = new ArrayList<>();
|
||||
- for (Instant interestingDate : interestingDates) {
|
||||
- scorer.updateSelection();
|
||||
- var forecastDate = Instant.parse("2023-04-26T14:00:00.000-07:00");
|
||||
- long daysForward = ChronoUnit.DAYS.between(forecastDate, interestingDate);
|
||||
- Boolean lookupWeather = scorer.lookupWeather(daysForward);
|
||||
- expectedWeather.add(lookupWeather);
|
||||
- }
|
||||
+ List<Boolean> expectedWeather = collectExpectedWeather(interestingDates, dayOffsetToWeather);
|
||||
|
||||
for (IceCream flavour : result.keySet()) {
|
||||
result.put(flavour, result.get(flavour) + 10);
|
||||
@@ -46,4 +37,16 @@
|
||||
}
|
||||
return result;
|
||||
}
|
||||
+
|
||||
+ private List<Boolean> collectExpectedWeather(List<Instant> interestingDates,
|
||||
+ LongFunction<Boolean> dayOffsetToWeather) {
|
||||
+ List<Boolean> result = new ArrayList<>();
|
||||
+ for (Instant interestingDate : interestingDates) {
|
||||
+ scorer.updateSelection();
|
||||
+ var forecastDate = Instant.parse("2023-04-26T14:00:00.000-07:00");
|
||||
+ long daysForward = ChronoUnit.DAYS.between(forecastDate, interestingDate);
|
||||
+ result.add(dayOffsetToWeather.apply(daysForward));
|
||||
+ }
|
||||
+ return result;
|
||||
+ }
|
||||
}
|
||||
diff -ru '--exclude=target' /tmp/rehearsal/pristine/src/test/java/codingdojo/DailyForecastsTest.salesForecast.received.txt /tmp/rehearsal/runs/A+Q/workspace/src/test/java/codingdojo/DailyForecastsTest.salesForecast.received.txt
|
||||
--- /tmp/rehearsal/pristine/src/test/java/codingdojo/DailyForecastsTest.salesForecast.received.txt 2026-09-27 14:39:03.626410227 +0000
|
||||
+++ /tmp/rehearsal/runs/A+Q/workspace/src/test/java/codingdojo/DailyForecastsTest.salesForecast.received.txt 2026-09-27 14:55:57.304657573 +0000
|
||||
@@ -1,4 +1,4 @@
|
||||
Forecast at: 2026-09-27
|
||||
Steve score: 9
|
||||
Julie score: 9
|
||||
-Francis score: 9
|
||||
+Francis score: 11
|
||||
diff -ru '--exclude=target' /tmp/rehearsal/pristine/src/test/java/codingdojo/LongRangeForecastsTest.longRangeForecast.received.txt /tmp/rehearsal/runs/A+Q/workspace/src/test/java/codingdojo/LongRangeForecastsTest.longRangeForecast.received.txt
|
||||
--- /tmp/rehearsal/pristine/src/test/java/codingdojo/LongRangeForecastsTest.longRangeForecast.received.txt 2026-09-27 14:39:03.582411308 +0000
|
||||
+++ /tmp/rehearsal/runs/A+Q/workspace/src/test/java/codingdojo/LongRangeForecastsTest.longRangeForecast.received.txt 2026-09-27 14:54:47.953932861 +0000
|
||||
@@ -1 +1 @@
|
||||
-{Strawberry:12, Chocolate:12, Vanilla:12, }
|
||||
\ No newline at end of file
|
||||
+{Strawberry:12, Chocolate:12, Vanilla:17, }
|
||||
\ No newline at end of file
|
||||
@@ -1,35 +0,0 @@
|
||||
--- Run A+Q ---
|
||||
session file: /tmp/rehearsal/runs/A+Q/session/2026-09-27T14-54-11-453Z_01a0e35b-fbfd-71d3-8ae9-9dcefb682f49.jsonl
|
||||
model: Qwen/Qwen3.6-35B-A3B
|
||||
session span: 127.1 s
|
||||
assistant messages: 27
|
||||
tool calls: 28 total
|
||||
bash 17
|
||||
read 8
|
||||
edit 3
|
||||
tokens input: 338864
|
||||
tokens output: 10563
|
||||
tokens cacheRead: 0
|
||||
tokens cacheWrite: 0
|
||||
tokens reasoning: 3385
|
||||
tokens TOTAL (sum): 352812
|
||||
--- /usr/bin/time -v (wall clock + max RSS) ---
|
||||
Elapsed (wall clock) time (h:mm:ss or m:ss): 2:07.25
|
||||
Maximum resident set size (kbytes): 272272
|
||||
Exit status: 0
|
||||
--- pi stdout (last 15 lines) ---
|
||||
+ private List<Boolean> collectExpectedWeather(List<Instant> interestingDates,
|
||||
+ LongFunction<Boolean> dayOffsetToWeather) {
|
||||
+ List<Boolean> result = new ArrayList<>();
|
||||
+ for (Instant interestingDate : interestingDates) {
|
||||
+ scorer.updateSelection();
|
||||
+ var forecastDate = Instant.parse("2023-04-26T14:00:00.000-07:00");
|
||||
+ long daysForward = ChronoUnit.DAYS.between(forecastDate, interestingDate);
|
||||
+ result.add(dayOffsetToWeather.apply(daysForward));
|
||||
+ }
|
||||
+ return result;
|
||||
+ }
|
||||
}
|
||||
```
|
||||
|
||||
The code **compiles cleanly**. The test fails because the old `longRangeForecast(String)` signature no longer exists — which is expected per the task instructions.
|
||||
@@ -1,40 +0,0 @@
|
||||
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
|
||||
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
|
||||
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
|
||||
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:35)
|
||||
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
|
||||
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:54)
|
||||
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:147)
|
||||
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:127)
|
||||
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:90)
|
||||
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:55)
|
||||
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:102)
|
||||
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:54)
|
||||
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:114)
|
||||
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:86)
|
||||
at org.junit.platform.launcher.core.DefaultLauncherSession$DelegatingLauncher.execute(DefaultLauncherSession.java:86)
|
||||
at org.apache.maven.surefire.junitplatform.LazyLauncher.execute(LazyLauncher.java:56)
|
||||
at org.apache.maven.surefire.junitplatform.JUnitPlatformProvider.execute(JUnitPlatformProvider.java:184)
|
||||
at org.apache.maven.surefire.junitplatform.JUnitPlatformProvider.invokeAllTests(JUnitPlatformProvider.java:148)
|
||||
at org.apache.maven.surefire.junitplatform.JUnitPlatformProvider.invoke(JUnitPlatformProvider.java:122)
|
||||
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:385)
|
||||
at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:162)
|
||||
at org.apache.maven.surefire.booter.ForkedBooter.run(ForkedBooter.java:507)
|
||||
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:495)
|
||||
|
||||
[ERROR] Failures:
|
||||
[ERROR] LongRangeForecastsTest.longRangeForecast:18 expected: <{Strawberry:12, Chocolate:12, Vanilla:17, }
|
||||
> but was: <{}
|
||||
>
|
||||
[ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0
|
||||
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:3.2.5:test (default-test) on project IceCreamForecasting-Refactoring-Kata: There are test failures.
|
||||
[ERROR]
|
||||
[ERROR] Please refer to /tmp/rehearsal/runs/A+Q/workspace/target/surefire-reports for the individual test results.
|
||||
[ERROR] Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
|
||||
[ERROR] -> [Help 1]
|
||||
[ERROR]
|
||||
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
|
||||
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
|
||||
[ERROR]
|
||||
[ERROR] For more information about the errors and possible solutions, please read the following articles:
|
||||
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
|
||||
@@ -1,74 +0,0 @@
|
||||
|
||||
|
||||
## Refactoring Complete — Final Diff
|
||||
|
||||
Here's the summary of all changes made to `LongRangeForecasts.java`:
|
||||
|
||||
### Summary of Changes
|
||||
|
||||
| Step | What was done |
|
||||
|------|---------------|
|
||||
| **1** | Introduced `dayOffsetToWeather` lambda (`LongFunction<Boolean>`) at top of method; replaced `scorer.lookupWeather(daysForward)` with `dayOffsetToWeather.apply(daysForward)` |
|
||||
| **2** | Extracted weather-collection loop into new private method `collectExpectedWeather(List<Instant>, LongFunction<Boolean>)` |
|
||||
| **3** | Removed `String quarter` parameter, added no-arg overload `longRangeForecast()` that wraps `scorer` in a lambda and delegates to `longRangeForecast(LongFunction<Boolean>)` |
|
||||
|
||||
### Final Diff
|
||||
|
||||
```diff
|
||||
@@ -3,36 +3,27 @@
|
||||
import java.time.Instant;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.*;
|
||||
+import java.util.function.LongFunction;
|
||||
|
||||
public class LongRangeForecasts {
|
||||
|
||||
private final Scorer scorer = new Scorer();
|
||||
|
||||
- public Map<IceCream, Integer> longRangeForecast(String quarter) {
|
||||
+ public Map<IceCream, Integer> longRangeForecast() {
|
||||
+ return longRangeForecast(dayOffset -> scorer.lookupWeather(dayOffset));
|
||||
+ }
|
||||
+
|
||||
+ public Map<IceCream, Integer> longRangeForecast(LongFunction<Boolean> dayOffsetToWeather) {
|
||||
|
||||
var result = new HashMap<IceCream, Integer>();
|
||||
- if (Objects.equals(quarter, "Q1") || Objects.equals(quarter, "Q4")) {
|
||||
- for (IceCream flavour : IceCream.values()) {
|
||||
- result.put(flavour, 5);
|
||||
- }
|
||||
- }
|
||||
- for (IceCream flavour : IceCream.values()) {
|
||||
- result.put(flavour, 0);
|
||||
- }
|
||||
+
|
||||
List<Instant> interestingDates = List.of(...);
|
||||
- List<Boolean> expectedWeather = new ArrayList<>();
|
||||
- for (...) {
|
||||
- scorer.updateSelection();
|
||||
- ...
|
||||
- Boolean lookupWeather = scorer.lookupWeather(daysForward);
|
||||
- expectedWeather.add(lookupWeather);
|
||||
- }
|
||||
+ List<Boolean> expectedWeather = collectExpectedWeather(interestingDates, dayOffsetToWeather);
|
||||
|
||||
@@ -46,4 +37,16 @@
|
||||
}
|
||||
return result;
|
||||
}
|
||||
+
|
||||
+ private List<Boolean> collectExpectedWeather(List<Instant> interestingDates,
|
||||
+ LongFunction<Boolean> dayOffsetToWeather) {
|
||||
+ List<Boolean> result = new ArrayList<>();
|
||||
+ for (Instant interestingDate : interestingDates) {
|
||||
+ scorer.updateSelection();
|
||||
+ var forecastDate = Instant.parse("2023-04-26T14:00:00.000-07:00");
|
||||
+ long daysForward = ChronoUnit.DAYS.between(forecastDate, interestingDate);
|
||||
+ result.add(dayOffsetToWeather.apply(daysForward));
|
||||
+ }
|
||||
+ return result;
|
||||
+ }
|
||||
}
|
||||
```
|
||||
|
||||
The code **compiles cleanly**. The test fails because the old `longRangeForecast(String)` signature no longer exists — which is expected per the task instructions.
|
||||
@@ -1 +0,0 @@
|
||||
Read AGENTS.md in this directory and execute the refactoring task it describes. Run mvn -q test after each change. When done, stop and report the final diff of LongRangeForecasts.java.
|
||||
@@ -1,23 +0,0 @@
|
||||
Command being timed: "timeout -k 15 600 pi -p -a --provider entrim --model Qwen/Qwen3.6-35B-A3B --session-dir /tmp/rehearsal/runs/A+Q/session --name rehearsal-A+Q Read AGENTS.md in this directory and execute the refactoring task it describes. Run mvn -q test after each change. When done, stop and report the final diff of LongRangeForecasts.java."
|
||||
User time (seconds): 31.53
|
||||
System time (seconds): 2.47
|
||||
Percent of CPU this job got: 26%
|
||||
Elapsed (wall clock) time (h:mm:ss or m:ss): 2:07.25
|
||||
Average shared text size (kbytes): 0
|
||||
Average unshared data size (kbytes): 0
|
||||
Average stack size (kbytes): 0
|
||||
Average total size (kbytes): 0
|
||||
Maximum resident set size (kbytes): 272272
|
||||
Average resident set size (kbytes): 0
|
||||
Major (requiring I/O) page faults: 0
|
||||
Minor (reclaiming a frame) page faults: 692349
|
||||
Voluntary context switches: 72429
|
||||
Involuntary context switches: 1660
|
||||
Swaps: 0
|
||||
File system inputs: 0
|
||||
File system outputs: 2744
|
||||
Socket messages sent: 0
|
||||
Socket messages received: 0
|
||||
Signals delivered: 0
|
||||
Page size (bytes): 4096
|
||||
Exit status: 0
|
||||
@@ -1,25 +0,0 @@
|
||||
--- Run A+Q ---
|
||||
final diff vs pristine: 38 changed lines -> /tmp/rehearsal/runs/A+Q/diff.patch
|
||||
mvn test with solution's test + approved file:
|
||||
RESULT: FAIL (see /tmp/rehearsal/runs/A+Q/mvn-verify.log, last lines:)
|
||||
[ERROR] LongRangeForecastsTest.longRangeForecast:18 expected: <{Strawberry:12, Chocolate:12, Vanilla:17, }
|
||||
> but was: <{}
|
||||
>
|
||||
[ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0
|
||||
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:3.2.5:test (default-test) on project IceCreamForecasting-Refactoring-Kata: There are test failures.
|
||||
[ERROR]
|
||||
[ERROR] Please refer to /tmp/rehearsal/runs/A+Q/workspace/target/surefire-reports for the individual test results.
|
||||
[ERROR] Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
|
||||
[ERROR] -> [Help 1]
|
||||
[ERROR]
|
||||
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
|
||||
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
|
||||
[ERROR]
|
||||
[ERROR] For more information about the errors and possible solutions, please read the following articles:
|
||||
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
|
||||
structural checks:
|
||||
[x] method collectExpectedWeather exists
|
||||
[x] takes LongFunction<Boolean> parameter
|
||||
[i] note: 'scorer' still referenced in LongRangeForecasts.java
|
||||
[x] lambda syntax present
|
||||
[i] main class differs from reference solution (may still be a valid variant)
|
||||
@@ -1,31 +0,0 @@
|
||||
# Rehearsal workspace — refactoring task
|
||||
|
||||
You are refactoring `src/main/java/codingdojo/LongRangeForecasts.java` in this
|
||||
Maven project. Run `mvn -q test` after each change (mvn is on your PATH). It may
|
||||
fail — that is expected: the dependency on `Scorer` makes the method untestable —
|
||||
but the code must still *compile*.
|
||||
|
||||
## Goal
|
||||
|
||||
In `LongRangeForecasts.longRangeForecast()`, the loop over `interestingDates`
|
||||
collects weather by calling `scorer` (an unwanted external dependency).
|
||||
|
||||
1. Introduce a lambda for the scorer-dependent code and move its definition to
|
||||
the top of the method, leaving the invocation where it is. (No refactoring
|
||||
tool supports this — do it as a minimal manual edit.)
|
||||
2. Extract the weather-collection loop into a new method `collectExpectedWeather`
|
||||
that takes the lambda as a parameter (suggested type `LongFunction<Boolean>`).
|
||||
3. Add the lambda as a parameter to `longRangeForecast` itself: the signature
|
||||
must become `longRangeForecast(LongFunction<Boolean> dayOffsetToWeather)`,
|
||||
with a public no-arg overload `longRangeForecast()` that delegates and wraps
|
||||
the `scorer` field in a lambda. Do not keep the `String quarter` parameter.
|
||||
(`java-refactor change-method-signature` or `java-refactor introduce-param`
|
||||
may help; fall back to a minimal manual edit if no subcommand can do it.)
|
||||
4. Stop. Report the final diff.
|
||||
|
||||
## Tool rules (variant A)
|
||||
You may only change code by invoking `java-refactor` CLI commands
|
||||
(`java-refactor --help` lists them; it is on your PATH). Never edit `.java`
|
||||
files with text edits — the ONLY exceptions are the introduce-lambda edit
|
||||
(step 1) and, if no `java-refactor` subcommand can accomplish it, a minimal
|
||||
manual edit for step 3.
|
||||
@@ -1,92 +0,0 @@
|
||||
Only in /tmp/rehearsal/runs/A/workspace: AGENTS.md
|
||||
diff -ru '--exclude=target' /tmp/rehearsal/pristine/src/main/java/codingdojo/LongRangeForecasts.java /tmp/rehearsal/runs/A/workspace/src/main/java/codingdojo/LongRangeForecasts.java
|
||||
--- /tmp/rehearsal/pristine/src/main/java/codingdojo/LongRangeForecasts.java 2026-09-27 14:39:01.304467310 +0000
|
||||
+++ /tmp/rehearsal/runs/A/workspace/src/main/java/codingdojo/LongRangeForecasts.java 2026-09-27 14:52:09.626887233 +0000
|
||||
@@ -3,36 +3,25 @@
|
||||
import java.time.Instant;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.*;
|
||||
+import java.util.function.LongFunction;
|
||||
|
||||
public class LongRangeForecasts {
|
||||
|
||||
private final Scorer scorer = new Scorer();
|
||||
|
||||
- public Map<IceCream, Integer> longRangeForecast(String quarter) {
|
||||
+ public Map<IceCream, Integer> longRangeForecast() {
|
||||
+ return longRangeForecast(daysForward -> {
|
||||
+ scorer.updateSelection();
|
||||
+ return scorer.lookupWeather(daysForward);
|
||||
+ });
|
||||
+ }
|
||||
|
||||
+ public Map<IceCream, Integer> longRangeForecast(LongFunction<Boolean> dayOffsetToWeather) {
|
||||
var result = new HashMap<IceCream, Integer>();
|
||||
- if (Objects.equals(quarter, "Q1") || Objects.equals(quarter, "Q4")) {
|
||||
- for (IceCream flavour : IceCream.values()) {
|
||||
- result.put(flavour, 5);
|
||||
- }
|
||||
- }
|
||||
for (IceCream flavour : IceCream.values()) {
|
||||
result.put(flavour, 0);
|
||||
}
|
||||
- List<Instant> interestingDates = List.of(
|
||||
- Instant.parse("2023-05-01T14:00:00.000-07:00"),
|
||||
- Instant.parse("2023-05-18T14:00:00.000-07:00"),
|
||||
- Instant.parse("2023-06-05T14:00:00.000-07:00"),
|
||||
- Instant.parse("2023-06-23T14:00:00.000-07:00")
|
||||
- );
|
||||
- List<Boolean> expectedWeather = new ArrayList<>();
|
||||
- for (Instant interestingDate : interestingDates) {
|
||||
- scorer.updateSelection();
|
||||
- var forecastDate = Instant.parse("2023-04-26T14:00:00.000-07:00");
|
||||
- long daysForward = ChronoUnit.DAYS.between(forecastDate, interestingDate);
|
||||
- Boolean lookupWeather = scorer.lookupWeather(daysForward);
|
||||
- expectedWeather.add(lookupWeather);
|
||||
- }
|
||||
+ List<Boolean> expectedWeather = collectExpectedWeather(dayOffsetToWeather);
|
||||
|
||||
for (IceCream flavour : result.keySet()) {
|
||||
result.put(flavour, result.get(flavour) + 10);
|
||||
@@ -46,4 +35,21 @@
|
||||
}
|
||||
return result;
|
||||
}
|
||||
+
|
||||
+ private List<Boolean> collectExpectedWeather(LongFunction<Boolean> dayOffsetToWeather) {
|
||||
+ List<Instant> interestingDates = List.of(
|
||||
+ Instant.parse("2023-05-01T14:00:00.000-07:00"),
|
||||
+ Instant.parse("2023-05-18T14:00:00.000-07:00"),
|
||||
+ Instant.parse("2023-06-05T14:00:00.000-07:00"),
|
||||
+ Instant.parse("2023-06-23T14:00:00.000-07:00")
|
||||
+ );
|
||||
+ List<Boolean> expectedWeather = new ArrayList<>();
|
||||
+ for (Instant interestingDate : interestingDates) {
|
||||
+ var forecastDate = Instant.parse("2023-04-26T14:00:00.000-07:00");
|
||||
+ long daysForward = ChronoUnit.DAYS.between(forecastDate, interestingDate);
|
||||
+ Boolean lookupWeather = dayOffsetToWeather.apply(daysForward);
|
||||
+ expectedWeather.add(lookupWeather);
|
||||
+ }
|
||||
+ return expectedWeather;
|
||||
+ }
|
||||
}
|
||||
diff -ru '--exclude=target' /tmp/rehearsal/pristine/src/test/java/codingdojo/LongRangeForecastsTest.java /tmp/rehearsal/runs/A/workspace/src/test/java/codingdojo/LongRangeForecastsTest.java
|
||||
--- /tmp/rehearsal/pristine/src/test/java/codingdojo/LongRangeForecastsTest.java 2026-09-27 14:39:01.304467310 +0000
|
||||
+++ /tmp/rehearsal/runs/A/workspace/src/test/java/codingdojo/LongRangeForecastsTest.java 2026-09-27 14:51:53.937183804 +0000
|
||||
@@ -14,7 +14,7 @@
|
||||
@Test
|
||||
void longRangeForecast() {
|
||||
var quarter = "Q3";
|
||||
- var actual = new LongRangeForecasts().longRangeForecast(quarter);
|
||||
+ var actual = new LongRangeForecasts().longRangeForecast();
|
||||
StringBuilder printedResult = printIceCreamForecast(actual);
|
||||
|
||||
Approvals.verify(printedResult);
|
||||
diff -ru '--exclude=target' /tmp/rehearsal/pristine/src/test/java/codingdojo/LongRangeForecastsTest.longRangeForecast.received.txt /tmp/rehearsal/runs/A/workspace/src/test/java/codingdojo/LongRangeForecastsTest.longRangeForecast.received.txt
|
||||
--- /tmp/rehearsal/pristine/src/test/java/codingdojo/LongRangeForecastsTest.longRangeForecast.received.txt 2026-09-27 14:39:03.582411308 +0000
|
||||
+++ /tmp/rehearsal/runs/A/workspace/src/test/java/codingdojo/LongRangeForecastsTest.longRangeForecast.received.txt 2026-09-27 14:52:12.646830236 +0000
|
||||
@@ -1 +1 @@
|
||||
-{Strawberry:12, Chocolate:12, Vanilla:12, }
|
||||
\ No newline at end of file
|
||||
+{Strawberry:10, Chocolate:10, Vanilla:10, }
|
||||
\ No newline at end of file
|
||||
@@ -1,35 +0,0 @@
|
||||
--- Run A ---
|
||||
session file: /tmp/rehearsal/runs/A/session/2026-09-27T14-48-26-884Z_01a0e356-ba04-72d0-9e2e-57b0a6b3e25d.jsonl
|
||||
model: z-ai/glm-5.3-flash
|
||||
session span: 245.6 s
|
||||
assistant messages: 27
|
||||
tool calls: 27 total
|
||||
bash 19
|
||||
read 4
|
||||
edit 4
|
||||
tokens input: 109575
|
||||
tokens output: 12572
|
||||
tokens cacheRead: 288000
|
||||
tokens cacheWrite: 0
|
||||
tokens reasoning: 8571
|
||||
tokens TOTAL (sum): 418718
|
||||
--- /usr/bin/time -v (wall clock + max RSS) ---
|
||||
Elapsed (wall clock) time (h:mm:ss or m:ss): 4:05.76
|
||||
Maximum resident set size (kbytes): 262040
|
||||
Exit status: 0
|
||||
--- pi stdout (last 15 lines) ---
|
||||
+ for (Instant interestingDate : interestingDates) {
|
||||
+ var forecastDate = Instant.parse("2023-04-26T14:00:00.000-07:00");
|
||||
+ long daysForward = ChronoUnit.DAYS.between(forecastDate, interestingDate);
|
||||
+ Boolean lookupWeather = dayOffsetToWeather.apply(daysForward);
|
||||
+ expectedWeather.add(lookupWeather);
|
||||
+ }
|
||||
+ return expectedWeather;
|
||||
+ }
|
||||
|
||||
--- a/src/test/java/codingdojo/LongRangeForecastsTest.java (forced call-site update)
|
||||
- var actual = new LongRangeForecasts().longRangeForecast(quarter);
|
||||
+ var actual = new LongRangeForecasts().longRangeForecast();
|
||||
```
|
||||
|
||||
The scorer dependency is now injectable: `longRangeForecast()` delegates with a lambda wrapping the `scorer` field, and tests can pass a stub `LongFunction<Boolean>` instead.
|
||||
@@ -1,91 +0,0 @@
|
||||
Done — stopping here as instructed. Summary of how each step was performed:
|
||||
|
||||
## Steps taken
|
||||
|
||||
1. **Introduce lambda (manual edit, the allowed exception):** Moved the scorer-dependent code (`scorer.updateSelection()` / `scorer.lookupWeather(...)`) into a `LongFunction<Boolean> dayOffsetToWeather` lambda defined at the top of the method; the loop now calls `dayOffsetToWeather.apply(daysForward)` at the original invocation point. ✅ compiles, same 2 pre-existing test failures.
|
||||
|
||||
2. **Extract method (via `java-refactor extract`):** Selected the `interestingDates`/`expectedWeather` declarations + loop; produced exactly `private List<Boolean> collectExpectedWeather(LongFunction<Boolean> dayOffsetToWeather)`. ✅ same test result.
|
||||
|
||||
3. **Signature change (manual fallback):** No `java-refactor` subcommand could do it — I verified with dry-runs:
|
||||
- `decompose-conditional` generated broken code (out-of-scope `quarter`)
|
||||
- `remove-param` refuses: parameter still referenced
|
||||
- `introduce-param` refuses: "Selection is already a simple name"
|
||||
- `extract` generated invalid `var` parameter types; `change-method-signature` can't add/remove parameters
|
||||
|
||||
So per AGENTS.md I made a minimal manual edit: signature → `longRangeForecast(LongFunction<Boolean> dayOffsetToWeather)`, added public no-arg overload wrapping the `scorer` field in a lambda, dropped the `String quarter` parameter (and the dead `if` block that used it — its writes were unconditionally overwritten by the next loop anyway), and updated the test's call site to the no-arg overload (forced by the signature change). ✅ compiles; only the 2 baseline failures remain.
|
||||
|
||||
## Final diff
|
||||
|
||||
```diff
|
||||
--- a/src/main/java/codingdojo/LongRangeForecasts.java
|
||||
+++ b/src/main/java/codingdojo/LongRangeForecasts.java
|
||||
import java.time.Instant;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.*;
|
||||
+import java.util.function.LongFunction;
|
||||
|
||||
private final Scorer scorer = new Scorer();
|
||||
|
||||
- public Map<IceCream, Integer> longRangeForecast(String quarter) {
|
||||
+ public Map<IceCream, Integer> longRangeForecast() {
|
||||
+ return longRangeForecast(daysForward -> {
|
||||
+ scorer.updateSelection();
|
||||
+ return scorer.lookupWeather(daysForward);
|
||||
+ });
|
||||
+ }
|
||||
+
|
||||
+ public Map<IceCream, Integer> longRangeForecast(LongFunction<Boolean> dayOffsetToWeather) {
|
||||
var result = new HashMap<IceCream, Integer>();
|
||||
- if (Objects.equals(quarter, "Q1") || Objects.equals(quarter, "Q4")) {
|
||||
- for (IceCream flavour : IceCream.values()) {
|
||||
- result.put(flavour, 5);
|
||||
- }
|
||||
- }
|
||||
for (IceCream flavour : IceCream.values()) {
|
||||
result.put(flavour, 0);
|
||||
}
|
||||
- List<Instant> interestingDates = List.of(
|
||||
- Instant.parse("2023-05-01T14:00:00.000-07:00"),
|
||||
- Instant.parse("2023-05-18T14:00:00.000-07:00"),
|
||||
- Instant.parse("2023-06-05T14:00:00.000-07:00"),
|
||||
- Instant.parse("2023-06-23T14:00:00.000-07:00")
|
||||
- );
|
||||
- List<Boolean> expectedWeather = new ArrayList<>();
|
||||
- for (Instant interestingDate : interestingDates) {
|
||||
- scorer.updateSelection();
|
||||
- var forecastDate = Instant.parse("2023-04-26T14:00:00.000-07:00");
|
||||
- long daysForward = ChronoUnit.DAYS.between(forecastDate, interestingDate);
|
||||
- Boolean lookupWeather = scorer.lookupWeather(daysForward);
|
||||
- expectedWeather.add(lookupWeather);
|
||||
- }
|
||||
+ List<Boolean> expectedWeather = collectExpectedWeather(dayOffsetToWeather);
|
||||
|
||||
for (IceCream flavour : result.keySet()) {
|
||||
...
|
||||
}
|
||||
return result;
|
||||
}
|
||||
+
|
||||
+ private List<Boolean> collectExpectedWeather(LongFunction<Boolean> dayOffsetToWeather) {
|
||||
+ List<Instant> interestingDates = List.of(
|
||||
+ Instant.parse("2023-05-01T14:00:00.000-07:00"),
|
||||
+ Instant.parse("2023-05-18T14:00:00.000-07:00"),
|
||||
+ Instant.parse("2023-06-05T14:00:00.000-07:00"),
|
||||
+ Instant.parse("2023-06-23T14:00:00.000-07:00")
|
||||
+ );
|
||||
+ List<Boolean> expectedWeather = new ArrayList<>();
|
||||
+ for (Instant interestingDate : interestingDates) {
|
||||
+ var forecastDate = Instant.parse("2023-04-26T14:00:00.000-07:00");
|
||||
+ long daysForward = ChronoUnit.DAYS.between(forecastDate, interestingDate);
|
||||
+ Boolean lookupWeather = dayOffsetToWeather.apply(daysForward);
|
||||
+ expectedWeather.add(lookupWeather);
|
||||
+ }
|
||||
+ return expectedWeather;
|
||||
+ }
|
||||
|
||||
--- a/src/test/java/codingdojo/LongRangeForecastsTest.java (forced call-site update)
|
||||
- var actual = new LongRangeForecasts().longRangeForecast(quarter);
|
||||
+ var actual = new LongRangeForecasts().longRangeForecast();
|
||||
```
|
||||
|
||||
The scorer dependency is now injectable: `longRangeForecast()` delegates with a lambda wrapping the `scorer` field, and tests can pass a stub `LongFunction<Boolean>` instead.
|
||||
@@ -1 +0,0 @@
|
||||
Read AGENTS.md in this directory and execute the refactoring task it describes. Run mvn -q test after each change. When done, stop and report the final diff of LongRangeForecasts.java.
|
||||
@@ -1,23 +0,0 @@
|
||||
Command being timed: "timeout -k 15 600 pi -p -a --session-dir /tmp/rehearsal/runs/A/session --name rehearsal-A Read AGENTS.md in this directory and execute the refactoring task it describes. Run mvn -q test after each change. When done, stop and report the final diff of LongRangeForecasts.java."
|
||||
User time (seconds): 43.49
|
||||
System time (seconds): 3.19
|
||||
Percent of CPU this job got: 18%
|
||||
Elapsed (wall clock) time (h:mm:ss or m:ss): 4:05.76
|
||||
Average shared text size (kbytes): 0
|
||||
Average unshared data size (kbytes): 0
|
||||
Average stack size (kbytes): 0
|
||||
Average total size (kbytes): 0
|
||||
Maximum resident set size (kbytes): 262040
|
||||
Average resident set size (kbytes): 0
|
||||
Major (requiring I/O) page faults: 0
|
||||
Minor (reclaiming a frame) page faults: 1012660
|
||||
Voluntary context switches: 94163
|
||||
Involuntary context switches: 2559
|
||||
Swaps: 0
|
||||
File system inputs: 0
|
||||
File system outputs: 3992
|
||||
Socket messages sent: 0
|
||||
Socket messages received: 0
|
||||
Signals delivered: 0
|
||||
Page size (bytes): 4096
|
||||
Exit status: 0
|
||||
@@ -1,10 +0,0 @@
|
||||
--- Run A ---
|
||||
final diff vs pristine: 49 changed lines -> /tmp/rehearsal/runs/A/diff.patch
|
||||
mvn test with solution's test + approved file:
|
||||
RESULT: PASS (deterministic)
|
||||
structural checks:
|
||||
[x] method collectExpectedWeather exists
|
||||
[x] takes LongFunction<Boolean> parameter
|
||||
[i] note: 'scorer' still referenced in LongRangeForecasts.java
|
||||
[x] lambda syntax present
|
||||
[i] main class differs from reference solution (may still be a valid variant)
|
||||
@@ -1,27 +0,0 @@
|
||||
# Rehearsal workspace — refactoring task
|
||||
|
||||
You are refactoring `src/main/java/codingdojo/LongRangeForecasts.java` in this
|
||||
Maven project. Run `mvn -q test` after each change (mvn is on your PATH). It may
|
||||
fail — that is expected: the dependency on `Scorer` makes the method untestable —
|
||||
but the code must still *compile*.
|
||||
|
||||
## Goal
|
||||
|
||||
In `LongRangeForecasts.longRangeForecast()`, the loop over `interestingDates`
|
||||
collects weather by calling `scorer` (an unwanted external dependency).
|
||||
|
||||
1. Introduce a lambda for the scorer-dependent code and move its definition to
|
||||
the top of the method, leaving the invocation where it is. (No refactoring
|
||||
tool supports this — do it as a minimal manual edit.)
|
||||
2. Extract the weather-collection loop into a new method `collectExpectedWeather`
|
||||
that takes the lambda as a parameter (suggested type `LongFunction<Boolean>`).
|
||||
3. Add the lambda as a parameter to `longRangeForecast` itself: the signature
|
||||
must become `longRangeForecast(LongFunction<Boolean> dayOffsetToWeather)`,
|
||||
with a public no-arg overload `longRangeForecast()` that delegates and wraps
|
||||
the `scorer` field in a lambda. Do not keep the `String quarter` parameter.
|
||||
(`java-refactor change-method-signature` or `java-refactor introduce-param`
|
||||
may help; fall back to a minimal manual edit if no subcommand can do it.)
|
||||
4. Stop. Report the final diff.
|
||||
|
||||
## Tool rules (variant B)
|
||||
You may change code with your normal file-editing tools.
|
||||
@@ -1,85 +0,0 @@
|
||||
Only in /tmp/rehearsal/runs/B+Q/workspace: AGENTS.md
|
||||
diff -ru '--exclude=target' /tmp/rehearsal/pristine/src/main/java/codingdojo/LongRangeForecasts.java /tmp/rehearsal/runs/B+Q/workspace/src/main/java/codingdojo/LongRangeForecasts.java
|
||||
--- /tmp/rehearsal/pristine/src/main/java/codingdojo/LongRangeForecasts.java 2026-09-27 14:58:14.139167804 +0000
|
||||
+++ /tmp/rehearsal/runs/B+Q/workspace/src/main/java/codingdojo/LongRangeForecasts.java 2026-09-27 15:20:44.058440673 +0000
|
||||
@@ -3,19 +3,23 @@
|
||||
import java.time.Instant;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.*;
|
||||
+import java.util.function.LongFunction;
|
||||
|
||||
public class LongRangeForecasts {
|
||||
|
||||
private final Scorer scorer = new Scorer();
|
||||
|
||||
- public Map<IceCream, Integer> longRangeForecast(String quarter) {
|
||||
+ public Map<IceCream, Integer> longRangeForecast() {
|
||||
+ return longRangeForecast(
|
||||
+ days -> {
|
||||
+ scorer.updateSelection();
|
||||
+ return scorer.lookupWeather(days);
|
||||
+ });
|
||||
+ }
|
||||
+
|
||||
+ public Map<IceCream, Integer> longRangeForecast(LongFunction<Boolean> dayOffsetToWeather) {
|
||||
|
||||
var result = new HashMap<IceCream, Integer>();
|
||||
- if (Objects.equals(quarter, "Q1") || Objects.equals(quarter, "Q4")) {
|
||||
- for (IceCream flavour : IceCream.values()) {
|
||||
- result.put(flavour, 5);
|
||||
- }
|
||||
- }
|
||||
for (IceCream flavour : IceCream.values()) {
|
||||
result.put(flavour, 0);
|
||||
}
|
||||
@@ -25,14 +29,8 @@
|
||||
Instant.parse("2023-06-05T14:00:00.000-07:00"),
|
||||
Instant.parse("2023-06-23T14:00:00.000-07:00")
|
||||
);
|
||||
- List<Boolean> expectedWeather = new ArrayList<>();
|
||||
- for (Instant interestingDate : interestingDates) {
|
||||
- scorer.updateSelection();
|
||||
- var forecastDate = Instant.parse("2023-04-26T14:00:00.000-07:00");
|
||||
- long daysForward = ChronoUnit.DAYS.between(forecastDate, interestingDate);
|
||||
- Boolean lookupWeather = scorer.lookupWeather(daysForward);
|
||||
- expectedWeather.add(lookupWeather);
|
||||
- }
|
||||
+
|
||||
+ List<Boolean> expectedWeather = collectExpectedWeather(interestingDates, dayOffsetToWeather);
|
||||
|
||||
for (IceCream flavour : result.keySet()) {
|
||||
result.put(flavour, result.get(flavour) + 10);
|
||||
@@ -46,4 +44,16 @@
|
||||
}
|
||||
return result;
|
||||
}
|
||||
+
|
||||
+ private List<Boolean> collectExpectedWeather(
|
||||
+ List<Instant> interestingDates,
|
||||
+ LongFunction<Boolean> dayOffsetToWeather) {
|
||||
+ List<Boolean> expectedWeather = new ArrayList<>();
|
||||
+ for (Instant interestingDate : interestingDates) {
|
||||
+ var forecastDate = Instant.parse("2023-04-26T14:00:00.000-07:00");
|
||||
+ long daysForward = ChronoUnit.DAYS.between(forecastDate, interestingDate);
|
||||
+ expectedWeather.add(dayOffsetToWeather.apply(daysForward));
|
||||
+ }
|
||||
+ return expectedWeather;
|
||||
+ }
|
||||
}
|
||||
diff -ru '--exclude=target' /tmp/rehearsal/pristine/src/test/java/codingdojo/DailyForecastsTest.salesForecast.received.txt /tmp/rehearsal/runs/B+Q/workspace/src/test/java/codingdojo/DailyForecastsTest.salesForecast.received.txt
|
||||
--- /tmp/rehearsal/pristine/src/test/java/codingdojo/DailyForecastsTest.salesForecast.received.txt 2026-09-27 14:58:16.443126144 +0000
|
||||
+++ /tmp/rehearsal/runs/B+Q/workspace/src/test/java/codingdojo/DailyForecastsTest.salesForecast.received.txt 2026-09-27 15:20:48.113390371 +0000
|
||||
@@ -1,4 +1,4 @@
|
||||
Forecast at: 2026-09-27
|
||||
Steve score: 9
|
||||
-Julie score: 9
|
||||
+Julie score: 3
|
||||
Francis score: 3
|
||||
diff -ru '--exclude=target' /tmp/rehearsal/pristine/src/test/java/codingdojo/LongRangeForecastsTest.longRangeForecast.received.txt /tmp/rehearsal/runs/B+Q/workspace/src/test/java/codingdojo/LongRangeForecastsTest.longRangeForecast.received.txt
|
||||
--- /tmp/rehearsal/pristine/src/test/java/codingdojo/LongRangeForecastsTest.longRangeForecast.received.txt 2026-09-27 14:58:16.402126886 +0000
|
||||
+++ /tmp/rehearsal/runs/B+Q/workspace/src/test/java/codingdojo/LongRangeForecastsTest.longRangeForecast.received.txt 2026-09-27 15:20:37.371523442 +0000
|
||||
@@ -1 +1 @@
|
||||
-{Strawberry:10, Chocolate:10, Vanilla:10, }
|
||||
\ No newline at end of file
|
||||
+{Strawberry:12, Chocolate:12, Vanilla:12, }
|
||||
\ No newline at end of file
|
||||
@@ -1,35 +0,0 @@
|
||||
--- Run B+Q ---
|
||||
session file: /tmp/rehearsal/runs/B+Q/session/2026-09-27T15-19-54-490Z_01a0e373-8779-7024-b65c-e6e4e01334b3.jsonl
|
||||
model: Qwen/Qwen3.6-35B-A3B
|
||||
session span: 72.5 s
|
||||
assistant messages: 17
|
||||
tool calls: 17 total
|
||||
bash 8
|
||||
read 5
|
||||
edit 4
|
||||
tokens input: 253072
|
||||
tokens output: 6343
|
||||
tokens cacheRead: 0
|
||||
tokens cacheWrite: 0
|
||||
tokens reasoning: 637
|
||||
tokens TOTAL (sum): 260052
|
||||
--- /usr/bin/time -v (wall clock + max RSS) ---
|
||||
Elapsed (wall clock) time (h:mm:ss or m:ss): 1:12.70
|
||||
Maximum resident set size (kbytes): 255980
|
||||
Exit status: 0
|
||||
--- pi stdout (last 15 lines) ---
|
||||
All three steps are complete. The code compiles successfully. Here's a summary of the refactoring:
|
||||
|
||||
---
|
||||
|
||||
## Final Diff for `LongRangeForecasts.java`
|
||||
|
||||
| Change | Description |
|
||||
|--------|-------------|
|
||||
| **Import** | Added `java.util.function.LongFunction` |
|
||||
| **New no-arg overload** | `longRangeForecast()` — wraps the scorer in a lambda and delegates to the parameterized version |
|
||||
| **Signature change** | `longRangeForecast(String quarter)` → `longRangeForecast(LongFunction<Boolean> dayOffsetToWeather)` — removed `quarter` param and its Q1/Q4 block |
|
||||
| **Lambda extraction** | Removed the internal `dayOffsetToWeather` lambda definition; it's now passed as a method parameter |
|
||||
| **New helper method** | `collectExpectedWeather(List<Instant>, LongFunction<Boolean>)` — extracted weather-collection loop, receives the dependency-injected lambda |
|
||||
|
||||
The key outcome is that `Scorer` is no longer directly invoked inside `longRangeForecast`. Its behavior is injected via the `LongFunction<Boolean>` parameter, making the method testable and decoupled from the external dependency.
|
||||
@@ -1,15 +0,0 @@
|
||||
All three steps are complete. The code compiles successfully. Here's a summary of the refactoring:
|
||||
|
||||
---
|
||||
|
||||
## Final Diff for `LongRangeForecasts.java`
|
||||
|
||||
| Change | Description |
|
||||
|--------|-------------|
|
||||
| **Import** | Added `java.util.function.LongFunction` |
|
||||
| **New no-arg overload** | `longRangeForecast()` — wraps the scorer in a lambda and delegates to the parameterized version |
|
||||
| **Signature change** | `longRangeForecast(String quarter)` → `longRangeForecast(LongFunction<Boolean> dayOffsetToWeather)` — removed `quarter` param and its Q1/Q4 block |
|
||||
| **Lambda extraction** | Removed the internal `dayOffsetToWeather` lambda definition; it's now passed as a method parameter |
|
||||
| **New helper method** | `collectExpectedWeather(List<Instant>, LongFunction<Boolean>)` — extracted weather-collection loop, receives the dependency-injected lambda |
|
||||
|
||||
The key outcome is that `Scorer` is no longer directly invoked inside `longRangeForecast`. Its behavior is injected via the `LongFunction<Boolean>` parameter, making the method testable and decoupled from the external dependency.
|
||||
@@ -1 +0,0 @@
|
||||
Read AGENTS.md in this directory and execute the refactoring task it describes. Run mvn -q test after each change. When done, stop and report the final diff of LongRangeForecasts.java.
|
||||
@@ -1,23 +0,0 @@
|
||||
Command being timed: "timeout -k 15 600 pi -p -a --provider entrim --model Qwen/Qwen3.6-35B-A3B --session-dir /tmp/rehearsal/runs/B+Q/session --name rehearsal-B+Q Read AGENTS.md in this directory and execute the refactoring task it describes. Run mvn -q test after each change. When done, stop and report the final diff of LongRangeForecasts.java."
|
||||
User time (seconds): 22.32
|
||||
System time (seconds): 1.63
|
||||
Percent of CPU this job got: 32%
|
||||
Elapsed (wall clock) time (h:mm:ss or m:ss): 1:12.70
|
||||
Average shared text size (kbytes): 0
|
||||
Average unshared data size (kbytes): 0
|
||||
Average stack size (kbytes): 0
|
||||
Average total size (kbytes): 0
|
||||
Maximum resident set size (kbytes): 255980
|
||||
Average resident set size (kbytes): 0
|
||||
Major (requiring I/O) page faults: 0
|
||||
Minor (reclaiming a frame) page faults: 474539
|
||||
Voluntary context switches: 45190
|
||||
Involuntary context switches: 1059
|
||||
Swaps: 0
|
||||
File system inputs: 0
|
||||
File system outputs: 2072
|
||||
Socket messages sent: 0
|
||||
Socket messages received: 0
|
||||
Signals delivered: 0
|
||||
Page size (bytes): 4096
|
||||
Exit status: 0
|
||||
@@ -1,10 +0,0 @@
|
||||
--- Run B+Q ---
|
||||
final diff vs pristine: 39 changed lines -> /tmp/rehearsal/runs/B+Q/diff.patch
|
||||
mvn test with solution's test + approved file:
|
||||
RESULT: PASS (deterministic)
|
||||
structural checks:
|
||||
[x] method collectExpectedWeather exists
|
||||
[x] takes LongFunction<Boolean> parameter
|
||||
[i] note: 'scorer' still referenced in LongRangeForecasts.java
|
||||
[x] lambda syntax present
|
||||
[i] main class differs from reference solution (may still be a valid variant)
|
||||
@@ -1,27 +0,0 @@
|
||||
# Rehearsal workspace — refactoring task
|
||||
|
||||
You are refactoring `src/main/java/codingdojo/LongRangeForecasts.java` in this
|
||||
Maven project. Run `mvn -q test` after each change (mvn is on your PATH). It may
|
||||
fail — that is expected: the dependency on `Scorer` makes the method untestable —
|
||||
but the code must still *compile*.
|
||||
|
||||
## Goal
|
||||
|
||||
In `LongRangeForecasts.longRangeForecast()`, the loop over `interestingDates`
|
||||
collects weather by calling `scorer` (an unwanted external dependency).
|
||||
|
||||
1. Introduce a lambda for the scorer-dependent code and move its definition to
|
||||
the top of the method, leaving the invocation where it is. (No refactoring
|
||||
tool supports this — do it as a minimal manual edit.)
|
||||
2. Extract the weather-collection loop into a new method `collectExpectedWeather`
|
||||
that takes the lambda as a parameter (suggested type `LongFunction<Boolean>`).
|
||||
3. Add the lambda as a parameter to `longRangeForecast` itself: the signature
|
||||
must become `longRangeForecast(LongFunction<Boolean> dayOffsetToWeather)`,
|
||||
with a public no-arg overload `longRangeForecast()` that delegates and wraps
|
||||
the `scorer` field in a lambda. Do not keep the `String quarter` parameter.
|
||||
(`java-refactor change-method-signature` or `java-refactor introduce-param`
|
||||
may help; fall back to a minimal manual edit if no subcommand can do it.)
|
||||
4. Stop. Report the final diff.
|
||||
|
||||
## Tool rules (variant B)
|
||||
You may change code with your normal file-editing tools.
|
||||
@@ -1,103 +0,0 @@
|
||||
Only in /tmp/rehearsal/runs/B/workspace: AGENTS.md
|
||||
diff -ru '--exclude=target' /tmp/rehearsal/pristine/src/main/java/codingdojo/LongRangeForecasts.java /tmp/rehearsal/runs/B/workspace/src/main/java/codingdojo/LongRangeForecasts.java
|
||||
--- /tmp/rehearsal/pristine/src/main/java/codingdojo/LongRangeForecasts.java 2026-09-27 14:39:01.304467310 +0000
|
||||
+++ /tmp/rehearsal/runs/B/workspace/src/main/java/codingdojo/LongRangeForecasts.java 2026-09-27 14:53:31.157357707 +0000
|
||||
@@ -3,36 +3,25 @@
|
||||
import java.time.Instant;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.*;
|
||||
+import java.util.function.LongFunction;
|
||||
|
||||
public class LongRangeForecasts {
|
||||
|
||||
private final Scorer scorer = new Scorer();
|
||||
|
||||
- public Map<IceCream, Integer> longRangeForecast(String quarter) {
|
||||
+ public Map<IceCream, Integer> longRangeForecast() {
|
||||
+ return longRangeForecast(daysForward -> {
|
||||
+ scorer.updateSelection();
|
||||
+ return scorer.lookupWeather(daysForward);
|
||||
+ });
|
||||
+ }
|
||||
|
||||
+ public Map<IceCream, Integer> longRangeForecast(LongFunction<Boolean> dayOffsetToWeather) {
|
||||
var result = new HashMap<IceCream, Integer>();
|
||||
- if (Objects.equals(quarter, "Q1") || Objects.equals(quarter, "Q4")) {
|
||||
- for (IceCream flavour : IceCream.values()) {
|
||||
- result.put(flavour, 5);
|
||||
- }
|
||||
- }
|
||||
for (IceCream flavour : IceCream.values()) {
|
||||
result.put(flavour, 0);
|
||||
}
|
||||
- List<Instant> interestingDates = List.of(
|
||||
- Instant.parse("2023-05-01T14:00:00.000-07:00"),
|
||||
- Instant.parse("2023-05-18T14:00:00.000-07:00"),
|
||||
- Instant.parse("2023-06-05T14:00:00.000-07:00"),
|
||||
- Instant.parse("2023-06-23T14:00:00.000-07:00")
|
||||
- );
|
||||
- List<Boolean> expectedWeather = new ArrayList<>();
|
||||
- for (Instant interestingDate : interestingDates) {
|
||||
- scorer.updateSelection();
|
||||
- var forecastDate = Instant.parse("2023-04-26T14:00:00.000-07:00");
|
||||
- long daysForward = ChronoUnit.DAYS.between(forecastDate, interestingDate);
|
||||
- Boolean lookupWeather = scorer.lookupWeather(daysForward);
|
||||
- expectedWeather.add(lookupWeather);
|
||||
- }
|
||||
+ List<Boolean> expectedWeather = collectExpectedWeather(dayOffsetToWeather);
|
||||
|
||||
for (IceCream flavour : result.keySet()) {
|
||||
result.put(flavour, result.get(flavour) + 10);
|
||||
@@ -46,4 +35,21 @@
|
||||
}
|
||||
return result;
|
||||
}
|
||||
+
|
||||
+ private List<Boolean> collectExpectedWeather(LongFunction<Boolean> dayOffsetToWeather) {
|
||||
+ List<Instant> interestingDates = List.of(
|
||||
+ Instant.parse("2023-05-01T14:00:00.000-07:00"),
|
||||
+ Instant.parse("2023-05-18T14:00:00.000-07:00"),
|
||||
+ Instant.parse("2023-06-05T14:00:00.000-07:00"),
|
||||
+ Instant.parse("2023-06-23T14:00:00.000-07:00")
|
||||
+ );
|
||||
+ List<Boolean> expectedWeather = new ArrayList<>();
|
||||
+ for (Instant interestingDate : interestingDates) {
|
||||
+ var forecastDate = Instant.parse("2023-04-26T14:00:00.000-07:00");
|
||||
+ long daysForward = ChronoUnit.DAYS.between(forecastDate, interestingDate);
|
||||
+ Boolean lookupWeather = dayOffsetToWeather.apply(daysForward);
|
||||
+ expectedWeather.add(lookupWeather);
|
||||
+ }
|
||||
+ return expectedWeather;
|
||||
+ }
|
||||
}
|
||||
diff -ru '--exclude=target' /tmp/rehearsal/pristine/src/test/java/codingdojo/DailyForecastsTest.salesForecast.received.txt /tmp/rehearsal/runs/B/workspace/src/test/java/codingdojo/DailyForecastsTest.salesForecast.received.txt
|
||||
--- /tmp/rehearsal/pristine/src/test/java/codingdojo/DailyForecastsTest.salesForecast.received.txt 2026-09-27 14:39:03.626410227 +0000
|
||||
+++ /tmp/rehearsal/runs/B/workspace/src/test/java/codingdojo/DailyForecastsTest.salesForecast.received.txt 2026-09-27 14:53:35.478277153 +0000
|
||||
@@ -1,4 +1,4 @@
|
||||
Forecast at: 2026-09-27
|
||||
Steve score: 9
|
||||
-Julie score: 9
|
||||
-Francis score: 9
|
||||
+Julie score: 3
|
||||
+Francis score: 11
|
||||
diff -ru '--exclude=target' /tmp/rehearsal/pristine/src/test/java/codingdojo/LongRangeForecastsTest.java /tmp/rehearsal/runs/B/workspace/src/test/java/codingdojo/LongRangeForecastsTest.java
|
||||
--- /tmp/rehearsal/pristine/src/test/java/codingdojo/LongRangeForecastsTest.java 2026-09-27 14:39:01.304467310 +0000
|
||||
+++ /tmp/rehearsal/runs/B/workspace/src/test/java/codingdojo/LongRangeForecastsTest.java 2026-09-27 14:53:33.005323250 +0000
|
||||
@@ -13,8 +13,7 @@
|
||||
|
||||
@Test
|
||||
void longRangeForecast() {
|
||||
- var quarter = "Q3";
|
||||
- var actual = new LongRangeForecasts().longRangeForecast(quarter);
|
||||
+ var actual = new LongRangeForecasts().longRangeForecast();
|
||||
StringBuilder printedResult = printIceCreamForecast(actual);
|
||||
|
||||
Approvals.verify(printedResult);
|
||||
diff -ru '--exclude=target' /tmp/rehearsal/pristine/src/test/java/codingdojo/LongRangeForecastsTest.longRangeForecast.received.txt /tmp/rehearsal/runs/B/workspace/src/test/java/codingdojo/LongRangeForecastsTest.longRangeForecast.received.txt
|
||||
--- /tmp/rehearsal/pristine/src/test/java/codingdojo/LongRangeForecastsTest.longRangeForecast.received.txt 2026-09-27 14:39:03.582411308 +0000
|
||||
+++ /tmp/rehearsal/runs/B/workspace/src/test/java/codingdojo/LongRangeForecastsTest.longRangeForecast.received.txt 2026-09-27 14:53:35.437277917 +0000
|
||||
@@ -1 +1 @@
|
||||
-{Strawberry:12, Chocolate:12, Vanilla:12, }
|
||||
\ No newline at end of file
|
||||
+{Strawberry:12, Chocolate:12, Vanilla:17, }
|
||||
\ No newline at end of file
|
||||
@@ -1,35 +0,0 @@
|
||||
--- Run B ---
|
||||
session file: /tmp/rehearsal/runs/B/session/2026-09-27T14-53-00-499Z_01a0e35a-e6d3-70ca-b02c-6d99534cf397.jsonl
|
||||
model: z-ai/glm-5.3-flash
|
||||
session span: 39.2 s
|
||||
assistant messages: 10
|
||||
tool calls: 10 total
|
||||
bash 5
|
||||
edit 4
|
||||
read 1
|
||||
tokens input: 21722
|
||||
tokens output: 4361
|
||||
tokens cacheRead: 33984
|
||||
tokens cacheWrite: 0
|
||||
tokens reasoning: 2129
|
||||
tokens TOTAL (sum): 62196
|
||||
--- /usr/bin/time -v (wall clock + max RSS) ---
|
||||
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:39.35
|
||||
Maximum resident set size (kbytes): 249312
|
||||
Exit status: 0
|
||||
--- pi stdout (last 15 lines) ---
|
||||
Boolean lookupWeather = dayOffsetToWeather.apply(daysForward);
|
||||
expectedWeather.add(lookupWeather);
|
||||
}
|
||||
return expectedWeather;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Diff summary** (relative to the original):
|
||||
1. Added import `java.util.function.LongFunction`.
|
||||
2. The scorer-dependent code (`scorer.updateSelection()` + `scorer.lookupWeather(daysForward)`) was extracted into a lambda, now passed in as `dayOffsetToWeather` (`LongFunction<Boolean>`); the invocation site (`dayOffsetToWeather.apply(daysForward)`) stayed in the loop.
|
||||
3. The weather-collection loop (with `interestingDates`) moved into new private method `collectExpectedWeather(LongFunction<Boolean> dayOffsetToWeather)` returning `List<Boolean>`.
|
||||
4. Signature changed from `longRangeForecast(String quarter)` to `longRangeForecast(LongFunction<Boolean> dayOffsetToWeather)`; the `String quarter` parameter and its `Q1`/`Q4` block were removed, and a public no-arg overload was added that delegates and wraps the `scorer` field in a lambda.
|
||||
|
||||
**Note:** the test call site in `src/test/java/codingdojo/LongRangeForecastsTest.java` was updated (`longRangeForecast(quarter)` → `longRangeForecast()`) so the project still compiles after the signature change.
|
||||
@@ -1,69 +0,0 @@
|
||||
Done. All three refactoring steps are complete; after each one `mvn -q test` compiled successfully (the remaining test failures are the pre-existing ones caused by `Scorer`'s use of `new Random().nextBoolean()` — the expected untestability, not a compile problem). Stopping here as instructed.
|
||||
|
||||
There's no git repo in the workspace, so here is the final state and a manual diff summary of `LongRangeForecasts.java`:
|
||||
|
||||
```java
|
||||
package codingdojo;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.*;
|
||||
import java.util.function.LongFunction;
|
||||
|
||||
public class LongRangeForecasts {
|
||||
|
||||
private final Scorer scorer = new Scorer();
|
||||
|
||||
public Map<IceCream, Integer> longRangeForecast() {
|
||||
return longRangeForecast(daysForward -> {
|
||||
scorer.updateSelection();
|
||||
return scorer.lookupWeather(daysForward);
|
||||
});
|
||||
}
|
||||
|
||||
public Map<IceCream, Integer> longRangeForecast(LongFunction<Boolean> dayOffsetToWeather) {
|
||||
var result = new HashMap<IceCream, Integer>();
|
||||
for (IceCream flavour : IceCream.values()) {
|
||||
result.put(flavour, 0);
|
||||
}
|
||||
List<Boolean> expectedWeather = collectExpectedWeather(dayOffsetToWeather);
|
||||
|
||||
for (IceCream flavour : result.keySet()) {
|
||||
result.put(flavour, result.get(flavour) + 10);
|
||||
var sunnyHolidays = expectedWeather.stream().filter(s -> s).count();
|
||||
if (sunnyHolidays > 2 && flavour == IceCream.Vanilla) {
|
||||
result.put(flavour, result.get(flavour) + 5);
|
||||
}
|
||||
if (sunnyHolidays > 1 && expectedWeather.get(1)) {
|
||||
result.put(flavour, result.get(flavour) + 2);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private List<Boolean> collectExpectedWeather(LongFunction<Boolean> dayOffsetToWeather) {
|
||||
List<Instant> interestingDates = List.of(
|
||||
Instant.parse("2023-05-01T14:00:00.000-07:00"),
|
||||
Instant.parse("2023-05-18T14:00:00.000-07:00"),
|
||||
Instant.parse("2023-06-05T14:00:00.000-07:00"),
|
||||
Instant.parse("2023-06-23T14:00:00.000-07:00")
|
||||
);
|
||||
List<Boolean> expectedWeather = new ArrayList<>();
|
||||
for (Instant interestingDate : interestingDates) {
|
||||
var forecastDate = Instant.parse("2023-04-26T14:00:00.000-07:00");
|
||||
long daysForward = ChronoUnit.DAYS.between(forecastDate, interestingDate);
|
||||
Boolean lookupWeather = dayOffsetToWeather.apply(daysForward);
|
||||
expectedWeather.add(lookupWeather);
|
||||
}
|
||||
return expectedWeather;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Diff summary** (relative to the original):
|
||||
1. Added import `java.util.function.LongFunction`.
|
||||
2. The scorer-dependent code (`scorer.updateSelection()` + `scorer.lookupWeather(daysForward)`) was extracted into a lambda, now passed in as `dayOffsetToWeather` (`LongFunction<Boolean>`); the invocation site (`dayOffsetToWeather.apply(daysForward)`) stayed in the loop.
|
||||
3. The weather-collection loop (with `interestingDates`) moved into new private method `collectExpectedWeather(LongFunction<Boolean> dayOffsetToWeather)` returning `List<Boolean>`.
|
||||
4. Signature changed from `longRangeForecast(String quarter)` to `longRangeForecast(LongFunction<Boolean> dayOffsetToWeather)`; the `String quarter` parameter and its `Q1`/`Q4` block were removed, and a public no-arg overload was added that delegates and wraps the `scorer` field in a lambda.
|
||||
|
||||
**Note:** the test call site in `src/test/java/codingdojo/LongRangeForecastsTest.java` was updated (`longRangeForecast(quarter)` → `longRangeForecast()`) so the project still compiles after the signature change.
|
||||
@@ -1 +0,0 @@
|
||||
Read AGENTS.md in this directory and execute the refactoring task it describes. Run mvn -q test after each change. When done, stop and report the final diff of LongRangeForecasts.java.
|
||||
@@ -1,23 +0,0 @@
|
||||
Command being timed: "timeout -k 15 600 pi -p -a --session-dir /tmp/rehearsal/runs/B/session --name rehearsal-B Read AGENTS.md in this directory and execute the refactoring task it describes. Run mvn -q test after each change. When done, stop and report the final diff of LongRangeForecasts.java."
|
||||
User time (seconds): 15.30
|
||||
System time (seconds): 1.08
|
||||
Percent of CPU this job got: 41%
|
||||
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:39.35
|
||||
Average shared text size (kbytes): 0
|
||||
Average unshared data size (kbytes): 0
|
||||
Average stack size (kbytes): 0
|
||||
Average total size (kbytes): 0
|
||||
Maximum resident set size (kbytes): 249312
|
||||
Average resident set size (kbytes): 0
|
||||
Major (requiring I/O) page faults: 0
|
||||
Minor (reclaiming a frame) page faults: 332026
|
||||
Voluntary context switches: 28517
|
||||
Involuntary context switches: 935
|
||||
Swaps: 0
|
||||
File system inputs: 0
|
||||
File system outputs: 1624
|
||||
Socket messages sent: 0
|
||||
Socket messages received: 0
|
||||
Signals delivered: 0
|
||||
Page size (bytes): 4096
|
||||
Exit status: 0
|
||||
@@ -1,10 +0,0 @@
|
||||
--- Run B ---
|
||||
final diff vs pristine: 54 changed lines -> /tmp/rehearsal/runs/B/diff.patch
|
||||
mvn test with solution's test + approved file:
|
||||
RESULT: PASS (deterministic)
|
||||
structural checks:
|
||||
[x] method collectExpectedWeather exists
|
||||
[x] takes LongFunction<Boolean> parameter
|
||||
[i] note: 'scorer' still referenced in LongRangeForecasts.java
|
||||
[x] lambda syntax present
|
||||
[i] main class differs from reference solution (may still be a valid variant)
|
||||
@@ -1,36 +0,0 @@
|
||||
# Rehearsal workspace — refactoring task
|
||||
|
||||
You are refactoring `src/main/java/codingdojo/LongRangeForecasts.java` in this
|
||||
Maven project. Run `mvn -q test` after each change (mvn is on your PATH). It may
|
||||
fail — that is expected: the dependency on `Scorer` makes the method untestable —
|
||||
but the code must still *compile*.
|
||||
|
||||
## Goal
|
||||
|
||||
In `LongRangeForecasts.longRangeForecast()`, the loop over `interestingDates`
|
||||
collects weather by calling `scorer` (an unwanted external dependency).
|
||||
|
||||
1. Introduce a lambda for the scorer-dependent code and move its definition to
|
||||
the top of the method, leaving the invocation where it is. (No refactoring
|
||||
tool supports this — do it as a minimal manual edit.)
|
||||
2. Extract the weather-collection loop into a new method `collectExpectedWeather`
|
||||
that takes the lambda as a parameter (suggested type `LongFunction<Boolean>`).
|
||||
3. Add the lambda as a parameter to `longRangeForecast` itself: the signature
|
||||
must become `longRangeForecast(LongFunction<Boolean> dayOffsetToWeather)`,
|
||||
with a public no-arg overload `longRangeForecast()` that delegates and wraps
|
||||
the `scorer` field in a lambda. Do not keep the `String quarter` parameter.
|
||||
(`java-refactor change-method-signature` or `java-refactor introduce-param`
|
||||
may help; fall back to a minimal manual edit if no subcommand can do it.)
|
||||
4. Stop. Report the final diff.
|
||||
|
||||
## Tool rules (variant C)
|
||||
A semantic Java refactoring CLI, `java-refactor`, is on your PATH
|
||||
(`java-refactor --help` lists the subcommands). Using it is OPTIONAL — you
|
||||
may also change code with your normal file-editing tools. Choose per step
|
||||
whatever you think is most reliable.
|
||||
|
||||
Subcommands that may be relevant for this task:
|
||||
- `extract` — extract selected statements into a new private method
|
||||
(selection given as start/end line+column; `--dry-run` previews)
|
||||
- `introduce-param` / `change-method-signature` — alter a method's parameters
|
||||
- `rename` — rename a symbol project-wide
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user