3 ms·
The justification for shared state that the author presents is that threading a lot of (mutable) state as parameters through a tree of SRP-obeying internal func
by Sharlin 7y ago
The justification for shared state that the author presents is that threading a lot of (mutable) state as parameters through a tree of SRP-obeying internal functions gets unwieldy. Factoring that state into the fields of a class and making those functions methods is, of course, the ”obvious” solution when you’re thinking inside OOP box.
It is also completely isomorphic to the obvious imperative solution of factoring the state into a struct/record and passing that around. Both approaches have the same problem of being based on mutable state.
The obvious solution to a functional programmer is to make the state immutable and make the functions return the new state instead of modifying it in place. Furthermore, each function should only take the state it actually needs. Of course, this doesn’t really help with the unwieldiness, especially in a language without helpers such as the Clojure `->` threading macro, or a reasonable lens library (and lenses themselves are not the most obvious abstraction ever), so I guess YMMV. But getting rid of mutable state is often a win anyway.
- BubRoss 7y agoIf state is immutable, then it's constant data, not state.
- Sharlin 7y agoAu contraire! Languages with no mutation at all, like Haskell, still have a well-defined concept of state. If anything, it's more well-defined than in imperative languages based on side effects. I refer you to Rich Hickey's "Values and Change: Clojure’s approach to Identity and State" [1]. [1] https://clojure.org/about/state https://clojure.org/about/state
- BubRoss 7y agoThose are both just hiding mutation and copying.