News for hackers. Stuff that matters.
Re:JavaScript Growing Pains: From 0 to 13,000 Dependencies by jonny383 on Sunday June 14, 2020 @10:31AM (#23517878)
One can see why this happens when packes like [1] and [2] not only exist, but are frequently used.

[1] https://www.npmjs.com/package/is-even

[2] https://www.npmjs.com/package/is-odd

EDIT: my favourite part, is-even depends on is-odd.

https://github.com/jonschlinkert/is-even/blob/master/index.j...

Re:JavaScript Growing Pains: From 0 to 13,000 Dependencies by onion2k on Sunday June 14, 2020 @10:55AM (#23518080)
Anyone who needs to test an input is an odd number using vanilla JS will eventually end up rolling something that looks a lot like isOdd() themselves (or they'll just ignore a set of potential problems, which definitely is worse). It's very obvious when you read the code - https://github.com/jonschlinkert/is-odd/blob/master/index.js.

Utilities like that are a function of using a weakly typed language. "Just use TypeScript" is an alternative solution, but even in TS you should still check the number is within the bounds of isSafeInteger.

Re:JavaScript Growing Pains: From 0 to 13,000 Dependencies by Carpetsmoker on Sunday June 14, 2020 @11:48PM (#23523606)
Are those checks actually needed?

"2.2 % 2 === 1" works just as well, so why is the isInteger check needed? Why is the isSafeInteger check needed? And "a % 2" is already NaN (and thus false), although an explicit error is arguably better than silently eating it.

Re:JavaScript Growing Pains: From 0 to 13,000 Dependencies by rozab on Sunday June 14, 2020 @11:37AM (#23518453)
But why should isOdd, isEven and isNumber reside in seperate packages? Surely a single math tools package should cover all of this?
Re:JavaScript Growing Pains: From 0 to 13,000 Dependencies by onion2k on Sunday June 14, 2020 @04:00PM (#23520828)
Surely a single math tools package should cover all of this?

They shouldn't. It's silly. You're right that there should be "math"[1], but while those things aren't part of the main JS ecosystem people write utility libraries and publish them.

[1] There is, it's https://mathjs.org/. More people should use it.

Re:JavaScript Growing Pains: From 0 to 13,000 Dependencies by wongarsu on Sunday June 14, 2020 @12:07PM (#23518777)
It's an overreaction to the critizism of bloated libraries like JQuery. Dead code removal with tree shaking is a relatively recent feature in JS toolchains, so instead you have single-function libraries. For example there are also npm packages with each lodash function as its own package [1]

1: https://www.npmjs.com/search?q=keywords:lodash-modularized

Re:JavaScript Growing Pains: From 0 to 13,000 Dependencies by z3t4 on Sunday June 14, 2020 @01:12PM (#23519391)
Think of it like being able to pull any function from a global scope. eg. var/const foo = require("bar") Its one gigantic standard library.
Re:JavaScript Growing Pains: From 0 to 13,000 Dependencies by nicoburns on Sunday June 14, 2020 @10:38AM (#23517944)
I feel like somebody ought to start a project which systematically goes through the ecosystem removing these dependencies.

For more complex micro-libraries, perhaps they could be collated into a single, larger high quality library.

Re:JavaScript Growing Pains: From 0 to 13,000 Dependencies by wereHamster on Sunday June 14, 2020 @11:08AM (#23518206)
Very often these stupid dependencies are at the leaves of the dependency tree, often in packages which no longer get new releases to NPM. Fixing those packages has zero net effect on the whole situation.
Re:JavaScript Growing Pains: From 0 to 13,000 Dependencies by jart on Sunday June 14, 2020 @11:41AM (#23518482)
Re:JavaScript Growing Pains: From 0 to 13,000 Dependencies by slykar on Sunday June 14, 2020 @11:02AM (#23518145)
It gets better when you read the repo description.

> I created this in 2014, when I was learning how to program.

Someone probably just did this for the fun of learning and somehow it got adopted elsewhere.

Re:JavaScript Growing Pains: From 0 to 13,000 Dependencies by caseymarquis on Sunday June 14, 2020 @10:44AM (#23517992)
I immediately thought: "But no one would actually add that dependency!" Only to see 120k weekly downloads of is-even.

I... don't understand...

Re:JavaScript Growing Pains: From 0 to 13,000 Dependencies by onion2k on Sunday June 14, 2020 @11:04AM (#23518171)
It's not just checking if the number is odd. It checks if the input is a number (modulus on a non-Number value returns a NaN in JS), if it's an integer (not strictly necessary I guess), and if it's within the bounds of JS's safe integer range (again, you'll get a NaN if the number is too big). If you want to make sure your code is safe those are sensible things to check.

A lot of JS devs wouldn't bother check those things, so really a lot of code is improved by this library. It's kind of a shame that so many developers just skip over checking inputs really. That's the real problem.

Re:JavaScript Growing Pains: From 0 to 13,000 Dependencies by berkes on Sunday June 14, 2020 @11:45AM (#23518518)
The obvious solution would be to have some stdlib or even a common-utils project or something like it, which contain all such items. In one package.

Because, indeed, no-one should be rewriting obvious code like isArray, leftPad, isEven and so on.

Yes, that might cause some overhead in some situations (when you need isOdd, but not leftPad). But I'm certain that overhead is minimal. In practice, however, we currently see dependency-trees where these "should-be-stdlib" modules like "leftPad" or "isArray" are repeated numerous times in one project.

Any stdlib could even be optimized by browsers and engines, turning that "overhead" into a net benefit.

Re:JavaScript Growing Pains: From 0 to 13,000 Dependencies by eeereerews on Sunday June 14, 2020 @01:47PM (#23519660)
It doesn't.

  isOdd('') => false
  isOdd(null) => false
  isOdd(2n) => TypeError: Cannot convert a BigInt value to a number
But even if it did, the same line of reasoning leads to the absurd conclusion we should have an addOne function.
Re:JavaScript Growing Pains: From 0 to 13,000 Dependencies by enriquto on Sunday June 14, 2020 @10:51AM (#23518041)
> I... don't understand...

You are not supposed to reinvent the wheel. Somebody has already gone through the trouble of implementing the is-even algorithm, and it would be a waste of time to re-write it for no purpose. You may think that it is a simple algorithm, but I doubt you could implement it better than the people who uploaded these files to npm. Notice that the algorithm has many obscure corner-cases that you are likely to miss on your first implementation. Fortunately, it is already written and packaged; just use it.

Re:JavaScript Growing Pains: From 0 to 13,000 Dependencies by robert-boehnke on Sunday June 14, 2020 @11:01AM (#23518143)
Note that this implementation of `isEven` throws on input that isn't a number or numbers that are not integers, NaN or larger than 2^53-1. I think it's fair to argue an implementation could just as well return false for 1.4, NaN or '2'. There is a cost to learning these minutiae of your dependencies, especially if there is no compiler support to assist you.
Re:JavaScript Growing Pains: From 0 to 13,000 Dependencies by _0ffh on Sunday June 14, 2020 @11:15AM (#23518259)
I've not programmed a lot of JS, so this might be obvious for someone who did, but:

Why does he first use Math.abs on the parameter and then type check the result of that? I'd think if you do an argument type check, you'd do it before using it. Just to make it not throw on null? I don't see the sense in that...

Re:JavaScript Growing Pains: From 0 to 13,000 Dependencies by rodw on Sunday June 14, 2020 @12:01PM (#23518729)
I'm not sure this is the reason for that specific implementation but `Math.abs` _will_ do a string-to-number conversion.

I.e., `Math.abs("-27")` yields `27`.

It might be the case that Math.abs does other *-to-number conversions also, so maybe this method is trying to take advantage of that. (Math.abs is usually implemented as a native function so without digging more deeply it's not obvious to me what the actual implementation does.)

UPDATE: Curiously, `Math.abs(null)` yields `0`, so a null argument passed to is-even would yield `true` I guess.

Also whatever the logic is for that conversion is it is not the same as the built-in `parseInt` or `parseFloat`. `parseInt("27 meters")` yields `27` but `Math.abs("27 meters")` yields `NaN`.

Personally in JavaScript I almost universally make use of home-grown `isInt` and `toInt` methods whose behavior is predicable for me -- eg. my toInt() will reject a string that's not _just_ an integer value (and return `null` rather than `NaN` in all cases because who wants to check `isNaN`?) -- but that's probably not a great practice either. But I've been programming long enough to realize that the principle of least surprise is probably the most important concern here for long-term efficiency. I suppose what is or is not surprising may be subjective though.

Re:JavaScript Growing Pains: From 0 to 13,000 Dependencies by caseymarquis on Sunday June 14, 2020 @11:05AM (#23518173)
</sarcasm>

I think you're getting some down votes as you missed a closing tag.

Re:JavaScript Growing Pains: From 0 to 13,000 Dependencies by enriquto on Sunday June 14, 2020 @11:40AM (#23518474)
But if you indicate sarcasm as such, it ceases to be so! The best sarcasm is one that leaves exactly half of the people believing it to be sarcasm, and the other half agreeing with it. Otherwise, it is just a farcical exaggeration (which is also funny, but for different reasons).
Re:JavaScript Growing Pains: From 0 to 13,000 Dependencies by _0ffh on Sunday June 14, 2020 @11:17AM (#23518278)
Man I hope they did (just forget the closing tag). Poe's law in action.
Re:JavaScript Growing Pains: From 0 to 13,000 Dependencies by rodw on Sunday June 14, 2020 @11:17AM (#23518273)
For the record, 120k weekly downloads isn't really _that_ large in this ecosystem.

That's 120k instances of people or scripts doing `npm install is-even` or the equivalent.

For comparison:

* Lodash - a general purpose set of library/utility functions by the way - has ~26M weekly downloads.

* request - an HTTP client library - has ~19M weekly downloads

I wouldn't add is-even as a direct dependency either, but I think people are underestimating the size and diversity of the JavaScript ecosystem as a whole.

Re:JavaScript Growing Pains: From 0 to 13,000 Dependencies by umvi on Sunday June 14, 2020 @10:57AM (#23518101)
> EDIT: my favourite part, is-even depends on is-odd.

Probably because it just does

    return !is-odd(x);
So it can take advantage of all the type checking and other corner case junk in `is-odd`
Re:JavaScript Growing Pains: From 0 to 13,000 Dependencies by Kye on Sunday June 14, 2020 @11:11AM (#23518225)
It does seem like JavaScript/NPM has the advantages of composability found in Unix tools. It's no different from zcat | grep or similar. All that's different is the people making these tools have a wider range of experience and knowledge than people hacking on C making Unix tools, and easier distribution.
Re:JavaScript Growing Pains: From 0 to 13,000 Dependencies by 15rthughes on Sunday June 14, 2020 @04:09PM (#23520880)
There’s an argument to be made that micro packages follow the ideas of the Unix philosophy, but to compare is-odd and a derivative that simply negates it’s results to piping two well optimized coreutils together is definitely a stretch.

I’d also say that implying JS developers inherently have a wider range of experience and knowledge compared to developers of coreutils is flat out absurd.

Re:JavaScript Growing Pains: From 0 to 13,000 Dependencies by Kye on Sunday June 14, 2020 @04:53PM (#23521273)
>> "I’d also say that implying JS developers inherently have a wider range of experience and knowledge compared to developers of coreutils is flat out absurd."

I was way too ambiguous, so I see how you understood it that way. I meant in the downward direction. There are more packages from more people in JS, so you end up with lots of first projects, small experiments, etc. So you have 100% of coreutils being decent to amazing, while it's probably closer to 1% with NPM. Both ways have their advantages, but it's easier to footgun with the highly inclusive NPM approach.

Re:JavaScript Growing Pains: From 0 to 13,000 Dependencies by magicalist on Sunday June 14, 2020 @10:41AM (#23517974)
> but are frequently used.

From the short dependents lists, it looks like neither is frequently used directly. Edits to one or two dependent projects would likely be enough to largely eliminate them from the ecosystem.

Re:JavaScript Growing Pains: From 0 to 13,000 Dependencies by benjaminsuch on Sunday June 14, 2020 @10:35AM (#23517922)
I opened the index.js files of both repos and their copyright comment made me chuckle.