You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
12
12
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.
14
14
15
15
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.
returnstate; //This line is doing a lot of heavy lifting
74
+
returnstate; //feel free to forget a case, nobody will tell you (or even know)
75
75
}
76
76
};
77
77
```
@@ -162,7 +162,7 @@ You're back in the imperative world of manual cleanup, race condition management
162
162
163
163
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.
164
164
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:
166
166
167
167
1.**Never use `any` in action types.** If TypeScript is complaining about your action shape, fix the types, don't silence the compiler.
168
168
@@ -205,6 +205,8 @@ type UserState =
205
205
206
206
This prevents impossible states like `loading: true, error: "Something went wrong"` that can cause confusing UI states.
207
207
208
+
(More on that in [Making Impossible States Impossible with Functional Dependency Injection](/posts/making-impossible-states-impossible-with-functional-dependency-injection/).)
209
+
208
210
## The Deeper Pattern
209
211
210
212
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
213
215
214
216
## Bringing Elm's Discipline to React
215
217
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:
217
219
218
220
-**Exhaustive action handling**: Comment explicitly when you're intentionally ignoring an action.
219
221
-**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
222
224
223
225
You don't need Elm's compiler to enforce these patterns, but experiencing enforced discipline helps you recognize when you're being undisciplined.
224
226
227
+
(Actually, IMHO you _do_ need Elm's compiler; I'm trying not to sound Elm biased but who am I kidding 😆)
228
+
225
229
## The Craft Connection
226
230
227
231
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.
228
232
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.
230
234
231
235
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.
232
236
233
237
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?"
234
238
235
239
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.
0 commit comments