Some kind of pipe operator can make this easier to read by arranging the operations linearly from left to right (or top to bottom as is the case with this library). Although in my opinion this library has its own readability issues.
Sure, it is good most of the time, but operator precedence rules exist for a reason.
I don't want to read
2.chain(x => x * 2).chain(f)
instead of f(2*x)
These patterns have their place for async programming, pure FP, streams and more.They can also be a distraction.
E.g. the library introduces its own promise-unwrapping semantics and more.
I don't see the use for such a generic implementation, although I applaud the effort.
Props to the author for trying out something neat, but this is probably a bit to clever for my liking.
Same here. Not to mention this wouldn't work in a codebase with an autoformatter.The idea/API is clever. So, a pretty cool experiment I suppose
Edit: I just checked the docs again and while it does look a bit "misaligned" at the start, I do think it looks more readable with the autoformatted version. Pretty cool!
I love JavaScript but this pipe thing is horrific.
The clunky syntax removes all the benefits of the pipe operator. The entire thing is syntactic sugar, wrapping it in confusing functions just breaks the entire concept.
const msg = capitalize(greeting) + “!”;
const { status } = await send(msg);
console.log(status);
Now it’s perfectly readable, no weird shenanigans required.I’m obviously not being serious about naming being hard in this example.
But there are cases where the pattern of using return object property names as variables comes back to bite uou.
Even in this case, what if the response object from ‘send()’ also has a ‘msg’ property?
Changing const { status } into const { status, msg } is an error.
We should be cautious about syntaxes that force us to allocate names. Sometimes.
You don't have to write something like "this is the message", you can see it.
But still I’ve done what people advised, for many years. And know what? It never paid back. I’m sort of tired to keep that secret and nod to this obvious overgeneralization and a blanket idea. Pretty sure I’m not alone.
[1] https://github.com/lua/lua/blob/6baee9ef9d5657ab582c8a4b9f88...
const foo = capitalize(underline(reverse(exclaim(indent(value)))));
I already forgot the amount of parentheses I needed to close with while typing that. value.indent()
.exclaim()
.reverse()
.underline()
.capitalize()
There’s always the OOP version of method chaining!I think it could be done today with a (big) number of ESLint plugins.
Better that you either avoid that pattern or at least indent it in such a way that its not so visually confusing. Or yknow use a pipe operator.