News for hackers. Stuff that matters.
Re:NPM Vulnerability Discussion on Twitter by _fat_santa on Tuesday May 10, 2022 @09:04AM (#31326602)
One of the problems I see are the super popular holdover packages before ES6 was standardized on. The tread mentions the `foreach` package. And remembering back, many previous supply chain attacks were on these small packages like: `iseven`, `isodd`, etc.

I don't fully understand why packages like this are so popular. My guess is it has to be a combo of legacy projects pulling those deps in and inexperienced developers not knowing that these features are now part of standard JS.

While this would cause controversy, I think that NPM should lock all these dependencies down and not allow any modifications. The modules would basically be passthroughs to native ES6 functionality. I'm not saying NPM should lock down all legacy packages, just the ones that implemented ES6 functionality (isBetween, isEven, isOdd, forEach, etc)

Re:NPM Vulnerability Discussion on Twitter by leodriesch on Tuesday May 10, 2022 @09:35AM (#31326947)
I don’t get why anyone would introduce a dependency this small to their codebase.

It’s a much better choice to just copy the source code and possibly license into your own code to just eliminate the overhead.

It’s not like any of these packages depend on being updated for security reasons or anything.

Re:NPM Vulnerability Discussion on Twitter by chrisfosterelli on Tuesday May 10, 2022 @12:17PM (#31328884)
At risk of criticism, I'll bite. I used to think this way and include a lot of small dependencies in most projects I worked on.

The thinking was as follows: Of course you could just copy the code, but then that increases LOC in my codebase that I'm responsible for. More code is more work. Lines in a dependency are the responsibility of someone else. If there's a bug, even in a small function, the community can identify it and fix it. I can get new features I might not have known I needed. I can benefit from all of these fixes indefinitely into the future without ever having to have any mental overhead about that code. So can everyone else; it's good to maximize code reuse.

I don't think I've ever used something that could be an obvious one-liner like `isOdd` but for lots of only slightly more complex stuff like left-pad, email format validation, GPS coordinate math functions -- all stuff that's really less than 30 lines -- it was really nice to just not have to think about the implementation details of that and get back to solving your problem. I could have reviewed the code or written it myself but it's just more work when remaining at a high level `leftPad()` call let's me stay focused on my original task.

That said, I've since realized I was wrong of course. Trying to maintain projects that haven't been touched in more than a year led to hours of fixing dependency issues. We switched to using dependabot, which is better, but just makes it obvious how much work it actually is to keep dependencies up to date week-to-week. Then there's all of the security issues. These days, for small packages, I advocate for reviewing the code from these packages, ensuring we understand it, and then copying it in directly with a comment for attribution. We generally try to keep dependencies low; still more than in other languages but at least some thoughtfulness about whether it's "worth it". I think a lot of the community has shifted similarly, but there's still a lot of older projects with older dependencies.

Re:NPM Vulnerability Discussion on Twitter by pilif on Tuesday May 10, 2022 @12:45PM (#31329257)
> Of course you could just copy the code, but then that increases LOC in my codebase that I'm responsible for.

Once your company is owned by a supply chain attack or by an RCE in one of your dependencies, you will learn that you are in-fact very much responsible for the code in your external dependencies.

Re:NPM Vulnerability Discussion on Twitter by tylersmith on Tuesday May 10, 2022 @03:30PM (#31331522)
This drives me crazy. If your software is using a library that library is part of your software whether you originated its code or not. If you are responsible for the software then you're responsible for the LOC in every library your software depends on.
Re:NPM Vulnerability Discussion on Twitter by bin_bash on Tuesday May 10, 2022 @09:54AM (#31327128)
I think a lot of it is from a single engineer: sindresorhus

He’s a prolific open source author that traditionally had a lot of modularization in his packages—though I think he has started to move away from it recently. He talks about it here: https://blog.sindresorhus.com/small-focused-modules-9238d977...

Most projects will include at least one package by him in the dependency tree.

Re:NPM Vulnerability Discussion on Twitter by sanitycheck on Tuesday May 10, 2022 @11:06AM (#31327999)
Funnily enough, I was just digging through my current project's dependencies and he's slipped in via a Rollup plugin which uses his "globby" package, which in turn uses his "slash" package.. Slash is about 5 lines of code, which is roughly 4 more than most people need for what it does.

I vastly prefer to use libraries with 0 dependencies but I never quite manage it, so I end up with the same problems as everyone else.

Re:NPM Vulnerability Discussion on Twitter by nobleach on Tuesday May 10, 2022 @12:15PM (#31328863)
For years, the bane of my existence was always lodash. Import one tiny utility and it brings along 15 of its closest friends. You can't get away from it. It was built so incredibly modularly, that it has to import a bunch of other things. Now most folks will quickly rush to its defense and tell you that Webpack should be able to tree-shake it well enough. My actual experience is that it was a tremendous waste of my customers' bandwidth. Seeing LoDash as a dependency of anything I use is an immediate NOPE for me. I've yet to see anything it provides that I couldn't just do myself.

It's an unpopular opinion for some silly reason. It's like when Guava or Apache Commons was included on every single Java project in the 2010s. "It's just so much easier to use a well-tested library". That line of thinking is what got us here.

Re:NPM Vulnerability Discussion on Twitter by sanitycheck on Tuesday May 10, 2022 @01:01PM (#31329457)
I'm the opposite, I tend to be relieved when I see lodash as a dependency. In any normal sized project I'm probably already using it, or it's already a dependency of a dependency. I trust it a lot more than a hundred tiny libs by random authors, both in terms of sketchy behaviour and reliability/performance. If I need to target platforms which don't support modern JS I vastly prefer to stick to lodash than use a bunch of polyfills of unknown quality.

The individual functions are installable separately from NPM, and lodash-es should tree-shake quite well, but I do know what you mean about it dragging in its internal dependencies so you end up with a 15kb of lodash code for a single thing. I probably wouldn't love to use it client-side on an ordinary website, were I to make one.

Re:NPM Vulnerability Discussion on Twitter by thatwasunusual on Tuesday May 10, 2022 @09:58AM (#31327176)
> I don’t get why anyone would introduce a dependency this small to their codebase.

This might be controversial, but I think it has to do with less experienced developers, beginners, who doesn't know how to find out via "pure code" if a number is even or not, or even if something is a number (IIRC, there is an isNumber package out there as well).

I see this in other languages as well, but not in a "JavaScript scale", but that could probably just mean that the language's availability and popularity decides the number of "stupid packages."

Re:NPM Vulnerability Discussion on Twitter by BigJono on Tuesday May 10, 2022 @10:15AM (#31327395)
Let's call a spade a spade. It's a bunch of noobs that have become the majority and created a culture of dependency-first development.

I haven't worked with anyone for years that doesn't start troubleshooting an issue by reaching for a random dependency. They just don't even bother learning Javascript or the Web APIs or CSS anymore. Without a solid base of fundamentals every trivial problem seems insurmountable so they just don't even try.

Re:NPM Vulnerability Discussion on Twitter by ratww on Tuesday May 10, 2022 @10:39AM (#31327673)
If we're going to call a spade a spade, then let's go ahead and say that the majority of the packages that are always referenced in those discussions (is-even, is-odd, etc) is maintained and primarily used by a minority of developers that are very well known in the ecosystem.

This minority makes money out of their popularity in the NPM ecosystem. Having 1000 NPM packages is better for your reputation than having 100. And having all 1000 packages with lots of weekly downloads is better than having just a portion of that.

But how do they achieve that? Well, they have 1000 NPM packages, so each one depends on 5 to 10, that then depends on a few handful more. You have packages for checking if an HTTP status is a certain number, you have packages that have colors as constants, you have is-even, is-odd and so on. All that exists to maintain that closed ecosystem.

So out of the 1000 they basically have 20 useful tools and 980 garbage packages that exist only to maintain their own ecosystem.

Most people isn't using is-even or is-odd directly. They imported some other packages that are quite useful, but often need 10-20 sub-dependencies. Another interesting thing is that those shitty packages aren't really that important in applications. They're often used in build tools, CI and testing, tools for making CLI tools, and the sort.

The crazy thing is that a lot of people using is-even/is-odd aren't really "noobs": they're probably experienced developers that said "fuck it, I'll use some random tool from the web" when facing some random a problem.

Re:NPM Vulnerability Discussion on Twitter by Tainnor on Tuesday May 10, 2022 @11:02AM (#31327944)
That's a more charitable (and more reassuring) interpretation than "developers don't know the modulo operator".

That said, it still leaves a sour taste as this effectively implies that a certain set of JS developers is very happy to abuse their (maybe initially rightfully earned) prestige to gain even more prestige while leaving behind a mess for the whole ecosystem. I don't understand why this is tolerated. The Node community needs to have a serious discussion about why certain packages are allowed to spread garbage, create forks of the relevant packages that rip out "is-even" etc. and then eventually converge to these forks. But to this day, I don't see the community taking this problem seriously enough.

Now, supply chain attacks and "too many dependencies" are a potential issue for every language with dependency management (see also log4j, etc.), but no other ecosystem seems to be have such a high frequency of issues and (widely used) "is-even" packages are simply not a thing in any other mainstream language (some languages, like Swift, include similar functionality in the standard library, which is totally fair).

Re:NPM Vulnerability Discussion on Twitter by ratww on Tuesday May 10, 2022 @02:28PM (#31330657)
The reason it is tolerated is because the philosophy of "thousands of small packages" has spread far and wide.

For every person calling it out like we're doing here, there are ten others praising maintainers able to whip ten semi-useless packages per week.

It's not just random maintainers making small packages. The core infrastructure of Javascript is in it. Babel is made of hundreds of packages, which all live on the same repository (because of course the maintainers don't want the hassle of maintaining multiple things). Some of those packages don't even have anything of importance in it, just metadata, a couple flags and some boilerplate [1]. The package is just a way of organizing code. Webpack, ESLint and others aren't exactly better.

EDIT: And of course I got downvoted :)

[1] https://github.com/babel/babel/blob/main/packages/babel-plug...

Re:NPM Vulnerability Discussion on Twitter by cxr on Tuesday May 10, 2022 @03:17PM (#31331358)
You're tacitly giving the people you despise leverage when you say things like:

> The core infrastructure of Javascript is in it. Babel[...]

Babel is not core JS infrastructure. It may be close to fundamental to the modern NodeJS development experience, but JS exists happily (and capably) without any of that stuff (including package.json, for that matter).

Re:NPM Vulnerability Discussion on Twitter by ratww on Tuesday May 10, 2022 @04:40PM (#31332394)
Fair enough! True, Babel is only "core" for a subset of JS developers, not for the language.

I don't really despise anyone in Babel, though, I'm only criticising their packaging method. Babel isn't doing the million-packages thing to gain popularity.

Re:NPM Vulnerability Discussion on Twitter by bostik on Tuesday May 10, 2022 @12:41PM (#31329194)
> Having 1000 NPM packages is better for your reputation than having 100. And having all 1000 packages with lots of weekly downloads is better than having just a portion of that.

Should we treat them as spammers and polluters, then? Because if what you describe is true, that deserves to be called out and mowed to the ground.

Re:NPM Vulnerability Discussion on Twitter by ratww on Tuesday May 10, 2022 @02:10PM (#31330391)
Definitely.

It is more akin to SEO spamming than black market spamming, though. They're polluting NPM in the same way SEO farms spam Google. It makes life difficult for everyone, but it's still a gray area in terms of legitimacy. Which is why nobody really talks about it.

Re:NPM Vulnerability Discussion on Twitter by sdoering on Tuesday May 10, 2022 @01:22PM (#31329711)
I don't know if this is true, but if it were true you'd have a veritable "tragedy of the commons" [0] situation were privatization of (some) gains leads to the creation of negative externalities for the rest.

If incentives were aligned differently, different results might have resulted. Probably with different externalities (or unintended consequences).

[0]: https://en.wikipedia.org/wiki/Tragedy_of_the_commons?wprov=s...

Re:NPM Vulnerability Discussion on Twitter by thatwasunusual on Tuesday May 10, 2022 @12:02PM (#31328704)
> Most people isn't using is-even or is-odd directly. They imported some other packages that are quite useful, but often need 10-20 sub-dependencies.

IMO, that's even worse, because that means that a lot of people are using stupid and vulnerable packages without knowing it.

I wish there was some sort of better control over the NPM directory, where someone could block/downvote (or whatever) packages that doesn't deserve to live. How this would - or should - work in practice, I have no idea, but it's just getting scarier by the day to import a package in your application.

Re:NPM Vulnerability Discussion on Twitter by ratww on Tuesday May 10, 2022 @02:33PM (#31330728)
The problem is that those fame-chaser maintainers aren't the only ones doing it. Babel does it. Webpack does. ESLint does it. Before them others did.

If we ban those, we'd have to ban Babel and Webpack too... Oh now, wait a minute, now that actually sounds interesting...

Re:NPM Vulnerability Discussion on Twitter by thatwasunusual on Wednesday May 11, 2022 @10:00AM (#31340047)
Wow. You must be a special kind of stupid?

What if we focused on fixing all the problems, and not just retreating thinking that "we can't solve this problem, because there are so many other problems related to it"?

Your thinking is literally the definition of the problem.

Re:NPM Vulnerability Discussion on Twitter by cobertos on Tuesday May 10, 2022 @12:33PM (#31329087)
A few developers will register multiple packages that are essentially the same package with small tweaks. They choose the names in a way to get the right keywords to hit from npmjs's search
Re:NPM Vulnerability Discussion on Twitter by ZetaZero on Tuesday May 10, 2022 @10:23AM (#31327489)
If a developer doesn't know how to write their own "iseven", I don't want their product.

Maybe you could argue other for other reasons to use these packages.

Re:NPM Vulnerability Discussion on Twitter by haswell on Tuesday May 10, 2022 @11:58AM (#31328647)
I suspect a lot of this use doesn’t go into products, it goes into projects.
Re:NPM Vulnerability Discussion on Twitter by zzyzxd on Tuesday May 10, 2022 @01:00PM (#31329450)
Comparing to:

1. search for a package that does X

2. scan the search result and find the package that really does X

3. learn to use the package's API

4. import the package in the code

5. use it

Isn't it much easier to just copy-paste code from stackoverflow? There's also a good chance that the you can get some very good explanation and interesting discussion around the implementation there.

Re:NPM Vulnerability Discussion on Twitter by ratww on Tuesday May 10, 2022 @04:55PM (#31332593)
Well, yes.

There's also Github Copilot, which pretty much replaced almost my entire usage of Stack Overflow.

But the thing is that with a rando package is that one doesn't have to review the code. Sure, the code is also coming from somewhere else. Sure, it might never get updated. Sure it might be full of bugs. Sure, it might be more dangerous than copying from StackOverflow. But out of sight, out of mind.

In the end the overuse of packages isn't about saving time or "doing the best for business" or "ensuring that the code is maintained by someone else". It's purely about covering our asses.

Re:NPM Vulnerability Discussion on Twitter by xboxnolifes on Wednesday May 11, 2022 @12:04PM (#31341892)
I feel it's more likely related to how often "don't reinvent the wheel" and "NIH syndrome" is thrown around. It's no surprise to me that people will first check to see if a library already exists before going to write their own, probably lacking function.
Re:NPM Vulnerability Discussion on Twitter by duxup on Tuesday May 10, 2022 @10:09AM (#31327314)
I find that I do that more often now than ever.

Most times I get the urge to pull in a rando package I find I really only need a few things it does. I check it out. Read it. Write my own.

I almost never need "all the things" outside the situations where I am using a big framework. So reading it, getting inspired / ideas from someone who did the thing and then I write a much more narrow focused version for myself.

Re:NPM Vulnerability Discussion on Twitter by megous on Tuesday May 10, 2022 @04:45PM (#31332468)
And you can easilly add the features you actually need. :)
Re:NPM Vulnerability Discussion on Twitter by Merad on Tuesday May 10, 2022 @12:16PM (#31328876)
4-5 years ago (when the popularity of node was skyrocketing but es6 was still relatively new) I remember some people arguing passionately that it was good to maximize code reuse, i.e. better to pull in a package with one utility than to write 5 lines of code in your own application. I think it was mainly a symptom of the gaping holes in javascript's standard library... thankfully that attitude seems to have mostly died off.
Re:NPM Vulnerability Discussion on Twitter by remram on Tuesday May 10, 2022 @02:49PM (#31330967)
> possibly license into your own code

This can be complicated. Depending on another package is usually very safe, at least as safe as "dynamic linking", but including code in your own source tree needs licenses to be compatible. Even then, you might have to change your license to "BSD-3-Clause + ISC" or similar composite and you will get complaints from users.

Re:NPM Vulnerability Discussion on Twitter by Vinnl on Tuesday May 10, 2022 @09:12AM (#31326692)
Generally, I don't think they're particularly popular, but a package that depends on them is popular. Or in fact, a package that depends on that package (that depends...) is.
Re:NPM Vulnerability Discussion on Twitter by mhitza on Tuesday May 10, 2022 @09:26AM (#31326844)
At least at some point I've randomly stumbled over a GitHub account that had published many NPM packages with one-two function source code. They marketed themselves as a big contributor to the NPM ecosystem, which was directly based upon the number of NPM packages published and the number of monthly downloads. Some packages seemed to take off because people just do a npm install instead of replicating a one line function, while others I think were more popular nodejs projects of this user where they tried to force as many of their own dependencies as they could.

TL;DR "SEO" spam to market ones own nodejs expertise, would not be out of the question

Re:NPM Vulnerability Discussion on Twitter by krembanan on Tuesday May 10, 2022 @09:30AM (#31326892)
ES6 really has nothing to do with isEven and isOdd
Re:NPM Vulnerability Discussion on Twitter by jamal-kumar on Tuesday May 10, 2022 @12:16PM (#31328879)
truly... those are just modulo operations
Re:NPM Vulnerability Discussion on Twitter by skrebbel on Tuesday May 10, 2022 @09:06AM (#31326632)
Hahahhahh isBetween, why

Compared to that, left-pad is rocket science

Re:NPM Vulnerability Discussion on Twitter by daurnimator on Tuesday May 10, 2022 @09:26AM (#31326843)
https://github.com/yefremov/isnan

An entire module for just `return value !== value;`

Re:NPM Vulnerability Discussion on Twitter by __s on Tuesday May 10, 2022 @09:36AM (#31326950)
Re:NPM Vulnerability Discussion on Twitter by jefftk on Tuesday May 10, 2022 @10:01AM (#31327214)
Which wasn't everywhere in 2017, when isnan was created: IE11 didn't (and still doesn't, though it's less relevant) have isNaN()

(Not supporting a culture of taking a dependency for this sort of thing, though)

Re:NPM Vulnerability Discussion on Twitter by jaywalk on Tuesday May 10, 2022 @10:22AM (#31327482)
Just to be clear, because your comment made no sense to me: what IE11 doesn't support is Number.isNaN(). It definitely does support (and has since IE3) plain isNaN(): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
Re:NPM Vulnerability Discussion on Twitter by jefftk on Tuesday May 10, 2022 @11:03AM (#31327967)
Regular isNaN is very tricky, and almost never what you want: it first coerces its input to a Number, and then checks if that is NaN:

                isNaN   Number.isNaN
                -----   ------------
    {}           true          false
    [true]       true          false
    undefined    true          false
    "{}"         true          false
Re:NPM Vulnerability Discussion on Twitter by jaywalk on Tuesday May 10, 2022 @11:50AM (#31328546)
But none of those things you listed are numbers. The isNaN output is exactly what I want in pretty much every case.
Re:NPM Vulnerability Discussion on Twitter by jefftk on Tuesday May 10, 2022 @11:55AM (#31328620)
isNaN does not tell you whether something is not a Number either. Applying isNaN to any of these returns false: null, true, false, [], "", "cabbage".
Re:NPM Vulnerability Discussion on Twitter by _Algernon_ on Tuesday May 10, 2022 @10:34AM (#31327608)
I'm gonna create a package for isX() for each x in the set of integers. Please use my packages for all comparisons from now on.
Re:NPM Vulnerability Discussion on Twitter by Izkata on Tuesday May 10, 2022 @01:03PM (#31329494)
is-thirteen is six years old: https://www.npmjs.com/package/is-thirteen
Re:NPM Vulnerability Discussion on Twitter by _fat_santa on Tuesday May 10, 2022 @09:08AM (#31326653)
Nevermind on that one. I just checked NPM and thankfully that package does not exist.
Re:NPM Vulnerability Discussion on Twitter by sdevonoes on Tuesday May 10, 2022 @11:32AM (#31328341)
> I don't fully understand why packages like this are so popular.

It actually works like this: Author X develops `iseven`, `isodd`, etc. No one really downloads such packages. Author X then develops `importantPackage` which does do something useful developers out here download. The thing is `importantPackage` relies on `iseven` and `isodd`. Now `iseven`, `isodd` are downloaded alongside `importantPackage`. Profit.

My point is, we should recognize certain NPM authors as toxic, but I guess "freedom of speech/code" stops us from doing so. Example of such an author: https://github.com/jonschlinkert/

Re:NPM Vulnerability Discussion on Twitter by broken8ball on Tuesday May 10, 2022 @06:29PM (#31333501)
This guy would lose his shit if npm locked down his packages. I’m probably just venting but he’s been nothing but rude to me any time I’ve opened a legitimate issue on any of the larger downstream libraries of his. Not to mention, his Twitter is something else.
Re:NPM Vulnerability Discussion on Twitter by aa-jv on Tuesday May 10, 2022 @09:35AM (#31326946)
>I don't fully understand why packages like this are so popular.

I consider 'iseven' and 'isodd' to be signs that Javascript is a hellaciously engineered piece of crap that should be avoided at all costs. They're popular because Javascript is garbage.

Re:NPM Vulnerability Discussion on Twitter by haswell on Tuesday May 10, 2022 @12:07PM (#31328765)
JavaScript supports the modulo operator and the native syntax for this:

  let isEven = theNumber % 2 == 0;
There are reasons to dislike JavaScript, but this isn’t it. The native solution is about as standard as it gets.
Re:NPM Vulnerability Discussion on Twitter by aa-jv on Wednesday May 11, 2022 @05:15AM (#31337548)
Please explain to me why there is a package "iseven" and "isodd" if not for the fact that some Javascript kiddy was unable to work this out for themselves by applying the intrinsics of the language, standardly ..

Because I firmly believe that crap like this happens because the language itself is very, very dumb.

Re:NPM Vulnerability Discussion on Twitter by russtrotter on Tuesday May 10, 2022 @02:35PM (#31330772)
The old C bum in me says this should really be a bitwise AND with 1 but I'm sure modern compilers and JS JIT/V8 runtimes optimize MOD 2 to the same.
Re:NPM Vulnerability Discussion on Twitter by repiret on Tuesday May 10, 2022 @02:59PM (#31331112)
I spend most of my time writing C for embedded systems. You should focus first on making life easy for the people reading your code. Use `% 2` and `* 4` when those are the things you mean, rather than `& 1` and `<< 2`. Compilers have been able to do those strength reductions on their own for years, and in most cases even if the compiler didn't, the cycles you'd save aren't worth it.
Re:NPM Vulnerability Discussion on Twitter by jbverschoor on Tuesday May 10, 2022 @12:16PM (#31328870)
You’re not expressing what you mean by %.. I’m all against these packages, but in Ruby we have a good standard library.
Re:NPM Vulnerability Discussion on Twitter by haswell on Tuesday May 10, 2022 @02:05PM (#31330303)
I’m a big fan of Ruby and yes, Ruby handles this elegantly.

However, some variation on n%2==0 is idiomatic in a large number of languages, both modern and old [0].

This includes Lua, Java, Python, Scala, Perl, PHP, Swift, and many others.

It is true that there are numerous languages with built in convenience methods in their standard libraries, but that does not by itself mean that n%2==0 is problematic and certainly this idiom is deeply entrenched.

There is nothing preventing a developer using those languages from wrapping this in a function, and indeed this is probably a good idea if it’s something you find yourself using repeatedly throughout your code base, just like any other often-repeated code snippets.

- [0] https://www.rosettacode.org/wiki/Even_or_odd

Re:NPM Vulnerability Discussion on Twitter by krembanan on Tuesday May 10, 2022 @01:45PM (#31330011)
It's... the modulo operator
Re:NPM Vulnerability Discussion on Twitter by jbverschoor on Tuesday May 10, 2022 @01:50PM (#31330093)
Yes. But you’re not expressing what you mean. You don’t want the remainder.

It’s like doing a for(i=0; i<items.length:i++) { e=items[i] } to iterate over each element instead of something like foreach, or a lambda.

Re:NPM Vulnerability Discussion on Twitter by haswell on Tuesday May 10, 2022 @02:23PM (#31330556)
Of all the possible examples to choose, I would not have guessed a classic for/each would be at issue.

How is that for loop not expressing what it means? for is used to iterate over something. There are legitimate reasons to choose for even when forEach is available.

Does this not express what I mean?

  if (amount > balance)
    return “insufficient_funds”;
Chances are, I’ll put this in a method anyway, and that’s what a language enables me to do - to create meaning that is contextual to my program.

Abstraction can help simplify complex problems, and that’s what it is good at. But when literally everything is abstracted away, it becomes harder to reason about what the program is doing without also learning about what the particular abstraction does (e.g. implicitly handling negative numbers, etc).

A well implemented standard library can be a joy to use, and some people choose languages for this reason. Some languages very intentionally provide a very lightweight standard library, and that’s just fine. All of this shouldn’t absolve the developer from understanding some of the very basic constructs of the language they choose - a lack of which will lead to bugs and vulnerabilities.

If I choose JavaScript (or it’s chosen for me), it behooves me to understand the basics vs. farming this out to some node package that just made my app harder to reason about and vulnerable to supply chain attacks for essentially no value.

Re:NPM Vulnerability Discussion on Twitter by ColinWright on Tuesday May 10, 2022 @02:53PM (#31331014)
I don't usually get drawn into this, but I have an answer to this because I've seen something similar in production.

jbverschoor said (edited for brevity):

> Yes. But you’re not expressing what you mean.

> It’s like doing a

  > for(i=0; i<items.length:i++)
  >   { e=items[i] }
> to iterate over each element instead of something like foreach ...

haswell replied:

> How is that for loop not expressing what it means?

It's true that the for loop is accomplishing the goal, but it is specifically running through the elements from 0 to length.

But sometimes what you want to do is, for example, apply a function to every element, and the fact that they are in a structure indexed from 0 to N-1 is not relevant. Your intent is to do a thing for every element, not to run down a list doing the thing.

There is a difference in intent, and using foreach instead of a for loop quite specifically expresses that.

Small examples like this are never convincing, but there really is a difference between "Run down this list" and "do this thing for every element". The output is the same, but the intent is different, and expressing that intent is important in larger projects.

Re:NPM Vulnerability Discussion on Twitter by haswell on Tuesday May 10, 2022 @03:18PM (#31331364)
That intent is implicit, and using for or forEach does not by itself provide enough information about intent. I agree that using one vs. the other can be a strong hint. The surrounding code provides the rest of the story.

It may be true that running from 0…length is not the most important part, but it may also be true that I need more flexibility and control while iterating, and may still choose to use for as a result.

But I think we’re getting a bit off track (and I helped get us here…).

jbverschoor‘s comment was a followup about the expressiveness of the modulo operator vs built-in convenience methods.

At least in the case of JavaScript, there isn’t an isEven available. If it was, an argument could be made that using it is more expressive. Since it doesn’t, the argument can be made that the most expressive option is the idiomatic one. Certain code fragments become idiomatic for a reason.

I interpreted the comments about for and the perceived lack of expressiveness in using it against this backdrop, and my point was just that for still has a time and a place. It may be that jbverschoor recognizes this, but I think the comparison doesn’t quite help when examining the use of modulo.

Re:NPM Vulnerability Discussion on Twitter by ColinWright on Tuesday May 10, 2022 @03:52PM (#31331810)
I suspect we are largely in agreement. The example is not necessarily a good one, but I think we agree that the intent expressed by "foreach" is different from the intent expressed by "for ()", and while that difference might not matter, and might not be large, and might be carried equally by the context, it is real.

Going back to the original, there is a difference between "isEven(n)" and "0==n%2". Sometimes (and this is definitely true in some code I write) there is a conceptual difference between asking if something is even, versus asking if it leaves a remainder of 0 upon division by 2. Yes, they can be proven to be equivalent, but the underlying intent is different.

BTW, I agree entirely that in these cases it's really mostly pointless, but the principle carries over to larger examples. Trying to see that there is a difference is probably worth it, even if we agree that it doesn't really matter in these trivial cases.

Oh, and for a compiler I used to use it really did matter that you used "foreach" when you could, versus using "for ()", because it could dispatch things in parallel in the former case, and couldn't in the latter. So expressing the intent in the code itself and not just in the context really can matter.

Re:NPM Vulnerability Discussion on Twitter by haswell on Tuesday May 10, 2022 @08:08PM (#31334248)
Yeah, I think we are on the same page.

The only nit I'll pick is re: the 2nd paragraph. I do agree that there is a difference in the expressions "isEven(n)" and "0==n%2" in isolation. But these expressions will always be surrounded by more code, and the degree of difference will entirely depend on that code and its structure. All of this can be very simply solved with a quick:

  function isEven(n) { return 0==n%2 } 
And let isEven = 0 == n % 2 provides about as much context as isEven(n).

Admittedly, I had to be conscious of how I wrote that expression, but choosing meaningful variable names is also just part of writing code, and isEven() by itself likely doesn't provide enough context about why I'm checking for even-ness to begin with. These considerations all need to go into the design of the class/method, but at no point can the developer wash their hands of the need to provide context through standard practices, just because the standard library provided more descriptive methods.

And I'm not saying this is what you're saying, but the original comment seemed to believe that more expansive standard libraries are somehow inherently better. I'd argue they're just different, and more factors to consider when choosing a language. Sometimes they help. Sometimes they're not worth the price of admission.

I think the discussion could be generalized as: how far should standard libraries go? No matter how good that standard library is, at some point, I must apply the fundamental skill of adding context and structure to the code I write.