r/programmingcirclejerk • Software Craftsman • Dec 30 '17

Retiring Python as a Teaching Language because Javascript

http://prog21.dadgum.com/203.html
39 Upvotes

46 comments sorted by

View all comments

Show parent comments

12

u/[deleted] Dec 30 '17 edited Dec 31 '17

I guess that kind of makes sense. I suppose I'm just used to languages where smartlinking exists (and more broadly linking of any kind, obviously), haha.

Although I'd still say people should try to avoid unnecessary imports/includes/usings/e.t.c in any language where possible. I'm never going to buy that the sort of entirely trivial nature yet insanely overestimated importance and status of something like "leftPad" is a perfectly reasonable phenomenon.

That said, if I were actually a JS programmer/someone who regularly had to import a single-function not-library for something as simple as what leftPad does, I'd definitely be asking more and more questions as time went on about why JavaScript doesn't have at least a nominal standardized collection of string handling methods.

Also more specifically/less seriously, in my experience padding the left side of a string with anything is not even something most developers would ever need to do that much at all (although it's normally no big deal... in Pascal at least I can just call either PadLeft or PadRight directly through the strings themselves by default) so it seems strange/funny that leftPad is even that regularly useful for JS devs?

Is there some specific reason you need to pad stuff especially often while making web-things?

12

u/Earhacker Code Artisan Dec 31 '17

You don't need to pad strings especially often on the web. No more often than you do in your Pascal apps.

But it's not that exciting a problem to solve. You might write it out once and get a little kick out of passing some tests in under 10 minutes, but then the fun's over.

Then months later, you're on another project and need to left pad a string. Do you just go back to your old project and copy-paste? No you don't, because you're a shit hot programmer who remembers OO principles like abstraction and encapsulation, so you wrap up your old left-pad function and its tests and put it in an Node package, so that you can share functionality across two unrelated projects. And that's totally valid, that's even a good use case for packages.

Then later some rock star Front-end ninja who can't code for shit has to Google "how to add spaces to the start of a string" and finds your package on NPM. He installs it, calls your function, and orders himself a latte on his Starbucks app to celebrate another card moved from 'Doing' to 'Done' on the Trello board. Such agile. Much 10x. Wow.

Later the product the 10x'er is working on also gets uploaded to NPM. And later still it gets used by the guys at Babel, who really do know what they're doing, and need to use this product because it solves some problem they're having. They don't even care about the string padding thing, that's only used on the product's CLI output, which they aren't going to use anyway. But they npm install the product and that installs your old left-pad package as a dependency because that's how NPM works. And Babel do write a really awesome product that nearly everyone uses. So now your little left-pad package is getting millions of downloads a day.

That's how it snowballs. But your solution is absolutely right, there should be some string library made up of dozens of functions like pad-left, and JS devs should be smart enough to only require in the functions they need from that library. But that's putting a lot of faith in JavaScript developers, and there so many shit JavaScript developers out there.

5

u/[deleted] Dec 31 '17 edited Dec 31 '17

You seem reluctant to write a one liner but you're sure willing to write a wall of text. Of course left padding isn't an exciting problem to solve, it's not even a problem to solve in the first place. TL;DR:

String.prototype.leftPad = function(pad_char, len) { return pad_char.repeat(Math.max(len - this.length, 0)) + this; };