Skip to content

Commit c14db87

Browse files
committed
refine and publish react/elm post
1 parent 5e5e515 commit c14db87

File tree

2 files changed

+12
-268
lines changed

2 files changed

+12
-268
lines changed

content/posts/the-discipline-of-constraints-elm-usereducer-lessons.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ title: "The Discipline of Constraints: What Elm Taught Me About React's useReduc
33
date: 2025-09-18
44
description: "How enforced discipline in one language can teach you better patterns in another - lessons from crossing the Elm/React boundary"
55
tags: ["elm", "react", "usereducer", "architecture", "functional-programming"]
6-
draft: true
6+
draft: false
77
---
88

99
## The Accidental Teacher
1010

1111
I've been thinking about discipline lately. Not the "wake up at 5 AM and eat nothing but kale" kind, but the more interesting variety: the kind that comes from working within constraints that make bad choices impossible.
1212

13-
Last month, I found myself in an unusual position. After spending several months deep in Elm land - where the compiler is your strict but helpful mentor - I returned to a React codebase that was enthusiastically using `useReducer` everywhere. The whiplash was immediate and instructive.
13+
After spending several months deep in Elm land - where the compiler is your strict but helpful mentor - I returned to a React codebase that was enthusiastically using `useReducer` everywhere. The whiplash was immediate and instructive.
1414

1515
You see, both approaches solve the same fundamental problem: managing complex state changes in a predictable way. But experiencing Elm's enforced discipline first made me realize just how much rope React gives you to hang yourself with - and, more importantly, how to avoid doing exactly that.
1616

@@ -71,7 +71,7 @@ const reducer = (state: State, action: Action): State => {
7171
};
7272

7373
default:
74-
return state; // This line is doing a lot of heavy lifting
74+
return state; // feel free to forget a case, nobody will tell you (or even know)
7575
}
7676
};
7777
```
@@ -162,7 +162,7 @@ You're back in the imperative world of manual cleanup, race condition management
162162

163163
What Elm taught me wasn't that React's approach is wrong - it's that discipline is a muscle that needs exercise. When the language forces you to be disciplined, you develop better habits. When it doesn't, you need to bring that discipline yourself.
164164

165-
After my Elm detour, I found myself writing React code differently:
165+
After using Elm _every day_ at my client's, I found myself writing React code differently:
166166

167167
1. **Never use `any` in action types.** If TypeScript is complaining about your action shape, fix the types, don't silence the compiler.
168168

@@ -205,6 +205,8 @@ type UserState =
205205

206206
This prevents impossible states like `loading: true, error: "Something went wrong"` that can cause confusing UI states.
207207

208+
(More on that in [Making Impossible States Impossible with Functional Dependency Injection](/posts/making-impossible-states-impossible-with-functional-dependency-injection/).)
209+
208210
## The Deeper Pattern
209211

210212
The real insight isn't about Elm vs React - it's about constraint-driven design. Working in a language that makes certain mistakes impossible teaches you to recognize and avoid those same mistakes when they become possible again.
@@ -213,7 +215,7 @@ Elm's constraints taught me better patterns for `useReducer`. The compiler's ins
213215

214216
## Bringing Elm's Discipline to React
215217

216-
If you've never tried Elm but work with `useReducer` regularly, here are some constraints I learned to impose on myself:
218+
If you've never tried Elm but work with `useReducer` (or Redux) regularly, here are some constraints I learned to impose on myself:
217219

218220
- **Exhaustive action handling**: Comment explicitly when you're intentionally ignoring an action.
219221
- **Total state transitions**: Think through what should happen to every piece of state for every action.
@@ -222,14 +224,18 @@ If you've never tried Elm but work with `useReducer` regularly, here are some co
222224

223225
You don't need Elm's compiler to enforce these patterns, but experiencing enforced discipline helps you recognize when you're being undisciplined.
224226

227+
(Actually, IMHO you _do_ need Elm's compiler; I'm trying not to sound Elm biased but who am I kidding 😆)
228+
225229
## The Craft Connection
226230

227231
This connects back to something I've been thinking about regarding [coding as craft](/posts/coding-as-craft-going-back-to-the-old-gym/). Master craftsmen often impose constraints on themselves - not because they have to, but because constraints force innovation and build skill.
228232

229-
The discipline I learned from Elm's compiler made me a better React developer. The constraints didn't limit my creativity; they channeled it in more productive directions.
233+
The discipline I learned from Elm's compiler made me a better React developer. And I'm more mindful with Kotlin and Golang as well, even though most of that stuff isn't functional at all. The constraints didn't limit my creativity; they channeled it in more productive directions.
230234

231235
When you're building state management with `useReducer`, you're not just solving the immediate problem - you're practicing a way of thinking about state, time, and change. The habits you build in one context carry over to others.
232236

233237
The real question isn't "Which approach is better?" It's "What can I learn from this constraint that will make me better when the constraint is removed?"
234238

235239
Sometimes the best teacher is a language that simply won't let you make certain mistakes. Even if you never ship Elm to production, the lessons in discipline are worth the price of admission.
240+
241+
So even if you have no realistic prospect of using Elm (or Haskell, OCaml or any other ambitious functional language) professionally – learning might make you a better developer nonetheless.

content/posts/the-elm-architecture-vs-react-usereducer.md

Lines changed: 0 additions & 262 deletions
This file was deleted.

0 commit comments

Comments
 (0)