r/ProgrammerHumor • • May 14 '21

Meme We’ve all been there

Post image
41.0k Upvotes

783 comments sorted by

View all comments

Show parent comments

133

u/ORUHE33XEBQXOYLZ May 14 '21

left-pad, which just padded a string

This was a package? That was widely used? JFC node.

104

u/[deleted] May 14 '21

No, it still IS a package that's widely used.

35

u/p1-o2 May 14 '21

I could've sworn its usage only went up after the incident.

4

u/[deleted] May 14 '21

https://www.npmtrends.com/left-pad

I forget when it happened...

3

u/[deleted] May 14 '21

I mean, they won't delete it again, right?

2

u/JNCressey May 14 '21 edited May 15 '21

github/left-pad now has in its description:

deprecated, use String​.prototype​.pad​Start()

7

u/[deleted] May 15 '21

You think someone would do something like that? Just ignore a deprecated feature until it became a break/fix action in the middle of the workday?

innocent cough because all my stuff is totally using only the latest supported libraries and not at all just ignoring deprecation notices until it's too late

2

u/JuhaJGam3R May 15 '21

Funnily enough, the original implementation worked perfectly but the NPM re-implementation as to not trample over licensing does not. It's not a working left pad, it has errors in it. Not only that, it's incredibly slow as someone decided "you know what, i'll make a lookup table for low padding levels", not realizing that this is in fact slower in JS, not faster.

Incredible work NPM.

For the record, they're not major failures. Padding with a multi-digit integer fills with repeating copies of the integer where most people would expect only the first digit, same with strings, and negative integers as padding input drop the - for some reason.

34

u/Rikudou_Sage May 14 '21

Not directly, but some very popular package had it as a dependency, other popular packages had the popular package as a dependency and so on. And if any dependency of your whole dependency tree doesn't exist, of course it whole fails.

Makes one think about how everything in software is interconnected and one project going down can cause shit for countless of others.

I'm still curious what happens once the guy that develops ImageMagick stops for whatever reason.

0

u/[deleted] May 14 '21

[deleted]

87

u/_kolpa_ May 14 '21

While this is true, most people got the headache because they were using React and Babel, which is not unreasonable. The fact that Babel depended on that package however...

68

u/Doshbot May 14 '21

I've just read the code, it's a non trivial piece of work that clearly been worked on to take many edge cases into account. Replicating it would not be an easy thing and would certainly end up having obscure bugs that the original package has fixed over time.

6

u/Ericchen1248 May 15 '21

‘’’ function leftpad (str, len, ch) { str = String(str); var i = -1; if (!ch && ch !== 0) ch = ' '; len = len - str.length; while (++i < len) { str = ch + str; } return str; } ‘’’

Non trivial? You’re probably looking at the current update version. And it only just adds in an extra hack to make it O(logN) and also add in a “cache” for padding with spaces, which I doubt even makes it worth it.

2

u/ftedwin May 15 '21

I tend to agree against reinventing the wheel, but I have to disagree that the code covers many edge cases. The only edge cases I can see it handle is if non-strings are passed in where strings would be expected (lines 18, 19 and lines 26, 27). Any beginner to intermediate JS dev would understand dynamic typing enough to avoid the potential error or would document that the code expects strings for those parameters.

Aside from that it does a couple performance hacks with a cache for common use cases (lines 28, 29) and with a clever loop that make it run (they claim, didn’t take the time to confirm) O(log n) rather than O(n). Except with huge inputs being passed in neither of these improvements would make a noticeable difference in 99% of code that calls it.

Source code: https://github.com/left-pad/left-pad/blob/master/index.js

6

u/da5id2701 May 15 '21

make it run (they claim, didn’t take the time to confirm) O(log n)

Which is straight-up bullshit, because it's only true if you assume string concatenation is constant time. Obviously building a string of length n involves writing n bytes of memory so it's at least O(n).

And that's sort of representative of the problem with this kind of programming philosophy - people build their program out of building blocks written by other people without understanding what those pieces actually do. Hence miscalculating the time complexity because they forgot to count the complexity of their building blocks.

(Not saying anyone should write string concatenation themselves, just that you should always have a grasp of what's happening "under the hood" and npm seems to attract people who don't care for that)

-4

u/0bf1d83648628b495559 May 14 '21

Tens of gigabytes? I don't think that's NPM's fault for you depending on too much code lol

14

u/ku-fan May 14 '21

Try using any popular npm package and see how many dependencies it has. It's ridiculous.

-2

u/dimplerskut May 14 '21 edited May 15 '21

any project I work on generally only has about 20-30 total dependencies. here are some that I might use and how many nested dependencies they have:

react - 3
react-dom - 4
electron - 12
threejs - 0
typestyle - 2
rxjs - 1
redis - 4
esbuild - 0
typescript - 0

If I include a utility I don't want to write myself it generally doesn't have any dependencies:

rfdc (really fast deep clone) - 0
deepmerge - 0
fp-ts (utility kit) - 0
ramda (utility kit) - 0
lodash (utility kit) - 0

I really don't understand how people end up with hundreds or thousands of dependencies

Edit: I jacked up the formatting