Add three C# demo examples for Slice with Lambda

- MetaphorFree.cs: guard -> SomeDependency.mightThrowWhenPreconditionNotSatisfied(a) -> tail,
  capture-to-parameter story for the deck
- WeatherStation.cs: SprinklerController, LightSensor.Read() byte -> SunExposure, tail branches
- IceCreamShop.cs: third repetition, Thermometer byte -> Warmth, tail branches
This commit is contained in:
Your Name
2026-09-29 13:15:45 +00:00
parent 60b1a02ea4
commit c153aba61e
3 changed files with 204 additions and 0 deletions
@@ -0,0 +1,54 @@
// 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);
}
}