Hacker News new | ask | show | jobs
by Fragoel2 862 days ago
"[For writing complex scripts] JavaScript is a perfect choice".

Not sold on that. Why should I choose it over Python, PHP or others?

7 comments

yea same here. I wrote nodejs programs for years. the focus on the eventloop and everything being asynchronous is great for servers and network IO but does NOT make it the perfect choice for scripts. Its a different use case entirely.

scripts are supposed to be small simple programs that run in sequence and terminate. I shouldn't need to deal with concurrency primitives at all in that particular situation.

With modern JavaScript (as in last 5-7 years) it's pretty straightforward to write sequential scripts.
There's more to it than writing a simple sequential python, ruby or perl script. You have to write everything using async/await.
async/await are dead simple. And many Node libs have synchronous versions. It's pretty straightforward if you are familiar with the language. If you aren't, well that isn't really the fault of JS nor any different from bash or python, which can both be just as painful for those unfamiliar.
> scripts are supposed to be small simple programs that run in sequence and terminate. I shouldn't need to deal with concurrency primitives at all in that particular situation.

I suppose bash pipes are a concurrency primitive, but I don't object to them. I think it's more about appropriate primitives.

Because you (i.e. I) already use it and have the full toolchain set up for your project?
100%

Use whatever you're proficient in. For A LOT of devs that means js.

Why spendtime pulling python into my js project?

Modern js is not what it was. Ergonomics of nodejs v20 are not in the same ballpark as node v6. A lot has happened since v12 even.

Js definitely has become a pleasant language to script in. And it's sooooo much faster that python and Ruby.

> And it's sooooo much faster that python and Ruby.

That advantage becomes vanishingly small when the language task is to glue together a bunch of spawn/fork/exec to external processes.

If the glue code somehow needs actual heavy processing, that processing can be factored out into another program dedicated to that, leaving only glue code. Side effect: the heavy processing code immediately becomes composable!

Still the same footguns for the sake of backwards compatibility. Things like:

  typeof '' == 'string'
  false
And many others that I'm are too depressing to mention. This is the latest one that I got surprised with.

Yes, it's fast but so is Lua, Dart or even Racket.

wha?

    Welcome to Node.js v19.9.0.
    Type ".help" for more information.
    > typeof '' == 'string'
    true
???
Can I qoute my comment verbatim back at you?

Seriously. Just don't use ==. Use ===. Et voila you're using a modern pleasant scripting language.

Modern guides on js won't ever mention ==. As a noob today you won't ever learn it. In a big project there will be a linter to stop you

Especially because these languages are only one package/install away and not two. I don‘t really get for which audience is targeted here. Usage in JS projects maybe, but then why not write it as npm tasks. ..

I‘m playing around with dotnet-scripts [1] at the moment (C# shop mainly) and this has the same issue imho. The reason why I looked into it was because we have developers not accustomed to bash etc. I still find it silly and would rather use ruby so…

[1] https://github.com/dotnet-script/dotnet-script

> why not write it as npm tasks.

Do you mean writing the entire script in a single line in a string in a JSON file (package.json)? Of do you mean that string just calling out to a non-inline script? Because the use case for this is very much that latter script - it will often still be started via `npm run`.

It's asynchronous, which is pretty neat
A script, by its very nature, is synchronous. You run, you get a result. If you want a service, don't use a script.
That’s a completely arbitrary limitation which should not exist. When my scripts contain independent non-instant subprocesses, I run them in parallel. I’m also using js/ts to write all my scripts, because why wouldn’t I use a proper programming language instead of that “bash” thing that can’t even handle its single datatype well and resorts to all sorts of gibberish to perform trivial operations on it.
This is pedantic to the point of being conversationally hostile.
How? Pipes are not synchronous
> Why should I choose [JavaScript] over [...] PHP [...]?

LOL dude

made my day

PHP is a surprisingly capable scripting language. Some relatively recent version is installed by default on a lot of Linux distros and its standard library has tons of functions for dealing with strings, files, and the network.
This [pun very much intended]
Where's the pun in that?
I’m assuming it’s this https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... (no pun intended)
`this` in javascript