// 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); } }