Tom Ritchford
2 min readJan 17, 2020

--

This just isn’t true — in at least two ways.

First, you can easily create private variables in Javascript classes. I was going to write it up, but then I remembered this classic article from twenty years ago.

And more, the main reason for classes is not “private variables”. I can say this fairly authoritatively, as I’ve been programming since before classes were a thing. In fact, we started using “classes” in languages with a series of hacks of languages like C before the languages themselves supported it, and certainly long before private variables were a thing at all. (I would also add that Python is object-oriented, Python programmers make heavy use of classes, and there is even a private variable feature — which almost no one uses!)

The main reason for classes and object-oriented code in general is better code re-use. The secondary reason is polymorphism. Private variables are only interesting because they make code reuse more reliable by preventing name collisions.

While we enjoy performant machines at the moment, the fact that Moore’s law is fading away can change all that.

Moore’s Law is fading away, yes, but it isn’t going to lead to slower CPU speeds in the future. If you are enjoying a “performant” machine today, you’ll be able to enjoy those same CPU speeds in the future too — and very likely, with a lot more cores.

Plain Javascript inherently performs badly because it’s an interpreted language where everything is an object. Classes are even somewhat worse, but I’m skeptical that switching away from classes can make a noticeable difference in the performance of a program. I would welcome some sort of evidence or real-world benchmark, however.

Classes introduce a straight top-to-bottom order and make changes harder to implement, which is unacceptable in most JS applications.

No, inheritance introduces the top-to-bottom order. Inheritance is considered dangerous for just this reason. The only good reason to use inheritance is an “is-a” relationship. Javascript doesn’t do inheritance well, and you’re generally better off not using it in that language. You can still put together classes in other, non-linear ways like composition, as you are aware.

Don’t get me wrong. Classes are one of JS’s most poorly-thought-out features. You should probably avoid them much of the time. I’m more or less agreeing with your conclusions — it’s just your reasoning I have problems with!

--

--

No responses yet