109 points · by galfarragem · 6y ago · 98 comments
And I was nodding my head in rhythm with the author until he got to the section on lenses. He writes:
That’s 14 lines of code to update one single field. Not only is this single example somewhat confusing to follow, you also need to imagine how this update function will look when taking into account the five or so other fields just in the address record! This is — quite frankly — pretty terrible. The trick here is not to stare out of the window and contemplate rewriting everything in ClojureScript. Instead, the thing to do is write a whole bunch of lenses.
To my eye, the lenses code he ended up writing didn't look all that much prettier or shorter to me. I guess it's more neatly structured and could even be organized its own file?
Is the advantage that you're providing access at each level of the nested record as you code it out? Are there other approaches to this nested field problem?
As for why his lenses code can be better than the 14 lines, you're exactly right, it can be extracted into its own file making it an easy-to-use API made of stable code. That lenses code is simple, though unusual at first glance. Once it's an established pattern in your codebase it'll take 5 seconds to grock an entire file of it.
That's his stance regardless of the situation, and it's a bit annoying if you're legitimately struggling with painful code.
> This [flat Msg design] does work, but at some point it becomes cumbersome to support a large number of constructors.
As guessed, I'd personally stick with the simpler flat approach the article acknowledges and then tries to improve on.
Nesting that data structure is (according to the article) an ergonomics improvement, but then the rest of the article is about how to solve ergonomics problems caused by the very nesting that was supposed to make things nicer!
Given that, surely it's reasonable to raise the question of whether this nesting was actually an improvement after all. My conclusion at the end of the article is that in retrospect nesting did more harm than good, and knowing that, I would have happily left it flat.
Incidentally, "leave it flat" is not my stance in all cases. For example, I gave a whole talk that could have been titled "when to nest" - https://youtu.be/DoA4Txr4GUs - and my most popular Elm project uses nesting in multiple places - https://youtu.be/RN2_NchjrJQ
But indeed, in this case, I'd happily leave Msg flat and not bother with all the lens stuff!
I would typically shuffle all these lenses into their own file, yes.
It doesn't look shorter in the small example, but applied to a real-world example — given that lenses compose — the savings quickly begin to add up. The result (at least, in my experience) is that `update` functions are much shorter and easier to follow.
The author himself admits that getting a value from a nested record is easy, so we don't need lenses to do that.
Setting a value from a nested record can be easily achieved using plain functions -- no need for a complex abstraction.
updatePersonDetails : (PersonDetails -> PersonDetails) -> Model -> Model
updatePersonDetails fn model =
{ model | personDetails = fn model.personDetails }
updateAddress : (Address -> Address) -> PersonDetails -> PersonDetails
updateAddress fn details =
{ details | address = fn details.address }
updateLine1 : (String -> String) -> Address -> Address
updateLine1 fn address =
{ address | line1 = fn address.line1 }
Notice that these functions are almost identical to the ones he wrote in order to build the Lenses.This update functions can be composed easily:
updatePersonDetailsAddressLine1 : (String -> String) -> Model -> Model
updatePersonDetailsAddressLine1 fn model =
updatePersonDetails (updateAddress (updateLine1 fn)) model
or, if you are feeling fancy: updatePersonDetailsAddressLine1 =
updatePersonDetails << updateAddress << updateLine1
On the other hand, nesting records is discouraged because it brings no benefit at all and it creates the problem of having to write extra boilerplate. He wouldn't even have the problem that lenses solve if he had just stuck to the "flat" record.Btw: I think it was a great article! I didn't mean to sound too harsh criticizing; just wanted to clarify that point.
1. What you've written looks quite similar to the lenses approach.
2. I disagree that having nested records brings no benefit at all. What about when the types are generated on the backend so the shape of the model never goes out of sync across that boundary? For a non-trivial size application, I believe it's cheaper to use code generation to derive the type definitions, JSON encoders, and JSON decoders than it is to manually write all of it and try to make sure everyone on your team is disciplined enough to always make those changes.
They're nice if you have nested data structures that aren't records (e.g. naked product types a la "MyType Int Int Int").
But yes in general flat records are the way to go (but what about nested message types? Turns out that the lens hierarchy helps there too in the form of prisms. Or it would be nice if Elm had extensible union types so we could have flat message types too...)
This. Redux is the worst for this.
"Compose your reducers, which magic a state object out of defaults, and split your switch statement across a zillion files, and isn't it amazing how it's just Javascript?" - feels like a direct quote from every Redux tutorial and blog post ever.
I yearn for "make your state an object, and have a big switch statement. Oh, and if that ever becomes unmanageable, only then think about reducers."
It's actually why I don't mind Redux's boilerplate. It's punishment for using Redux when you don't have to.
Seems like we are at a start of a transition period where we will move to more functional languages over the next 20 to 30 years. I wish a large company would design and modern ml and not try to make it backwards compatible with an existing runtime.
If you haven't looked at Redux recently, you should look again. "Modern Redux" code is drastically simpler than the Redux code you've probably seen before. In particular, our official Redux Toolkit package [0] and the React-Redux hooks API [1] make it much simpler to write Redux code, by auto-generating action creators, writing reducers as object lookup tables instead of switch statements, letting you write "mutating" immutable updates in reducers, and providing good defaults out of the box and utilities that handle common use cases.
FWIW, we've just completely rewritten the official tutorials in our docs to show how much simpler "modern Redux" code is, as well as better explain the fundamentals and why many of these patterns exist:
- The "Redux Essentials" tutorial [2] teaches "how to use Redux, the right way", by building a real-world app using Redux Toolkit
- The "Redux Fundamentals" tutorial [3] teaches "how Redux works, from the bottom up", by showing how to write Redux code by hand and why standard usage patterns exist, and how Redux Toolkit simplifies those patterns
In addition, we've added a "Style Guide" docs page [4] that lists our recommended patterns and best practices, which help you write Redux logic that is simpler, easier to understand, and easier to maintain. That includes things like "write Redux logic as single file 'slices'", "put as much logic as possible in reducers", "model actions as 'events', not 'setters'", and so on.
We've gotten a lot of great feedback telling us that RTK and the docs improvements have really made it a lot easier for people to learn and use Redux effectively.
[0] https://redux-toolkit.js.org
[1] https://react-redux.js.org/api/hooks
[2] https://redux.js.org/tutorials/essentials/part-1-overview-co...
[3] https://redux.js.org/tutorials/fundamentals/part-1-overview
Why do you guys complicate things? Why do you think new concepts are required? And new terminology? Why do you write so much code? Why don't you see how these things have been done in the past? This has been done in Mithril for ages.
My suggestion is - next time you guys decide to create something new, don't just keep thinking of new concepts and write code. It's all been done before. Just make it easier to use.
So, no, I'll not be looking at Redux, now or in the future. Because I've got applications to deliver and the end user doesn't give a shit about how fantastic your architecture is, if it takes ages to understand, implement, maintain & debug.
Because times change, ecosystems change, patterns change, and user bases change.
Why isn't React still using `createClass()` and mixins? Why were ES6 classes and hooks invented? Why did JS add arrow functions and `async/await`? Why did C++ add an `auto` keyword? Why is there ever a new version of a language or library? Wasn't enough thought put into them in the first place?
> Just make it easier to use
And that's exactly what we did :)
"Modern Redux" code is different because:
- We've seen how people have used Redux in practice, what they wanted to do, and what problems they've run into
- We've seen what solutions and other Redux-related libraries the community has invented to solve their problems
- And there's new tools and APIs that have come out since Redux was created, like Immer for immutable updates and React Hooks for writing reusable logic in React components
Like I said, this isn't about "new Redux concepts". It's still state, actions, reducers, dispatching, and subscribing in the UI. It's just much less code to do it, and prevention of common mistakes while using Redux. That means smaller bundles, faster development, and fewer bugs.
This tutorial page shows how Redux Toolkit simplifies all the older Redux logic patterns:
https://redux.js.org/tutorials/fundamentals/part-8-modern-re...
I can't force you or anyone else to use RTK, or to use Redux. If you've got other options that are working for you, please use them.
I'm just pointing out that we _have_ specifically addressed the very concerns you're raising, and if you are genuinely concerned about those issues, you _might_ want to take a look at what we've put together... and I've got a whole lot of enthusiastic RTK users who can vouch for how it's made their development lives and Redux usage experience a lot better.
Yeah, RTK is written in TS and specifically designed for a good TS usage experience with a lot of type inference, and the React-Redux hooks API is _much_ simpler to use with TS since it's really just a matter of declaring the type of `state` in your selectors. (Well, okay, to use thunks in components you gotta supply a customized type for `dispatch` that knows it can accept thunks too.)
FYI, we've got some new RTK APIs in the works that are close to alpha status that I think are really gonna surprise people and make some use cases a _lot_ nicer too. Hoping we can formally announce them here in the next couple weeks.
Lemme glance at that repo you listed there...
Oh wow, that's a really impressive project there, and some neat larger-scale use of RTK! Thanks for linking that.
A few quick bits of feedback on your code since I've got it open:
- A couple of those extra middleware look like they could maybe just be hand-written thunks instead, such as `projectMiddleware`
- for `projectMiddleware` in particular, you probably ought to be doing `storeAPI.dispatch(loadProject))` instead of trying to unwrap the thunk yourself
- strictly speaking, a reference to `window.width` shouldn't be in the reducer itself - the value should be in the action, preferably via the `prepare` callback. (In practice it won't really make a difference, just pointing it out as an FYI.)
- always nice to see actual uses of `undoable()` in the wild :)
I hate that it got chosen as the de facto solution, and that there was such a strong push for it as the main state management solution for react. Even though redux devs themselves have come out and said that it's not for every situation. Where does that leave people?
I really think there should be a greater focus to be more ergonomic rather than theoretically good. There should be one package that someone can just jump into. Separating react-redux, redux, and now redux-tookit is just frustrating.
I just get this gut feeling with redux that people just wrote and wrote stuff, without really stopping and squashing it down. The fact that part of the messaging with using a toolkit also involves reading up on the essentials is a problem.
Even if the problem space is very complex there shouldn't be this much information - I think it might be easier to learn some programming languages than the entirety of the redux ecosystem. And frankly, I don't even think the problem is that complicated, the basic flux model isn't that complicated. And yet it's been made to be so complicated. I recently did a refresher, and on the react-redux example, just in the intro of a basic todolist, there's ~50 things that are highlighted - implying you should remember them - in just the section of setting up a store. Not the entire section on stores, just the intro outlining the brief overview of the rest of the tutorial.
There is just so much stuff.
I don't even think the toolkit makes things that much simpler either. It's a step in the right direction, but it feels really leaky when I get into the intermediate and advanced sections.
With the choice to use it or not use it. Before that, they only had the choice to not use it.
I get what some of what you're saying here, but I also feel a couple of these complaints are misguided.
For one thing, the Redux team has never intentionally tried to make Redux "the main state management tool for React". We've never deliberately sold it that way, and goodness knows the React team has said "React is the only official state management tool for React". Redux got popular because it solved the problems people were dealing with when it came out, better than the existing tools in the ecosystem, at a time when React was taking off. We've done our best to clarify in the docs when it does actually make sense to use Redux.
Redux Toolkit is still Redux. You're still writing reducers, creating a store, dispatching actions, and updating the UI based on state. RTK just allows you to do so with less code. So, the "Essentials" tutorial starts by introducing those terms, concepts, and data flow, because you need to understand those regardless of how you're writing Redux logic.
While Redux _is_ normally used with React, it's also UI-agnostic, hence the separation from React-Redux, and using it with other UI frameworks is a valid use case that we support. We had some long debates about whether to add these additional APIs into the Redux core [0] [1], and concluded that there was too much inertia around the existing Redux ecosystem to build those into the core `redux` package. As it is, even though RTK isn't required, we've had folks say they didn't like some of the opinions we've built into RTK (like using Immer for writing reducers). So, combining all three of those packages into a single super-package is not feasible at this point.
I've tossed around the idea of trying to merge the Redux core, React-Redux, and RTK docs into one site, but that would likely require merging all three libs into a monorepo. Not impossible, but not something that's likely to happen any time soon, if only because my own time is very limited and I have a lot of other docs work I need to focus on [2].
Which specific docs page are you referring to? You said "intermediate" and "advanced" - sounds like you're referring to the RTK docs site tutorials [3]? FWIW, I wrote those a year ago, and at the time the main Redux docs were frankly very out of date. I've spent much of this year rewriting the Redux core tutorials, as listed above, and those Redux tutorials are where I would direct anyone who's new to Redux or really wants to know about RTK. We do also have an open issue to discuss what tutorial content should be on the RTK docs site [4], and tbh I'm really not sure _what_ tutorial content should actually be on the RTK site now that the "Essentials" and "Fundamentals" tutorials exist in the core docs.
You're right that the actual Redux core itself isn't very complicated, but the biggest issue is trying to teach people to "think in Redux". In fact, it's kind of _because_ the core is so small that there's a lot of documentation needed - so much of this is actually in the patterns around how to use Redux effectively.
Anyway, if you can point to some more concrete concerns or have specific suggestions on how we can improve things, I'm very interested - please feel free to comment over in the issues.
[0] https://github.com/reduxjs/redux/issues/3321
[1] https://blog.isquaredsoftware.com/2019/10/redux-starter-kit-...
[2] https://github.com/reduxjs/redux/issues/3592
[3] https://redux-toolkit.js.org/tutorials/intermediate-tutorial
Relevant Discourse thread that discussed this:
https://discourse.elm-lang.org/t/idea-extensible-union-types...
As always, Elm's biggest flaw is the sanctimonious nature of its design committee. No native modules, no custom operators, no lenses (would they really ban them?), etc.
That second one even comes back to bite the code here, because compared to e.g. Haskell lenses, the Elm lens code is ugly and verbose. Having to manually write lenses instead of being able to auto-generate them with macros is yet another Elm failing, though to be fair, I don't know that macros are opposed by the committee.
Elm for me is like a refuge from JavaScript where anything goes (good and bad).
It’s not a hammer for very nail, but it’s simple and predictable for the most part.
There are other frameworks like bucklescript / reason, purescript, closurescript, etc. with all the bells and whistles.
Ultimately, I think Elm is in a good place. That being said you have to pretty quickly jump into the deep end once you reach a fairly moderate sized code base.
Monocle isn't the only game in town for lenses. Arguably, you're still going to need to write the code that isn't generated, but the composition of lenses can be done using the generic composition (<</>>) too, which makes them much more elegant.
> updatePersonalInformation
> : Model
> -> (PersonalInformationModel, Cmd PersonalInformationMsg)
> -> (Model, Cmd Msg)
> This is way too complex already, and this approach doesn’t even actually buy you anything.Is this accurate? I feel like this may not be what the author intended to write, since the update function isn't given any kind of message.
The author's approach has data for every page of your app stored in the model at once, and available to every other page. Also, any page can edit the data for any other page and send messages to any other page. That makes me nervous.
We went with having a small amount of global state that was visible to every page, and that every page could update (returning (Model, GlobalState, Cmd Msg)). That seems... basically fine? It's still a bunch of boilerplate, but not that much, and I don't think we've had significant problems with it. We need Cmd.map and Html.map but I don't know why the author thinks this is bad.
(The boilerplate is mostly turning `(model, cmd)` into `(model, globalState, cmd)`. On the other hand, we don't need to wrap each of our messages with `ThisPageMsg`. Most pages don't need global state, and we let them use a regular Msg -> Model -> (Model, Cmd Msg) update function.)
(Oh, I think very occasionally we do need to send a message up to the top-level as well. I don't remember exactly how we do that. It might be something like... those pages' update functions take a `PageMsg` and return a type `PageMsg PageMsg | GlobalMsg GlobalMsg`, and the top-level update function interprets `ThisPageMsg (GlobalMsg ...)` and passes through `ThisPageMsg (PageMsg ...)`. Again, boilerplate, but not significantly problematic, and only on pages that need it.)
To address your other points: in just about every Elm application I've ever worked on, there was an initial ambition to separate and encapsulate things nicely, for reasons of safety or Clean Code or some other philosophical ideal.
Then requirements change (which is normal and good — businesses change and evolve, and the product should mirror that). Then you discover that oh, you need a bit of state from one page in another. Oh, and modifying the state of something on one page should affect this other page. Hmm… Why does this view function now take seven arguments?
Your final paragraph hints at the kind of engineering that inevitably begins to appear as a workaround to solving the wrong problem in the first place. All of it is extra complexity that doesn't need to exist at all.
> The author's approach has data for every page of your app stored in the model at once, and available to every other page. Also, any page can edit the data for any other page and send messages to any other page. That makes me nervous.
Why exactly this emotional reaction specifically? Does prior experience with bad surprises in Elm give you this intuition? Or is it from experience from a language like JavaScript where global mutable state is indeed something to be wary of?
I agree it's extra complexity, and it's not strictly necessary. But it's not that much extra, and it doesn't affect pages that don't need it, which in the app I work on, most don't. (Whereas if all your pages take a top-level model, and return a top-level message, they all need to deal with that even if they don't need it.)
> Does prior experience with bad surprises in Elm give you this intuition? Or is it from experience from a language like JavaScript where global mutable state is indeed something to be wary of?
Not specifically Elm, I've only written it in one app.
But I'm imagining bugs where a page mutates its state in ways that satisfy some invariant, and then that invariant is violated and no one can work out how it happens. Turns out some other page is editing its state, in ways that probably made sense at the time. (Granted the debugger will probably help, but... I dunno, I'd rather just not have to.)
I'm not convinced large Elm apps don't have problems like this. And saying "no, if you want to communicate with another page, you have to do it through this specific mechanism" seems like a fine way of avoiding it.
The solution to this particular problem is to stop thinking in terms of page.
Which page you're on is only a matter of view, and maybe a minimal amount of view-related model. Most of your model data should be related to your application logic, not to a specific page or component. Separating your model logic and your view logic helps a lot to avoid having this problem, because your model logic becomes agnostic to what page modifies it.
updatePersonalInformation
: PersonalInformationMsg
-> Model
-> (PersonalInformationModel, Cmd PersonalInformationMsg)
-> (Model, Cmd Msg)
I know the point was "this is a bad type signature", but I guess I'm still confused. I can't think what problems someone is trying to solve by writing a function like this.I can maybe see why it might have both `Model` and `PersonalInformationModel`: the author doesn't want to prefix everything with `model.personalInformation` so they pass both `model` and `model.personalInformation` to it. (Maybe they forget that they can start the function with `let personalInformation = model.personalInformation`?) So I can maybe guess why `PersonalInformationModel` might be there, and if my guess is right I agree it's silly.
But I have no idea what the `Cmd PersonalInformationMsg` is doing, and it makes me think I don't understand the `PersonalInformationModel` either. Where does that input come from, and what does the function do with it?
Furthermore, I've observed over a few years the behaviour in which developers with significant JavaScript ecosystem experience will impose these philosophical ideals far too early in an Elm application's design because they aren't familiar with Elm's refactoring story and are justifiably wary of a codebase growing to the size and level of complexity that is beyond saving.
I understand and agree with the idea of generally narrowing types to simplify APIs, but in my experience, the typical "page" in a multi-step webform actually contains too many cross-cutting concerns for this approach to remain frictionless as business needs change (and if your business/product needs don't change, then I would like to buy whichever crystal ball you're in possession of).
I can't remember of any concurrency issue arising from handling multiple messages from the view, to be honest. I don't know if there's a specific mechanism blocking them, but it's simply never happened in 3 years of building elm apps for a living.
You don't have any guarantees about when that will occur though. Updates and view rendering are run independently.
I have an application that tracks mouse movement using pointer events for the user to draw, and I've definitely had a message from that come through after my model has updated and I'm no longer on that screen (and so nothing in the view generated from the model should produce that message), so it definitely can happen, even if you haven't encountered it.
Even if you don't encounter it that way, there are a bunch of footguns using this approach (e.g: doing this with tasks, HTTP requests, anything that has a defined delay before the message comes back).
The basic problem is that there is an undefined delay between a message is issued and when `update` is called on the message which can be caused by other messages being in the queue.
Here's an example with subscriptions and views.
Time 0: A subscription arrives with message `A` which is added to the message queue in Elm's runtime. Note that one "tick" in Elm's runtime has not yet passed so we don't process the queue just yet (to see why this can happen we can either look at Elm's source code or note that the fact that Elm doesn't lose a subscription even if updates are busy processing forces there to be a message queue of length > 1).
Time 1: Your view sends a message `B` which contains a snapshot of the model `M0` at Time 1. `B` is added to the message queue.
Time 2: Your update function pops off the message queue and processes `A`, resulting in a new model `M1`.
Time 3: The Elm runtime processes another tick and sees no new messages from either views or subscriptions.
Time 4: Since the runtime is now done examining subscriptions and view messages, your update function pops off the message queue and processes `B`, resetting the model back to `M0`.
At the end of this sequence you effectively haven't handled the subscription at all, and all this is on a single thread.
This can be improved by using coarse-grained actions like say `UpdatePerson(person)`. And this new `person` value can be obtained by the event handler itself when the user changes the name. However, the actual logic can sit inside a Person module, allowing us to do something like: `onChange=dispatch(UpdatePerson(Person.setFirstName(newName, currentPerson))`
This approach lets us keep the behaviour close to the view while encapsulating the actual implementation inside a rich domain model.
The view is generated in its own thread synced to the browser's rendering system. It is entirely possible for the model to have changed without the view having been updated yet to match.
If your model changes under your feet while you use this approach, it result in you silently overwriting that change with the old data.
You have a `numberOfOperations` field in your model that is incremented every time a message is processed and then check that your incoming new model has a `numberOfOperations` value that is exactly one larger than the current value. If not you then fire off a `Cmd` asking for a recomputation of the original message.
However, that can get pretty hairy pretty quickly (since you may not have a nice way of asking for recomputation) so I'm not sure if it ends up working out in the end.
If you don't need prisms or isos, I would suggest looking at https://github.com/bChiquet/elm-accessors, which uses basic composition to make composable lenses/traversals.
I'll make a correction to the article.
I'm not really sure what this means. At Riskbook we have about 50,000 lines of Haskell and about 20,000 lines of Elm running in production. It's working great for us.
A few years later, they laid him off and deprecated their Elm codebase.
If you don’t care about recovering steps or entering mid step from existing data (routing) it’s still pretty straightforward IMO.
Do you have a better suggestion for updating deeply-nested record fields?
But when you don't like how something is done that other people do like, there comes a time when you just need to pinch it off and leave the pot so other people can enjoy it.
For some reason it's small niche languages that really bring out the hubris in some HNers. Maybe because they have a sense of ownership over it by virtue of merely using it, something they know is silly when it comes to larger languages.
But you are wrong about the sense of ownership. The bitterness comes from Evan deciding to make backwards breaking changes to the new version of the language and forcing you to spend days to update your code base.
It’s been so long that I don’t even remember what the changes were and can’t bother going back and look.
One example that stuck in my mind is when he decided that users should not be allowed to use the prime(‘) character in variables name.
It was not because that this character was now used in some other way. It was just him deciding he didn’t like that in variable names, so he not only forced it to the language codebase but to all the users as well.
Of course this was easy to change but gives you an idea. The rest of the changes where in the same spirit but much more difficult to change.
So it has nothing to do with small vs popular language. Imagine if Stroustrup had come out and said that since C+11 variable names would be case insensitive and operator overloading was forbidden.
Using prime characters in bindings is a convention that Haskell programmers (myself included) occasionally use, but it is by no means clearer nor more Googleable. Furthermore, a single project-wide find and replace hardly takes "days".
As a remark: I've been accused of feeling ownership over Elm. What I actually felt was investment in Elm, which is different. And I think it's an entirely reasonable thing for me to feel.
I know you used the word maybe. Still, I'd generally be wary of attributing motives like this towards people.