News for hackers. Stuff that matters.
Re:Show HN: A JavaScript function that looks and behaves lik... by smilingemoji on Wednesday September 27, 2023 @05:35AM (#37672333)

  The following example code is a bit hard to read:

  const { status } = await send(capitalize(greeting) + "!")
  console.log(status)
I disagree, I find this example code very easy to read because it reads like idiomatic Javascript. Unlike this library.
Re:Show HN: A JavaScript function that looks and behaves lik... by Piisamirotta on Wednesday September 27, 2023 @06:11AM (#37672624)
I agree, I really don't understand this library
Re:Show HN: A JavaScript function that looks and behaves lik... by knubie on Wednesday September 27, 2023 @06:20AM (#37672680)
I think the implication is that it's difficult to read because it's read non-linearly. You first need to start in the middle at `greeting`, then work your way out to `capitalize` on the left, then to `+ "!"` on the right, then work your way out one more layer to the left with `send`.

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.

Re:Show HN: A JavaScript function that looks and behaves lik... by moritzwarhier on Wednesday September 27, 2023 @12:36PM (#37677273)
I agree. In the example case, I don't see why left-to-right or top-to-bottom would be more readable.

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.

Re:Show HN: A JavaScript function that looks and behaves lik... by madeofpalk on Wednesday September 27, 2023 @06:34AM (#37672764)
Right - because this library tries to fake a new language syntax feature without the actual support for it, it ends up being the worst of both worlds. Even ignoring the lines-of-code explosion, the non-standard use of parens/spacing makes it incredible difficult for me to parse.

Props to the author for trying out something neat, but this is probably a bit to clever for my liking.

Re:Show HN: A JavaScript function that looks and behaves lik... by spoiler on Wednesday September 27, 2023 @07:33AM (#37673158)
> the non-standard use of parens/spacing makes it incredible difficult for me to parse.

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

Re:Show HN: A JavaScript function that looks and behaves lik... by FroLeMageBlanc on Wednesday September 27, 2023 @08:07AM (#37673464)
Author here. Thanks for your nice comment. I added a note to say it does work with autoformatters too. Actually someone in a comment below said he or she prefers the autoformatted syntax.
Re:Show HN: A JavaScript function that looks and behaves lik... by spoiler on Wednesday September 27, 2023 @10:24AM (#37675120)
Oh, that's good to know! I haven't tried it and assumed an auto formatter might misalign some of it or put things on the same line, so the visual "downward arrow" appearance would be lost

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!

Re:Show HN: A JavaScript function that looks and behaves lik... by newtom on Wednesday September 27, 2023 @10:49AM (#37675449)
Just an FYI to folks, there has been a proposal to add pipes to JavaScript for a few years:

https://github.com/tc39/proposal-pipeline-operator

Re:Show HN: A JavaScript function that looks and behaves lik... by BerislavLopac on Wednesday September 27, 2023 @06:50AM (#37672851)
Which supports the age-old notion that readability is in the eyes of the reader.
Re:Show HN: A JavaScript function that looks and behaves lik... by gnfargbl on Wednesday September 27, 2023 @07:23AM (#37673073)
If you read all the text, it seems like the entire repo is intended to be a satire on https://github.com/tc39/proposal-pipeline-operator.
Re:Show HN: A JavaScript function that looks and behaves lik... by code_runner on Wednesday September 27, 2023 @07:38AM (#37673214)
Unfortunately I’m not getting satire vibes from that blurb. I’m only more scared for JavaScript.

I love JavaScript but this pipe thing is horrific.

Re:Show HN: A JavaScript function that looks and behaves lik... by keb_ on Wednesday September 27, 2023 @09:01AM (#37674017)
Tell that to Unix/FP lovers.
Re:Show HN: A JavaScript function that looks and behaves lik... by code_runner on Wednesday September 27, 2023 @10:40AM (#37675326)
hahaha i mean this implementation. piping things in the shell is great
Re:Show HN: A JavaScript function that looks and behaves lik... by keb_ on Wednesday September 27, 2023 @12:24PM (#37677061)
Ahh OK. Yeah, I always find trying to shoehorn language semantics via functions to be kinda awkward. The best attempt I've seen is this addition of async/await to Lua, which actually looks pretty usable [1].

[1] https://github.com/ms-jpq/lua-async-await

Re:Show HN: A JavaScript function that looks and behaves lik... by jeroenhd on Wednesday September 27, 2023 @09:57AM (#37674731)
Is it? I think it's an implementation of the pipe operator rather than satire of it. Javascript doesn't provide the flexibility to extend the language with pipe operators (though tools like Babel will let you use the pipe operator already).

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.

Re:Show HN: A JavaScript function that looks and behaves lik... by pwdisswordfishc on Wednesday September 27, 2023 @11:49AM (#37676474)
So what exactly does it satirize?
Re:Show HN: A JavaScript function that looks and behaves lik... by afavour on Wednesday September 27, 2023 @07:26AM (#37673108)
I agree with the author that it’s a little confusing, but that’s why I wouldn’t write code like that.

    const msg = capitalize(greeting) + “!”;
    const { status } = await send(msg);
    console.log(status);
Now it’s perfectly readable, no weird shenanigans required.
Re:Show HN: A JavaScript function that looks and behaves lik... by jameshart on Wednesday September 27, 2023 @07:37AM (#37673204)
But at what cost? You had to name two intermediate variables - and naming things is hard.
Re:Show HN: A JavaScript function that looks and behaves lik... by bassdigit on Wednesday September 27, 2023 @07:50AM (#37673314)
Not hard: msg can be derived from the parameter name of the send() function and status is a fixed attribute of the result.
Re:Show HN: A JavaScript function that looks and behaves lik... by jameshart on Wednesday September 27, 2023 @09:55AM (#37674697)
Even worse - you’re polluting your local namespace with names decided on by other code.

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.

Re:Show HN: A JavaScript function that looks and behaves lik... by bassdigit on Wednesday September 27, 2023 @10:24AM (#37675108)
Still not hard: In that case, instead of msg use sendMsg AND/OR in the second statement use { status, msg: resultMsg }, or just result and later result.msg.
Re:Show HN: A JavaScript function that looks and behaves lik... by colordrops on Wednesday September 27, 2023 @07:54AM (#37673352)
Because they provide value and document the chain of events. I fear a long list of piped expressions without any idea of what they are intended to to. Would be like big regexps.
Re:Show HN: A JavaScript function that looks and behaves lik... by progx on Wednesday September 27, 2023 @07:54AM (#37673353)
In this case the variable is the documentation of the code.

You don't have to write something like "this is the message", you can see it.

Re:Show HN: A JavaScript function that looks and behaves lik... by amadeuspagel on Wednesday September 27, 2023 @09:04AM (#37674053)
What if it isn't a message? What if "msg" is already used for another variable?
Re:Show HN: A JavaScript function that looks and behaves lik... by afavour on Wednesday September 27, 2023 @09:07AM (#37674113)
This is some wonderful performance art you’re doing here
Re:Show HN: A JavaScript function that looks and behaves lik... by Bjartr on Wednesday September 27, 2023 @09:47AM (#37674606)
`greetingMsg`? `greeting`? Naming the other one differently? Splitting the work done with that message and the work done with this message into their own functions?
Re:Show HN: A JavaScript function that looks and behaves lik... by wruza on Wednesday September 27, 2023 @02:04PM (#37678634)
Frankly, this always sounded like a readability issue seeking a readability problem. Splitting because the names clash, because you decided to name things, because a one-liner was less than ideally readable.
Re:Show HN: A JavaScript function that looks and behaves lik... by rickstanley on Wednesday September 27, 2023 @10:17AM (#37675012)
"_msg", then.
Re:Show HN: A JavaScript function that looks and behaves lik... by vikingerik on Wednesday September 27, 2023 @10:28AM (#37675164)
Code is read many more times than it is written. Put in the minute or two to devise good intermediate variable names, and that will pay back manyfold every time someone needs to read and understand it.
Re:Show HN: A JavaScript function that looks and behaves lik... by wruza on Wednesday September 27, 2023 @05:28PM (#37681588)
Some code. E.g. most of my code was barely read by anyone, as most of it was utilitary or mvp or niche/local enough to never see big light. And when from time to time I read other code, e.g. on github, in projects I’m occasionally interested in, there’s barely anything resembling these high class advices. FOSS with large reach - yeah. But a library that you found for a specific hardware is “just code”. And to be honest, the difference between the two is not that big. Any decent programmer navigates both almost equally fast. As someone who modified Lua internals (typical example [1]) for employer’s needs I can say that I prefer Lua style to ceremony on top of ceremony, to multiparagraph explanations of what should be obvious to a competent developer, and to hundreds of extracted functions for the sole reason of clean-ish scopes or whatever.

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...

Re:Show HN: A JavaScript function that looks and behaves lik... by marcus_cemes on Wednesday September 27, 2023 @10:36AM (#37675279)
This is clean JavaScript syntax in my opinion and should be what people strive for. It's perfectly readable, it's faster, it does async correctly without any unnecessary computation, can be typed and will have a normal stack trace. Piping is cool when done right, but can introduce complexity fast. Elixir is a good example where it works wonderfully.
Re:Show HN: A JavaScript function that looks and behaves lik... by weego on Wednesday September 27, 2023 @07:59AM (#37673390)
Yeah, it's concerning when even the contrived first example is less clear in the 'solution' than it is in the alleged problem.
Re:Show HN: A JavaScript function that looks and behaves lik... by keb_ on Wednesday September 27, 2023 @09:05AM (#37674085)
The strength of the pipeline operators comes when you have to do method chaining things like:

  const foo = capitalize(underline(reverse(exclaim(indent(value)))));
I already forgot the amount of parentheses I needed to close with while typing that.
Re:Show HN: A JavaScript function that looks and behaves lik... by williamcotton on Wednesday September 27, 2023 @09:28AM (#37674368)

  value.indent()
    .exclaim()
    .reverse()
    .underline()
    .capitalize()
There’s always the OOP version of method chaining!
Re:Show HN: A JavaScript function that looks and behaves lik... by jonahx on Wednesday September 27, 2023 @10:08AM (#37674879)
This won't look as good when you include all the boilerplate which defines those methods, and which needs to be repeated or otherwise included for every "value" type. You don't have to worry about this when they are pure utility functions.
Re:Show HN: A JavaScript function that looks and behaves lik... by mhink on Wednesday September 27, 2023 @02:23PM (#37678960)
Which is great until Prettier, in its infinite wisdom, decides it's better to stick four of these function invocations on one line and two on the next, so you have an obnoxious mishmash of tokens!
Re:Show HN: A JavaScript function that looks and behaves lik... by pzmarzly on Wednesday September 27, 2023 @02:57PM (#37679518)
Which is a problem of Prettier. I wish for the day that someone builds rustfmt but for Typescript, I will happily leave Prettier and StandardJS behind.

I think it could be done today with a (big) number of ESLint plugins.

Re:Show HN: A JavaScript function that looks and behaves lik... by rat9988 on Wednesday September 27, 2023 @09:15AM (#37674213)
No worries your ide will help you for the parentheses.
Re:Show HN: A JavaScript function that looks and behaves lik... by keb_ on Wednesday September 27, 2023 @10:32AM (#37675216)
IDEs and linters should not be a crutch methinks because not everyone uses the same IDE or text editor. Either way, it will still be awkward to read.

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.

Re:Show HN: A JavaScript function that looks and behaves lik... by fshr on Wednesday September 27, 2023 @05:44PM (#37681771)
People do tend to use the same linter and LSP. That’s why they exist in the first place.
Re:Show HN: A JavaScript function that looks and behaves lik... by slmjkdbtl on Wednesday September 27, 2023 @09:15AM (#37674209)
It's funny that their example is much harder to read
Re:Show HN: A JavaScript function that looks and behaves lik... by kderbyma on Wednesday September 27, 2023 @09:41AM (#37674525)
Agreed.