Rename split-loop-with-lambda to slice-with-lambda
The refactoring is now published as Slice on sammancoaching.org: https://sammancoaching.org/refactorings/slice.html - git mv ideas/split-loop-with-lambda -> ideas/slice-with-lambda - rename learning-hour file and update frontmatter name - update paths in tools/rehearse scripts and report title - update prose: 'Split Loop with Lambda' -> 'Slice with Lambda' in learning hour, exercise READMEs, storyboard, solution comments - link the published Slice refactoring from the pattern draft
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user