Skip to content

Refactoring 3/35: Replace global state with parameter - #16

Open
kryptt wants to merge 2 commits into
masterfrom
refactoring/replace-global-state-with-parameter
Open

kryptt wants to merge 2 commits into
masterfrom
refactoring/replace-global-state-with-parameter

Conversation

@kryptt

@kryptt kryptt commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

Entry 3 of the refactoring catalogue: Replace global state with parameter.

  • Page at /refactorings/replace-global-state-with-parameter/ with the full entry shape: Motivation first, The move, To and from (koan), three Before/After example pairs in Scala 3 + Haskell, Pitfalls with the functional-reading footnote, Verification, 11 references.
  • Every reference verified with a skeptic pass; the failures are recorded in LESSONS.md (Fowler bliki GlobalVariables.html/Singleton.html and refactoring.com/catalog/globalData.html are 404 — the smells are cited from the 2e book; Hevery's dead blog is cited via the Google Testing Blog post that quotes it; "State in Haskell" says state transformer, not "state-passing style").
  • Examples: 01 a hidden setting (global constant becomes a leading parameter), 02 a shared policy record read by a fold step (whole record becomes one parameter; Haskell partial application at the fold), 03 a global mutable fresh-number supply (Scala var / Haskell top-level IORef) threaded as explicit state through a recursive walk — in Haskell the After entry point is pure again, and the signature records it.
  • sh pages/refactorings/replace-global-state-with-parameter/run.sh ends with all properties passed.
  • Skill memory updated in the same PR: LESSONS.md entry for this run + a withTests clarification in brief.md (it is a member of Test, not Property).
  • .gitignore gains /vendor/ for bundle config set --local path vendor/bundle (system gem dir is not writable on this machine).

Mutation check (review round)

Example Mutant Language Caught by
1 fee ignores its parameter, hard-codes 300 Scala, Haskell purpose property (agreement passes)
1 fee applied twice Scala agreement property
2 < → <= at the overdraft boundary Scala, Haskell agreement, shrunk to List(-30) within 5 tests (500-test property)
2 limit hard-coded, parameter ignored Scala purpose property (agreement passes)
3 supply increments by 2 / never Scala both properties
3 right subtree numbered first Haskell label properties
3 forces the Lit payload Haskell laziness probe only

No mutant survived; wrong-typed mutants were not used.

Adversarial probes

  • Haskell laziness: a Lit undefined payload survives the numbering in both versions; the mutant that forces it fails the probe.
  • Evaluation order: labels come out exactly x0, x1, … in pre-order on generated trees (self-consistency oracle).
  • Concurrency: example 3's Haskell spec uses checkSequential because the properties share Before's top-level IORef.

Verification

  • bundle exec jekyll build exits 0.
  • Headless-Chrome screenshots at 1280 and 390 wide: panes stack and scroll internally, no horizontal page scroll; diagrams render in dark (browser) and light (rsvg on white) — all SVGs are currentColor.

Preview: https://www.constructive.dev/pr-preview/pr-16/refactorings/replace-global-state-with-parameter/

Entry page, three checked example pairs (Scala 3 + Haskell, hedgehog),
diagrams, catalogue link, and the skill-memory update for this run.
sh pages/refactorings/replace-global-state-with-parameter/run.sh ends
with 'all properties passed'.
@github-actions

github-actions Bot commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor
PR Preview Action v1.8.1

QR code for preview link

🚀 View preview at
https://www.constructive.dev/pr-preview/pr-16/

Built to branch gh-pages at 2026-09-04 01:01 UTC.
Preview will be ready when the GitHub Pages deployment is complete.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant