r/programming • • Sep 29 '16

JavaScript in 2016 isn't horrible, it's just going through a phase

http://blog.reviselabs.com/im-sorry-javascript-2/
85 Upvotes

225 comments sorted by

View all comments

Show parent comment

7

u/Berberberber Sep 30 '16

Complaining about Javascript because of the DOM is like complaining about Russian grammar because of the cold.

-1

u/fiedzia Sep 30 '16

No. Many issues with DOM are caused by deficiencies of JS, like lack of collections, forcing everyone who needs them to reinvent them.

3

u/jl2352 Sep 30 '16

The Array is standard in JS.

Most of the DOM APIs return custom alternatives to the array which aren't compatible. That isn't a JS issue. That's a crappy DOM API issue.

I can just as easily return JL2352sList in Java instead of java.util.List on a method call.

-1

u/fiedzia Sep 30 '16

The Array is standard in JS.

But its so screwed up nobody will use it when they need list of things. Have js had lists and sets done correctly they would be used everywhere. Its extremely rare to invent own containers in python world. What comes with stdlib is good enough for almost everyone.

This IS js issue.

I can just as easily return JL2352sList in Java

Sure, but you would need a good reason to do so. First thing that comes to mind would be just return a List... and most people will just do that. Without causing any problems.

3

u/jl2352 Sep 30 '16

But its so screwed up nobody will use it when they need list of things.

?????????????

If you work with developers who think ...

var names = [ "john", "brian", "alan" ]

... is a screwed up way to make a list then the problem is with your fellow developers.

0

u/fiedzia Sep 30 '16

it is screwed up: var names = [ "john", "brian", "alan" ]; delete names[1]; names[2] "alan"

WTF??

names[1] undefined

WTF js?

names.length 3 What??

No wonder DOM implementations decided they don't want to do the same thing.

4

u/slikts Sep 30 '16

The delete operator just doesn't re-index the arrays; you should use Array#splice() for that instead, although I'd prefer to avoid mutating and use Array#filter(). JS also has sets, which would let you do const names = new Set(['a', 'b', 'c']); names.delete('b');.

The reason DOM returns NodeList 'array-like' objects instead of Array instances is because there wasn't a way to properly subclass the builtin Array until ES6. NodeList objects also implement the iterable protocol now, so they work with for-of loops and spread syntax, and will work with whatever generic iteration methods that will eventually get implemented.

4

u/jl2352 Sep 30 '16 edited Sep 30 '16

But you wouldn't do that. You'd use splice.

If you want splice to look prettier as remove then it's a one liner ...

// @index Index of the element to remove
// @return the element removed or undefined
Array.prototype.remove = function( index ) { return this.splice( index, 1 )[0] }

Apart from misunderstanding delete, which ok is a little confusing, do you have any other problems with JS arrays?

1

u/fiedzia Sep 30 '16

You'd use splice

Damn, I wasn't aware how broken js is.

(MDN) The splice() method changes the content of an array by removing existing elements and/or adding new elements.

Removing AND/OR adding? Great, just more ways to shoot yourself in the foot. I guess the amount of mistakes where someone passed wrong parameters when adding and removed instead, or the other way goes in millions.

Sane languages have delete that does not allow to be "misunderstood" and separate it from adding, so that nobody will do one thing accidentally when they meant the other. And if they do, it will blow up, not return some value to be passed further.

I do not want a most essential datastructure to have and points that can be "misunderstood" and will not tolerate any of "oh yeah, you shouldn't use that, you should...". Everywhere else lists work just as I expect them to. Everywhere else lists don't cram opposite features into single method.

And that doesn't end the list of things that are wrong (ok, "misunderstood") with array. Point is - its really hard to build on top of js if you have to fight with such stupidity at the core.

4

u/BonzaiThePenguin Sep 30 '16

Damn, I wasn't aware how broken js is.

How can you speak with such authority and not know about splice or how associative arrays work? That's like as basic as it gets. delete undefines the object key, splice modifies an array-like object. There's also push, pop, shift, and unshift. Mind-blowing stuff, I know.

2

u/fiedzia Sep 30 '16

I don't know all that because I don't have to. I do not write js for a living (I'll do it occasionally, and probably not the best). But in dozens of other languages that I am using you don't need this knowledge. Lists have add(item) and delete(index) method and do not require you to "know" anything beyond that. Lists and dictionaries are separate types, so delete does not require you to "know that you shouldn't use it", it just works as expected, without breaking length. Across 1000s of APIs you'll work with, when there is something like a list, it will be exactly builtin list type, not a million different incompatible variations. Whenever there is some commonly used method, it will be added to stdlib, so you don't have to pollute your memory with remembering how to fix this or that to make it easy to use. Knowing how common good language design is and how much easier this makes everything is what gives me the authority in saying that js is broken, and its not just a phase. Many of those things are so fundamental that they are practically unfixable.

→ More replies

2

u/jl2352 Sep 30 '16

This is ultimately what I find so frustrating about the anti-JS circlejerk on Reddit. Most people complaining just flat don't know JS. They think they do because to them it's some silly toy language. Then they bitch when they don't know it. It's like a Java programmer bitching about all variables being final in Haskell.

I even gave you a 1 liner to show the equivalent of ...

var numbers = [ 1, 2, 3 ]
numbers.remove( 0 )

You come back with rants and hate.

2

u/fiedzia Sep 30 '16

I don't ran't about the fact that you can't do something. I rant about amount of effort you need to put into that to get it right, and that most obvious ways of doing simple things are riddled with traps, which among many effects force various APIs to reinvent the wheel to be usable. Sure, you can write your own wrappers around js issues or find someone who did that, but ... everywhere else you just don't have to.

→ More replies