by ljm · 5y ago · 0 comments
Let's break down the terminology:
- hooks
- useEffect
- useHistory
- useSelector
- dispatch
- action
- useDispatch
- ducks
- Actions
- Epics (Observables)
- Sagas (generators)
- Reducers
Most of those terminologies don't come from a general programming paradigm, but are the domain language of specific libraries. And I've seen more, with some places establishing patterns like Compound/Molecule/Atom and such. There's no consistent, underlying principle with them, though. If you're invested in Sagas then you're stuck with a bizarre threading model implemented with generator functions and it's pretty much all-or-nothing. If you're invested in Redux then there's no breaking out of that either, you're always going to be contending with shared global state.
I really hate to think how many collective hours have been wasted on new coders who learn JavaScript as their first language --- especially when starting with React and Redux, as it's often taught --- and end up spending most of their time spinning their wheels on these domain-specific complexities, when they haven't even gotten to "while loops" yet. This may even be harder for beginners than understanding, say, one of the classic CS sorting algorithms. New coders don't realize though that whatever degree this stuff is harder, it's a sign of bad tooling or documentation, not that it's somehow intrinsically a harder problem in math / CS, since if you're new you don't know what to expect
That alone is a concern, because any junior programmer getting an introduction through React is going to get a twisted understanding of the language itself.
But in terms of the industry-standard practices/ecosystem, it is a terrible first language if you want to gain a generalized understanding. What you describe here is extremely true; I've seen it in practice:
> any junior programmer getting an introduction through React is going to get a twisted understanding of the language itself
I've talked with otherwise very capable JS devs who have built impressive things with multiple frameworks, but don't have a good handle on fundamental concepts like the difference between the two kinds of function syntax (they may have only ever used one), how Promises behave outside of a narrow subset of situations, what NodeJS actually is (despite having used it), or even the difference between passing by reference vs value.
Many of these frameworks create such a narrow lens into the language itself, add what are effectively (sometimes literally) their own DSLs on top, sprinkle in some magical behavior, and when all is said and done you hardly end up using JS at all.
Obviously there's a benefit from doing things this way or it wouldn't have happened. But I can't help but think the technologies that work well for industry are having a detrimental effect on a generation of new programmers.
And so much of the industry everywhere is about “do X to get Y result” and any further reflection is kindof a luxury. How does your package manager work? Part of the point is not to know, which works until it doesn’t. So we all get trained into basically a culture of issuing commands in a manner not too different from the machines we ask to do the work, and developing a model of what’s going on is either bonus study or earned by running your hands over the sharp edges when you’re bucked onto the unhappy path.
Which definitely happens outside of JS. I suspect it happens more inside because JS functions as maybe the biggest funnel into the field.
There is more than 2 now. :)
You have the keyword function, you have
const myFunc = (param) => { //body }
and then you have the various types that can occur within a class, which can have a little bit different behavior based on which syntax you use.
Fun times!
And remember, arrow functions within a class have a this, but arrow functions outside of a class don't have a this!
You can only pass by value in JS so maybe that's why. It is a bit confusing because of how objects work that it feels like you are passing by reference. You are passing reference as the value.
That said, vanilla JS can be hard like this as well, especially as "solved" legacy issues still regularly crop up in tutorials and code-bases. Many times I remember helping newer folks with challenges they found online that seemed to them like they were testing hard computer science problems, but were more just trivia to navigate some bad design decisions of JS (eg gotcha problems on "this" / binding, IFFE, hoisting, async/promises/callbacks, to name a few)
As a beginner, you should start with plain JS/TS. When you dealt with the hell of manipulating DOM elements all over the place and untraceable UI updates after hours of debugging, you should move on to some UI library. Only then will you understand and see what problem it solves. Then you should go with React, but not hooks or redux. Use plain old classes. When you felt the pain of distributed mutable state all over place, you will be open to hooks or redux etc. Still, do not add a library for thunks. Only after you realize how much boiler plate code you write to deal with async stuff, you will see a value in the added abstraction.
A beginner should start simple, and really feel the pain. Only then will there be an "a-ha!" moment where you can judge if a library/framework actually helps you and provides value.
The biggest issue in FE world is people proclaiming some given conglomerate of a library/framework soup as the silver bullet to all problems.
Don't feel to bad for them, that's our entire craft. `while(true)` and sorting algorithms are not.
Bad (but popular) tooling isn't new --- I remember learning PHP4 was like an encyclopedia of gotchas and defensive programming --- but I feel that even a disaster like PHP4 was a lot more approachable for new coders than the state of the art now, and that the skills learned overcoming these things were more portable outside of the framework / language. All of this definitely makes my job more secure, but, I dunno, still feels like a real shame.
- Dependency Injection/IoC - Containers, Resolvers, Scopes - Microservices (Circuit Breaker anyone?) - DDD + CQRS (i could fill a whole page with terms coined by those) - Mediator Pattern etc.
If you buy into a certain techstack, it will always bring associated patterns that have nothing to do with the original language/idiom.
Once you understand hooks in general, any specific hook should be quite straightforward so they can collapse into one. Hooks are quite novel, but I don't find them to be overly complicated unless you're dealing with an already complex situation.
Dispatch, actions and reducers can be collapsed into one since they rely on each other. I think this paradigm is not too dissimilar to message passing with something like RabbitMQ. Reducers are a common concept.
Ducks and Actions, Epics and Sagas are just a convention. It's not required to use React or Redux.
If you try to learn everything at once, of course you're going to get overwhelmed. But the same is true for almost any area of Software Engineering.
To me the proxy object (or proxy class) is really the best abstraction - much like valtio. Or a compiler like svelte.
There’s no reason for ducks, epics, sagas, etc etc. We have a programming language already, we should use it.
It's the same explaining a fully kitted out backend (Routing, caching, authentication, etc) and then going "see, it's so complicated!". Well duh, it's a big and complex system.
I think the issue with valtio is that there's no central store. As far as I can see, you'll end up having these proxies scattered around your codebase or you'll end up re-creating Redux and centralizing them.
Also, I have ironically never heard of valtio.
Also, you can easily wrap valtio with:
const createStore = (name, store) => {
if (window.stores[name]) throw 'store already exists'
window.stores = {
...window.stores
[name]: valtio(store)
}
}
And boom, you can see every store just like redux.If you ever did VB development in the 90s, you will know how easy it was to get a basic app running with those fancy Drag'n'Drop RAD tools. But there was no networking, everything worked offline. But then came the web and the networks, and now everything has to be a distributed system.
Dispatch is how you emit an event (pub in pub/sub). Action is the event being emitted.
Ducks is a code organization paradigm (colocation, organize by feature instead of layer).
Reducers are what they sound like: a reduce function.
Sagas and observables are how to perform side-effects in your application.
There is nothing new here. You could argue that hooks are the weird ones but they are heavily inspired by algebraic effects.
Not really? Most of these concepts can be linked back directly to earlier from CS theory and/or other languages. For example, Redux taking inspiration from FRP and functional lenses, or (most pertinent for this thread) hooks being a facsimile of algebraic effects in JS. As much as I like these concepts in general, I feel some go too far against the grain of what is idiomatic in the language and incur unnecessary friction/spooky action at a distance because of it. For example, here [1] are some well thought-out critiques of hooks and why people struggle with them from experienced JS framework authors.
I agree. Frontend is tough, but so is development. ;)