Why I don't chain everything in JavaScript anymore
Posted by BlondieCoder@reddit | programming | View on Reddit | 19 comments
Posted by BlondieCoder@reddit | programming | View on Reddit | 19 comments
king_Geedorah_@reddit
I find chaining very clumbersome in non Haskell languages (langs without higher kinded types). Rust probably does it the best besides Haskell in my experience.
I feel excessive chaining in a dynamically typed lang would be impossible to bugfix tho
beders@reddit
Why would it be „impossible to bugfix“? For any builder of sufficiently complexity you will need unit tests. Statically typed or not.
That said: builder pattern is less useful if your language is using immutable data. There’s no „building“. There’s transformation to turn something into the data shape you might want.
lelanthran@reddit
Only if you're using a dynamically typed language. In a statically typed language you don't need tests to check the type types returned by each stage of a pipeline.
That's probably what parent means by "impossible to bugfix" - not really impossible, but now a lot of extra code to test the typing, code which is only needed because the typing is enforced by the runtime.
beders@reddit
Any kind of builder needs unit tests. You might need different ones if you have types. Types don’t save your 🥓 especially if they are non-final.
But, yes, a dynamically typed language has tradeoffs for sure.
Kered13@reddit
There is a sweet spot in the middle. Too little chaining produces too many temporary values that are hard to keep track of. Too much chaining can be hard to debug and reason about.
BuriedStPatrick@reddit
I genuinely don't understand the opening example.
Huh? Why not just put it into a function "getTopActiveUserNames" or something?
I am not a chaining fanatic, quite the opposite. But I gotta' be honest, simple filtering and mapping is exactly where chaining reads a lot clearer to me.
How it's "unclear" in the first example is that you're storing it in a
const result. Like, what does that even mean? Why not store it with a name that describes it better?If you need to break this down even further, I'm sorry, but I question your reading comprehension.
imbev@reddit
You can even name intermediate steps like this:
Mirko_ddd@reddit
I see this "problem" a lot also with Java Streams. People really like to make all the work in one go, like it's faster. Luckily there s a friend in Intellij called "extract to variable/method".
amakai@reddit
In Java it's sort of even worse, because most operations on streams are "lazy", and not invoked until you actually invoke the "collector". Which means that you can't easily debug step 1 or 2 or 3, you have to debug entire pipeline.
azhder@reddit
Your only tool is the "chain" as you call it, you apply it to everything, you notice it doesn't work. Well, don't apply it to everything. There is the
composefunction, there is thetapfunction, there is the functor and monad... What do you think a Promise's.then()or an equivalent is supposed to do if not help you compose non-unary and even asynchronous functions?is an equivalent to the sequential programming so much so that JS added the
awaitkeyword to make it even simpler to read and maintain.is an equivalent to the "chaining" you are talking about. You're just not as explicit as to where the container of the mutable state is and possibly less strict about it (good or bad? depends).
Bitter-Apple-7929@reddit
I do feel chaining every thing make it clean and reduce lines of code. But you need to spend relatively more time to fix if same issue comes later.
Mirko_ddd@reddit
I strongly disagree
yes. I could also never insert a newline to reduce lines of code, but would be dumb, aint?
Bitter-Apple-7929@reddit
Why people have down voted this? Anything wrong?
Fred2620@reddit
Reducing lines of code isn't a worthwhile objective if the end result is code that is harder to read though. You might be optimizing for the wrong thing.
Bitter-Apple-7929@reddit
Agreed
ignorantpisswalker@reddit
I agree.
How do you debug this kind of code?
Bitter-Apple-7929@reddit
We can’t put breakpoints in between the chained statements. So need to just change and try or were some logs
ignorantpisswalker@reddit
So this method is write only.
Who debugs code anyways, right?
trmetroidmaniac@reddit
This isn't just about chaining, it's a general question in composition and it comes down to terseness, taste and readability.
You can break down expressions and name them or build them up and leave them point-free. It's probably unnecessary to subdivide
but the variables in
are clearer.
A coordinate type can just be a tuple, a User type is better with named fields.