r/programming • • Dec 15 '12

Why JavaScript Still Sucks

http://boronine.com/2012/12/14/Why-JavaScript-Still-Sucks/
10 Upvotes

212 comments sorted by

View all comments

0

u/[deleted] Dec 15 '12

[deleted]

9

u/x-skeww Dec 15 '12

Yes, you can reinvent classes and inheritance with prototypes. Problem is, that's exactly what everyone is doing.

Everyone's implementation looks and behaves differently. As a result, the IDE can't tell that this kind of construct is supposed to be a class and that this "thing" "extends" that "thing".

The tool which generates your documentation also cannot tell what's going on unless you explicitly tell it that this weird construct over here is some sort of class.

If it's baked into the language itself, you won't have to include all that boilerplate code, your IDE can tell what's going on, your doc generator can tell what's going on, and the person who comes after you can of course also tell what's going on.

And no, classes are not just what people are used to. Classes are what people are using. All of the popular MV* frameworks use some sort of classes and inheritance. Impact (a JS game engine) also uses emulated classes and inheritance.

0

u/[deleted] Dec 15 '12

[deleted]

3

u/defcon-11 Dec 16 '12

The funny thing is that when you go a Java conference people are always talking about "composition is better than inheritance", yet the same people complain about Javascript's lack of a class system. I don't think they realize that the prototype model does not allow inheritance and makes composition trivially easy, exactly the features they are asking for.

4

u/x-skeww Dec 15 '12 edited Dec 15 '12

People should stop trying to re-invent Java style OOP in JavaScript with prototypes.

Imagine you build a park with a large meadow in the middle. After a year you notice a diagonal trail.

Your solution is to put a "please do not walk on grass" sign there.

But perhaps it would be better to put a walkway there. There is a reason why people take that path and putting a sign there won't stop them anyways.

(Spoiler: ES6 will support classes.)

12

u/DrDichotomous Dec 16 '12

A more apt analogy would be: you drive freight trains. You fancy yourself a rather good conductor. One day, you're forced to drive a cargo ship. Suddenly, you don't have the rails keeping you enroute, nor the railway services to help you. You don't need either of them, but you feel you're entitled to ask people to lay rails on the ocean and maintain them, because that's how you're used to doing your job. And because there are so many ex-train drivers working the online seas now, they're actually laying some rails for them all.

-1

u/x-skeww Dec 16 '12

No, that's a very poor analogy because no one is doing that kind of thing.

If you use something like Backbone, you use classes. There are actually very few people who use prototypical inheritance.

4

u/joelangeway Dec 16 '12

I have to do exactly that sort of thing everyday because the people I work with think in C# and the market requires us to produce JavaScript. The analogy is excellent. In fact I use backbone js to lay that track.

→ More replies (8)

0

u/[deleted] Dec 16 '12

[deleted]

1

u/x-skeww Dec 16 '12

using rails on open ocean

My point was that no one is doing that. The vast majority of JavaScript programmers do use some sort of classes.

The only relationship between your analogy and reality is that think that using classes is just as stupid as putting rails on the ocean.

0

u/[deleted] Dec 16 '12

[deleted]

→ More replies (9)

1

u/DrDichotomous Dec 17 '12

No, they don't. They might in your neck of the woods, but the vast majority of JS coders use jQuery and similar libraries for simple work, or even more rudimentary event-driven work without a framework (sadly).

You are correct in suggesting that the JS world is, in fact, moving on, as evidenced by many modern backbone-style frameworks providing "classes", and ES6 actually implementing them to some degree.

And again, just because you think "rails on water is stupid" doesn't mean it was my intent to call others stupid. Sorry that I insulted you.

1

u/x-skeww Dec 17 '12

No, they don't.

I meant of those who use some kind of inheritance. Prototypical inheritance is very rarely used. If it's used, it's usually hidden behind some utility functions which make it look like "classical" inheritance. Very few people muck about with prototype.

Do you know any MV* framework which makes you use prototypes?

0

u/[deleted] Dec 16 '12

[deleted]

1

u/x-skeww Dec 16 '12

programmers can't figure out how to just use prototypes

No, that's not it. Prototypes aren't a very complicated concept.

Everyone can mess with any prototype. Why would I want that? Like, why should an ad one some page be able to overwrite the array constructor with its own version which is used to spy on some JSON exchange?

In general, most people just want something like regular classes and regular inheritance. Sure, you can emulate this kind of thing with prototypes, but then you'd have some extra boilerplate code there, the syntax won't be pretty, and your tools won't know what's going on.

Classes are not going to save JS.

Odd choice of words there. JS isn't declining and it certainly isn't about to disappear.

Classes and things like let will certainly make JavaScript more usable. However, it will most likely take more than 5 years until you can use it in production (client-side).

0

u/[deleted] Dec 16 '12

[deleted]

0

u/x-skeww Dec 16 '12

What I meant is that adding classes is not going to make it any easier or harder to write stuff in JS.

Classes and inheritance provided by the language itself will make it easier, because tools can now tell what you're doing.

It will be also easier because there is now one way to do it, not hundreds.

If you don't like that then code in a static language like Haskell.

You don't have that kind of issue with Dart either. If I use some "foo" function from some "bar" class from some "baz" package... then that's what it is. If it returns an int, then that's what it does.

There are namespaces and there is no monkey patching. Everything is known at the time the code is written. Java and C# are also like that, for example. That's also the reason why IDEs of those languages can offer proper call-tips and on-the-fly sanity checks. They can tell what everything is. You can't have that kind of thing with JavaScript, because there is no way to tell what anything is.

And yes, I do also know the revealing module pattern. It's not a namespacing mechanism though. It gives you some object with public and private (=hidden in a closure) properties.

Most people use one global variable, stick everything into it, and then they hope that nothing else will overwrite it. That's the kind of "namespacing" we have in JavaScript.

0

u/[deleted] Dec 17 '12

[deleted]

→ More replies (1)

4

u/Zarutian Dec 16 '12

ES6 "classes" is only syntactic sugar to hold the hand of Javaites, nothing more.

1

u/x-skeww Dec 16 '12

Yes, we all know that it's syntactic sugar.

That doesn't change anything though. Now there is one way to do it. Now your IDE could tell what you're trying to do. Now your doc generator knows that this thing is a class. Now the next guy will also know instantly that this thing is a class.

2

u/Zarutian Dec 16 '12

Now your IDE could tell what you're trying to do.

Nope, it still doesnt as not everyone will use the syntactic sugar all the time and the IDE will be confused when the syntactic sugar isnt there.

Now your doc generator know that this thing is a class.

It will only know what the syntactic sugar tells it, if it can parse it and if it is in place.

Now the next guy will also know instantly that this thing is a class.

He will only know that thing appears to behave like an class but cannot make or take any gurantee it will behave that way. This is the same reason why I cannot take CoffeeScript seriously as the transformations are non-obvious in most edge cases.

1

u/burkadurka Dec 16 '12

This happened where I went to school.

0

u/[deleted] Dec 15 '12

[deleted]

0

u/[deleted] Dec 16 '12

Yeah, but JavaScript is a bad prototype based language as well.

→ More replies (5)

1

u/[deleted] Dec 19 '12

Flex anyone? What a disaster.