r/programming • u/boronine • Dec 15 '12
Why JavaScript Still Sucks
http://boronine.com/2012/12/14/Why-JavaScript-Still-Sucks/19
u/Zarutian Dec 15 '12
Sorry to burst your bubble but OOP is not Class Oriented Programming.
And not agreeing on how to instanciate objects? The agreement is to always use constructor functions.
6
Dec 16 '12
I think what jump-started JavaScript's reemergence was Google's work on the V8 engine. Suddenly, JavaScript was fast. Real fast.
People have a short memory. Around the same time V8 came out, also SquirrelFish Extreme aka Nitro and TraceMonkey also came out, and they were also ridiculously fast. Each beat V8 on various benchmarks, like V8 beat them on others.
5
34
u/unpopular_opinion Dec 15 '12
Yet another content free blog.
-1
u/bonch Dec 17 '12
There's plenty of content. You just don't agree with it, so you're going to vote it down and claim it's content-free.
2
15
u/tmetler Dec 15 '12
The fact that he doesn't seem to comprehend how prototypical inheritance works pretty much discredits all his points... I agree that using the prototype is not in your face obvious, but with new compiled languages it isn't a problem any more if you want to use them. Otherwise, yes, you should learn how the prototype works and master it.
3
u/MatrixFrog Dec 18 '12
Yes, yes, OOP vs. FP, classes vs. prototypes. Was I the only one amused by this part though?
I didn't even get through the introduction before the first problem appeared: my mom and stepdad didn't want me installing software on the family computer—I was left without a Java compiler. I remember double checking my code before uploading it to an online compiler, then waiting, downloading the jar... rinse, repeat. Boy was it tedious.
"You can't install something! It could be a virus!"
"Okay, I'll just upload code to a server and let it send a jar back to me and then I'll just run the jar I downloaded from the internet because what could possibly go wrong?"
5
u/ramigb Dec 16 '12
this is a "non-sense" post :), JS wasn't "kickstarted" again because of V8, actually it was never gone, jQuery, prototype, mootools, and a huge set of libs made JS very easy to learn and use, actually HTML was holding it back, it just didn't have a solid partner (HTML), now HTML5 is here for the rescue, and technologies like Flash and Silverlight are becoming older and older with each tick, JS isn't a perfect language, but it sure kicks ass and makes front end developemnt a seamless job, no one can beat JS at this field, imo, Node.js and the culture, hype, and carelessness built around it is what makes JS look bad.
5
u/mpyne Dec 16 '12
Wow, 159 comments, 9 article karma. At least OP probably got a "controversial" trophy for his trophy box.
6
u/x-skeww Dec 15 '12 edited Dec 16 '12
I fail to see a compelling reason for [Dart] other than some Google engineers ignoring reality in favor of making a cool new language from scratch.
Well, Google tried all kinds of things. They created the Closure Compiler, which added type annotations to allow slightly better tooling and dead code removal. Unfortunately, it's kinda tedious to use. Putting those annotations everywhere is pretty annoying and it also makes the code somewhat noisy.
Then there is GWT, which certainly is an excellent piece of engineering. However, it also means using Java, which just feels too bulky for this kind of thing.
Dart is the next logical step.
JavaScript with annotations:
/**
* Foos the bar.
* @param x {int}
* @param y {int}
* @return {String}
*/
function foo (x, y) {
[...]
}Dart with exactly the same annotations:
/// Foos the bar.
String foo (int x, int y) {
[...]
}The latter is a lot less annoying. Plus, those annotations will be used by your IDE right away. If you now pass the wrong kind of thing or do the wrong kind of the stuff with the return value, your IDE will immediately tell you about it.
There are also things like built-in classes/inheritance (the IDE knows of course how those things fit together), proper lexical scoping (JS' and AS3's function scope is very inconvenient), a this which behaves as expected, isolates (actor-like concurrency), packages/namespaces, and far more predictability in general.
It's a lot of fun to use. I used a dozen different languages in the past and right now Dart is (obviously) my favorite. From my point of view, it just offers the right kind of balance. The syntax is very lightweight (the type annotations are only important at the API boundaries) and it offers enough built-in structure for creating huge applications. The tooling is also really great.
Well, it certainly isn't the right thing for most websites, which just include jQuery, a few jQuery plugins, and then glue it all together with maybe 50-200 lines of code. jQuery, a few jQuery plugins, and some glue code already solve this kind of problem just fine.
However, it's just perfect for actual applications like SPAs or games.
You can also write server-side code with it. There is a standalone VM which could be theoretically used for all kinds of things.
The editor is also shipped with a version of Chromium which is able to run Dart code directly. That's really neat for testing and debugging. You can step through your code inside your IDE. You can add break points while it's running. That kind of thing.
The Dart VM already outperforms V8 (Chrome's JS VM) in many cases. Their goal is to make it twice as fast as V8 and, by the looks of it, it appears they will manage to reach that goal pretty soon. If snapshots are used, the startup only takes 1/10 of the time.
While we won't see embedded Dart VMs in all modern browsers for the foreseeable future, having that VM is still very useful. Apart from debugging and writing server-side applications (or general scripting/automation stuff), it might be also become an option for writing Android applications. So far there is no ARM VM, but it's just a matter of time.
Sure, you can already run Dart applications which were compiled to JavaScript on Android handsets. However, running it in a Dart VM would be quite a bit faster and it would also reduce startup time drastically.
3
u/defcon-11 Dec 16 '12
Yep, Dart was written from the ground up with the express intention to be friendly to JIT compilers. Instead of designing a JIT for an existing language, they designed a language for a JIT, so it has huge potential performance wise. With its ability to compile to JS and run in Chrome, it might just have a chance, especially as Chrome grows market share.
2
u/gerdr Dec 16 '12
This is not the first time I've read that v8 somehow kick-started the Javascript revolution, but it actually happened the other way around:
While client-server communication using Javascript has been around for a long time (eg via hidden iframes), XMLHttpRequest made it more convenient and over time, JS web apps became ubiquitous.
The increased use of JS led to the development of high-performance engines which in turn led to an increased interest in server-side JS, but the trigger event was by no means the development of v8 (first released in 2008) - by that time, Gmail and Google Maps had been around for years, and development of Mozilla's TraceMonkey had already started.
2
u/Venar303 Dec 16 '12
why do so many people post opinion articles as if they are facts in /r/programming...
1
2
12
u/x86_64Ubuntu Dec 15 '12
I agree completely. After Flex and Silverlight both got shitcanned, I ventured into the HTML5/JS/CSS (HJC) world to not become one of those programmers stuck in the past. What I saw horrified me. I'm not going to go into detail because I have done so in my post history, but here are a few things that would have to happen to warm me up to the JS world for making web applications. Mind you, my main JS experience has been with ExtJS/Sencha Touch
Components - As an application developer, making a view ( window, list, navbar ) and then having to go into CSS and screw around with selectors, parents and divs is very inefficient. There aren't but so many components/containers used to make a webapp and layout should generally be a property of the container. A declarative framework would be much better.
Strong Typing/OOP/Object serialization - The lack of objects is "okay" until your app gets huge. Having to run around and refactor each and every piece of code is annoying and time consuming because you have to find out where the class was declared. Serialization from dataservices to JS objects seems like a bolted on afterthought. An example is with the ExtJS family. If you bring back a Classroom object, the Classroom gets deserialized, but the Student objects don't, you have to jump through hoops to get your Students into objects.
P.S For some reason I don't feel the same way about RoR and probaly Python if I knew it. I can't say why, but I don't miss strong typing for RoR. Disclaimer, my RoR skills are at the beginner level right now, so I am still in the "Oh, this is nice" phase as opposed to the "all this stuff is getting in my way" phase that experienced people achieve.
3. Overall weirdness - While ambiguous, you know what I mean. With my ExtJS stuff, if I have a listener that invokes a non-existing or misspelled function, I don't get an error saying "function not found BrokenClass.js Line 93". Instead, I get a blank white screen and an error that has absolutely nothing to do with what the error was or what class is causing the error. This right here makes coding exceptionally difficult not to mention I don't know of any other programming construct that chokes on something and goes "I'll never tell".
All in all, I think JS is great for delivering some UI dynamism to a protocol designed to push static boring ass documents. But when it comes to making apps it falls flat. Which brings up the issue of where does the JS world go from here ? I have no idea to be honest. The JS emitter technologies are nice and are probably the only way to go over the next 5 years. With enmity between the browser vendors, and specs never being completed and never being implemented, I don't think we will ever see a bytecode "write once, run anywhere" type of construct outside of the plugin world.
Disclaimer: I am still butthurt about the demise and uncertainty of the Flex world.
2
u/gogoyellowscreen Dec 16 '12
Is HJC a thing? I thought HTML5 was the collective term for all the things.
1
u/x86_64Ubuntu Dec 16 '12
I don't know, I just got tired of typing out HTML5/JS/CSS. Also in my case, I wanted to highlight problems with all of the stack components.
2
Dec 16 '12
May I suggest looking at Angular.js ? This library has completely changed the way I write web apps and has made JS much better to use!
6
u/donvito Dec 15 '12
I'm a Mac dekstop software developer and using JavaScript for webdev feels like a time travel back to 1995.
It's really sad as web is the future for most software.
3
u/skidooer Dec 16 '12 edited Dec 16 '12
This is why we have Cappuccino.
In fact, I'd say most web frameworks these days take at least some inspiration from OpenStep, though perhaps not to the extent the above does. Just because the platform is the web browser doesn't mean we get to throw out years of experience, and developers are starting to wise up to that fact.
I don't think it's as bleak as you suggest, but I can understand why leaving something that has been in development for ~25 years for something that has one or two years under its belt is a little saddening.
4
u/x86_64Ubuntu Dec 15 '12
...It's really sad as web is the future for most software.
Isn't it though ? I really wish there were some declarative view platforms out there that weren't provided by Microsoft. It seems like everyone has said "nope, this is good enough" and just stagnated. Then you compare the backend world, and see how it is has been driven by competition and ingenuity with people saying "fuck this shit, I wish there was a technology or library that made things easier".
1
u/ysangkok Dec 16 '12
QML?
1
u/x86_64Ubuntu Dec 16 '12
I tried using Qt, but couldn't tell which was was up or down. You get hit by so many concepts that you don't know which ones you need to get your job done and how they relate to one another. I would definitely put that in the good enough category.
1
u/Smallpaul Dec 16 '12
What does "declarative view platforms" have to do with it?
The web is not popular because it is a "declarative view platform". The web is popular because it had a gigantic installed base before it WAS an application development platform.
1
u/x86_64Ubuntu Dec 16 '12
I don't see how reading this line in my post
I really wish there were some declarative view platforms out there that weren't provided by Microsoft
leads to your response.
1
u/defcon-11 Dec 16 '12
I disagree. With the exception of alternatives to RDBs, backend technology and best practices are essentially the same as they were 10 years ago. Front end JS technology and best practices changes almost weekly. Declarative view framework? Isn't that what HTML and CSS have become?
0
u/X8qV Dec 16 '12
Then you compare the backend world, and see how it is has been driven by competition and ingenuity
... and PHP
1
u/x86_64Ubuntu Dec 16 '12
Exactly, the backend world is doing its damndest to get away from PHP. But just like JS, its too easy to slap stuff together and it has such a wide install and user base its here till the end of time.
0
Dec 15 '12
[deleted]
-1
u/RichardDurr Dec 15 '12
Yeah, except Cocoah doesn't do that anymore and the box-model has neither a grid nor vertical centering. LOL
3
u/mbetter Dec 16 '12
I don't think you said a single thing about Javascript in this post.
1
u/x86_64Ubuntu Dec 16 '12
I did ! The lack of strong typing, the way errors are handle in the JS world.
2
u/icantthinkofone Dec 15 '12
...here are a few things that would have to happen to warm me up to the JS world for making web applications.
Soooo what else ya' gonna' use?
1
u/x86_64Ubuntu Dec 15 '12
That's the problem. Nothing else can really compete with JS because it is the accepted currency and future of the web. It works without a plugin on almost every device with full support. Even if it were to happen, the demise of JS will happen through the usage of JS, nothing can come beside it and take it out. For that reason, I believe the JS emitter techs are the first steps to moving on.
1
u/defcon-11 Dec 16 '12
Any of the 10+ languages that compile to JS?
0
u/cooljeanius Dec 17 '12
That doesn't really count
2
u/defcon-11 Dec 17 '12
Why not? The goal of transpiling is to improve usability and evolve faster than JavaScript allows. Even the standards bodies are embracing it, ECMA script 6 will have new features with the explicit goal to make transpiling easier.
1
u/defcon-11 Dec 16 '12 edited Dec 16 '12
ExtJs layout system sucks monkey balls; straight css is far easier to use unless you have to support legacy browsers ie7 and 6. ExtJs does allow you to deserialize related objects. Most of ExtJs problems are because it tries to hard to be Flex/Java style. Frameworks that use the best features of JS and don't try to be Flex/Java such as Angular, Ember, Backbone are easier to use and more maintainable IMO.
0
Dec 16 '12
1
u/x86_64Ubuntu Dec 16 '12
I've tried Jquery at first because it seemed to be the defacto standard, but it still didn't give me the components I need to take over the world.
1
Dec 16 '12
Sure, JQuery is mostly for DOM manipulation, but i've found that combining it with JQueryUi and Bootstrap gives you basically all the standard components you might need.
4
u/Heterogenic Dec 15 '12
For what it's worth, I've been dipping my toe into Dart for a while, and am just about ready to make it my primary (web) language.
It compiles to JS, has classes and typing, and a great free integrated IDE. It turns out that's all I wanted all along. For all its flaws and inadequacies, I find myself loving it.
And if I ever drop it for a better JS environment, I'll just compile my projects to JS and carry on. It ends up pretty readable
2
u/icantthinkofone Dec 15 '12
Been thinking about setting some time aside to do this, too.
2
u/Heterogenic Dec 15 '12
Honestly, I'm glad I did. The other alternative is Kotlin, and I'm going to give that a shot before settling on Dart.
Using a Node stack for now makes Dart a great option, but in the future Java just has better server-side infrastructure and libraries.
1
u/x-skeww Dec 15 '12
Learning Dart takes very little time. In hackatons people were productive after just a few hours. The syntax if familiar and there aren't any nasty surprises. There is proper lexical scoping.
thisis what you think it is.==and!=do what you'd think they do.Unlike JavaScript, it's very straightforward. If you've used something like Java or C# before, you'll be fine.
1
u/ysangkok Dec 16 '12
How does it compare to GWT?
1
u/x-skeww Dec 16 '12
Using GTW means using Java. Java is a perfectly usable language, but many people consider it to be too verbose.
I used to like Java, but I just got too used to things like first-class functions, lambdas, closures, and things like that. However, I also do think that Java did some things very well. Its tooling and documentation is top notch.
Dart is more dynamic and more expressive, but it still gives me those things I really loved about Java: excellent tooling and cross-referenced documentation. You only have to add type annotations at the API boundaries (arguments and return values) to get those benefits. Since those annotations are baked into the syntax itself, they are a lot more lightweight than those used by the Closure Compiler. Since those type annotations are used by the Dart Editor right away, you're also far more inclined to add them.
2
u/artsrc Dec 16 '12
JavaScript is hard to evolve in the context of its installed base of systems, programs and other infrastructure. That is why it still sucks. This is not addressed at all in the article.
Java has a really useful standard library. The combination of Joda Time and the Java Calendar class show the issues of a standard library.
Npm really is a better way.
OSGI and DI containers show that rich module systems have real value. A more evolvable module system has real advantages.
Why does Objective C still suck?
2
2
Dec 16 '12
OOP argument aside, JavaScript does suck.
I can use JavaScript pretty well and do so on a regular basis. With modern tools like CoffeeScript, jQuery, Backbone and Underscore it's even a frequently pleasant task to write JS! But I've been programming for a very long time, and also I've been programming JS for a very long time, to the point where I'm used to a lot of the weird, stupid parts of the language.
Recently, though, I was pairing with somebody who is a novice programmer, and kept finding myself having to stop every minute or so to explain and then apologise about some part of JS. Think all of the binding of 'this'. Think about the bizarre object model. Think how hard it is to call the super of a method. Think of how difficult it is to organise code in a sensible manner without adding the complexity of a library like Require.js, which you then also have to explain. Consider the weird non-array 'arguments' object in the function. Etc. etc. You know all this stuff because we complain about it all the time, but we know learnt it long ago and it's stuck in our head.
But I think about going back to scratch and learning JS again as a novice and it's a frightening concept - especially while you're also trying to figure out how the DOM and the event model works. It's just too much information. Considering JS was supposed to be a simple scripting language for the web, it's got a hell of a lot of unnecessary ugly complexity. It's only really suitable for people who already know how to program reasonably well.
1
u/forgeflow Dec 20 '12
What language was this article written in? It seemed like some kind of bastardized subset of English, full of sentence fragments and random italics. I couldn't parse it past the first paragraph, and gave up.
0
u/buddhabrot Dec 15 '12
I don't get it.. JavaScript is a very expressive language, if you use it as a functional language instead of an imperative one. It has a lot of weird quirks that just suck (the undefined type matching), but the way you can use closures and weak type system gives you a dynamic toolbox that is unparallelled by any "OOP" language.
-1
u/skidooer Dec 16 '12 edited Dec 16 '12
First, Javascript is an OOP language. How can it be unparalleled by itself?
Second, many OOP languages do support functional-style constructs. Language features are never completely binary. Ruby, for instance, makes heavy use of map/reduce patterns, among other functional things.
Third, C is a weakly typed language that recently received closures. By your comment, it seems that it should make for a great functional language too?
0
u/buddhabrot Dec 16 '12
OOP is a word that (I hope) will die out in a couple of years. I have no idea what it means and I've been a programmer for decades. Writing in an object-oriented manner in C is indeed pretty similar to doing it in Javascript, using function pointers. Admittedly though, the variable scoping in Javascript is a lot more suited to closuring than in C, going overboard with FP in C quickly becomes awkward and complex.
1
u/contantofaz Dec 16 '12 edited Dec 16 '12
OOP was over-abused in Java. Luckily the younger generations didn't have to pay attention to the madness of when Java was thriving.
On the web with both JavaScript and Dart, I have been using more composition than inheritance. Because stuff doesn't map out 1-1 as one parent class and one subclass would lead you to believe. I guess that's the main reason folks haven't been able to move forward with JavaScript. Making APIs is hard with or without OOP.
JavaScript "just works! (tm)" until it does not. Again, making APis is difficult, with or without JavaScript. Having used a plethora of JavaScript libraries before, however shortly, I guess they all remind me of the Java madness that they followed.
There's no mistaking though, that for as much as we hate those past technologies for one reason or another, they have carried us very far. C++ may be buggy, but it powers these browsers. Java couldn't replace C++ for them. C# couldn't either. Ugly code, ugly systems, deliver great usability after a lot of manpower is deployed on them. But then some of that goes to waste when some of the projects lose their appeal and funds and become history instead. Even Java has many stories of losing it out once funds were gone.
And that brings us to software engineering. With funds, folks can do wonders. Without funds, folks have to scrape by. Then it's no wonder that most web development should be done with very little funding and corners must be cut in not so glorious ways to the disgust of those seeking software correctness above all else.
1
u/gerdr Dec 16 '12
OOP is a word that (I hope) will die out in a couple of years. I have no idea what it means and I've been a programmer for decades.
OOP is a design pattern to structure imperative code. It models computation as communication (message passing) between stateful objects (encapsulation).
An OO language is a programming language which makes using this pattern easy. Class-based inheritance is one possible way to achieve this: It provides a mechanism of code-reuse for ease of use as well as a type system for correctness.
0
u/wadcann Dec 17 '12
Stop defending JavaScript as a language and start embracing JavaScript as a platform, and as a compilation target.
That I can generally agree with.
People can laugh at Java all they want but when I look at Java documentation I see exactly which objects I can make, exactly which parameters each method takes, exactly which errors they throw. The best I could hope for with a JavaScript library is a fancy piece of example code. This makes it nearly impossible to write correct software, unless you read the source code of the library you are using. Even such integral JavaScript projects as Express have lousy API documentation.
Yeah, this still irks me when I go from C POSIX (where behaviors tend to be precisely-specified) to Ruby or Python (where behaviors tend to be "here's an example; try it out and see what it does in various situations").
One thing I am ready to bet on: the winner will be a compile-to-JS language.
Yup. I am curious as to any fundamental performance limitations with JS-based languages; AFAIK right now it's reasonable to expect JS to run as quickly as native code.
-1
u/contantofaz Dec 15 '12
It's easy to bash JavaScript. But it takes a user to know about its shortcomings. Sometimes people bash it and never used much of it. JavaScript makes for hideous code. And the more code, the more hideous it gets. It's no wonder then that people shun large code bases in JavaScript.
Then what to do? People try to write less JavaScript code by adopting jQuery and CoffeeScript and such. But their code ends up being hideous in different ways. In jQuery you have no encapsulation, it's statement after statement, function after function, event after event, in one or two JavaScript files at most. In CoffeeScript you get some encapsulation, but then you get to compile a bunch of JavaScript code afterwards. And still the CoffeeScript code itself is unusual enough to make you to think about it way too long and yet as it's not much different from JavaScript, once again you become afraid of larger code bases.
I've found great peace at Dart, but it's early days for Dart as well. I really like programming in Dart based on launching the Chromium version that understands Dart code from the Dart Editor itself. It feels like standard JavaScript in that regard. And the language being better and the editor warning of errors gets us a bit more confidence needed for larger code bases.
TypeScript is more of a joke in comparison. Again it's a little too much like JavaScript and encapsulation should suffer a bit without getters/setters and stuff. Then again, if you need shipping code that works across browsers, that could be your ticket to it.
With Dart I can then compile my code to JavaScript and run it relatively successfully on both Chrome and Firefox. But I've failed to run it on IE for now, for example.
Having converted a library that I started on JavaScript to Dart, I've been able to enjoy the features of Dart all the much more. While JavaScript gave me a good chance to get things off the ground, it was never polished enough. And the code of JavaScript feels bloated compared to the Dart I get with getters and setters and not having to use "===" when just "==" does the job in Dart.
Dart adds some cognitive load that's not present in JavaScript, like the two numeric types int and double and so on. So in Dart a division can get your ints to a double and mess up something down the code. It serves to remind that even types can make things a mess. If you're like me and can do without types making a mess of things, Dart could be for you, as I mostly program without types, waiting to add some types only for documentation purposes at some point. Then again, if you let other people to direct your thoughts, other Dart users could get you using types in no time too.
I refuse for example to add for (int i = 0; i < 100; i++) {} into my programs.
2
u/Zarutian Dec 16 '12
let me guess you replace i++ with i += 1 ?
0
u/contantofaz Dec 16 '12 edited Dec 16 '12
I was talking about types. I don't use local types just because I can. Ruby and JavaScript before Dart didn't require me to do so. And Java and Delphi lost their appeal during the transition to the web. In JavaScript I learned from the JSLint that declaring variables at the top of the function was a good convention. And that reminded me of Delphi (and Pascal) for that matter which also declared them first.
In Java and C# and languages that followed Java folks declared types everywhere, even mid statement to do type casting and such. While that's cool when you're just calling some library, it's not so cool when you don't even have a library or program yet. In other words, if you're still developing some prototype, having to deal with intrusive types the way Java and others like it did seems counter-productive.
Sample in Dart:
doSomething(String s) { var i, len = s.length; for (i = 0; i < len; i++) { // Handy stuff here. } for (i = 0; i < len; i++) { // More handy stuff here. } }And that should be about as good as we are ever going to get it.
0
u/eean Dec 16 '12
I think Dart is really interested since it's designed (by aforementioned V8 developers) to be really easy to compile to machine code. JavaScript is quite unnecessarily dynamic. V8 manages JIT, but it's not like the browser can keep a compiled version of particular JS file around. This is exactly what Dart enables.
They are also building in a standard library, which the author rightly notes is a glaring omission in JavaScript.
0
u/bonch Dec 17 '12
All the web "developers" are going to flood this article with downvotes even though in their hearts they know that JavaScript does, in fact, suck.
-9
-3
u/raving_coder Dec 17 '12
Having written hundreds of thousands of lines of code in JS, and millions across almost 25 other languages, I too think that Javascript sucks. It was a poorly designed, and even more poorly implemented, language quickly thrown together to add interactivity to web pages ...and it worked great for doing cutesy little tasks mike making snowflakes fall and playing MOD files.
But for building web apps and back-end infrastructures ...don't be silly. Lacking standard libraries or even a cachable module system, enormous amounts of code need to be downloaded to the user's client, sometimes every session, and it can make the user experience awful.
And as for node.js, I can't even imagine the legions of hackers standing eagerly by waiting for the first generation of "node.js" websites to start coming online ...all using hastily written libraries that simultaneously re-invent the wheel and are likely full of all sorts of security holes.
JavaScript should stay in the browser, we should look at it as a compilation target for now, and get out of there as quickly as possible in favor of native client.
0
Dec 15 '12
[deleted]
27
12
Dec 16 '12
The fact that JS is object-oriented is irrelevant. That isn't the issue here that the OP has, and I agree with him. The problem is that it becomes difficult to express what you are building with JavaScript's prototypes.
For example:
function foo() { }What is this built for? Is it a standard function? Am I building a class? Is it a function with properties (like the jQuery $)? It could end up as any of them; my intention is not clear.
What is worse, is that there a 100s of subtly different ways of building common idioms, like classes and modules. So two projects could both claim to be using standard classes, and both have code which is put together in different ways. The fact that there are 100s of libraries, and hand written alternatives, for building a class, is proof of this.
That's really bad for maintenance.
0
Dec 16 '12
[deleted]
1
Dec 16 '12
And it's not a class, there are no classes and you shouldn't be building classes, it's not a class based language.
Not having first hand support for classes does not mean you cannot build them. You can build classes in C! It just has to be built manually, which is the issue here, because it leads to lots of subtly different custom ways to build classes.
Most of the time, when a JS developer is working in an OO way, they need to use classes. Ok, they are using a 'constructor function with more functions added to it's prototype', not a class. However a class is really what they are conceptually building, and so a class is really what they want. Not being able to express that directly, is bad.
When people don't want classes, is either with very complex OO code (which is usually for maintenance), or if they are writing non-OO code.
0
Dec 16 '12
[deleted]
1
Dec 16 '12
And to define how those objects will work, most of the time you just want a class.
With JS prototypes, the syntax is far too verbose. You can build your own code to handle that, but that relates to my earlier point, that you end up with lots of subtly different versions. It's bad for maintenance when that's not standardised.
The other problem is that again, to describe the object, people just want to build a class. The extra abilities that prototypes bring, just complicate matters more than they help.
For example having an objects prototype change, or the functions assigned to that prototype change, or having new functions set to an object to replace that come from the prototype; they all make code much more difficult to reason about.
When you get into using multiple prototypes together, composure and inheritance tend to be better alternatives, because they better describe what you are trying to achieve.
To sum it up, if people are just using constructors/prototypes in class-like ways, why not just give them classes? The fact that they are also less powerful than prototypes, is also what makes them easier to reason about.
→ More replies (3)13
Dec 15 '12
Problem with JS's prototypes is that they resist any attempt to tool. For all the faults of the Java community, they have some really excellent developer tools.
For example, in JS there's a dozen ways to do inheritance, a dozen ways of making a "module", a dozen ways of attaching a method to a "class".
It's all very well saying that JS's prototypes are an alternative to classes, but they're not a better alternative.
9
u/strager Dec 15 '12
a dozen ways of attaching a method to a "class".
Can you elaborate? I see only two ways:
assignment:
A.prototype.f = ...;
Object.defineProperty:Object.defineProperty(A.prototype, 'f', { value: ... });5
Dec 16 '12
There are only a handful of substantially different ways to do classes. However they can in turn be done in one of hundreds of subtly different ways. The fact that we have dozens of class building libraries for JS, very few being identical, is proof of this. There are hundreds more custom versions scattered within JS projects. You even get projects using different class building code, within the same project. That's really bad for maintenance.
0
u/smog_alado Dec 16 '12
The problem is that there is more then one way to write code that does this and in general they are not amenable to static checking (for verification or documentation purposes). For example, jQuery has
extend, Dojo hasdeclare, etc.And this is not even counting the situations where you create "pseudoclasses" using closures. There are tons of different variations of that.
function make_gizmo(a,c,b){ function frobnicate(){ ... } return { ... } }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.
1
0
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
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
0
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
letwill 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
0
Dec 15 '12
[deleted]
0
-8
u/x-skeww Dec 15 '12
The point was that people prefer to use classes. Therefore it makes sense to have classes, because then your tools can also tell that these things are indeed classes.
0
Dec 15 '12
[deleted]
1
u/x-skeww Dec 16 '12
The vast majority of JavaScript programmers use some sort of emulated classes.
they bitch about how the language isn't like other languages they know
You're pretty ignorant yourself. Those complains aren't inherently wrong. For example, the scoping in JavaScript is just plain stupid. Other languages don't do that kind of thing, because it would be really annoying. So, yes, this part is different, but it's also way worse. Complaining about this is perfectly valid.
0
1
u/shevegen Dec 16 '12
He still has a valid point though - syntactic sugar.
JavaScript does not care about it, and there is no alternative to it.
1
u/gargantuan Dec 16 '12
At this point it doesn't matter much if due to some pure theoretical definition JS OO is more OO than OO in class based languages. Most programmers today have encountered and used OO in class based language, like it or not that is the default "OO" when we talk about OO anything else is "something else". Heck I could argue that Erlang is OO because methods are just messages being sent and not functions to be called which is more in tune with the original definition but I say Erlang is the most OO language I should be expected to be clapped across the moth (figuratively speaking, hopefully).
1
Dec 17 '12
Came here to search for a comment that would bring prototype up.
Thank you!
Class is just one way of doing OOP (or interpretation of how to go about doing OOP). Sheesh.
-6
Dec 15 '12
According to his first sentence he's also no more than 24 years old, maybe more like 23 or 22. Which puts him at most in the professional world for 1-2 years. That's about the same as me right now and I still have relatively little grasp on the primary languages I've been working in.
9
Dec 15 '12
I forgot that you couldn't get your professional programmer's license until you were 21.
1
Dec 16 '12
Hm I don't really understand what that means. But he said he got his first book when he waas 13, it came out in 2001. Which means he's probably my age, and I have no idea what I'm doing, much less confident enough to be instructing other people.
0
u/RizzlaPlus Dec 16 '12
it's funny because javascript compilers actually try to infer classes from javascript objects to optimise the code.
And prototypes are not more OOP than classes. OOP, as its name says, is about working with objects which both prototypes and classes provide. They're different formalisation of objects.
0
Dec 16 '12
[deleted]
2
Dec 16 '12
In most class based languages, classes are objects. Such as Ruby, or Java.
→ More replies (13)
0
Dec 15 '12
[deleted]
3
Dec 16 '12
Then again if you are so reliant on JS that your client side codebase grows to such proportions as to require modules and namespaces then you're probably doing it wrong and should offload some of the workload to the server
For many web apps it would not make sense to call over to the server. For example, a browser based editor. Why would that need to call the server?
The other issue is latency. I built a game building site which uses it's own language, and the compiler runs client side, because it means you get practically instant compile times. It would also suck if it had to talk to the server, every time you hit compile; what if your router just crashed/rebooted (which mine does all the time)?
Another example for latency is syntax highlighters. If it's running client side, it can syntax highlight in real time. If it's server side, it cannot.
There are a lot more examples, where it just doesn't make sense to move things over to the server.
0
9
u/ethraax Dec 15 '12
What about the lack of block-level scopes? Or the fact that constructors still "work" if you forget the
newkeyword, but they end up attaching functions/properties to the global object instead (which is pretty much never what you want to do)?2
u/defcon-11 Dec 16 '12
No one complains about function scoped variables in Python, but for some reason it's a big deal in js...
0
Dec 16 '12
[deleted]
2
u/ethraax Dec 16 '12
It's like a classic OO programmer saying I can't access a private variable from outside the instance. If you are going to use private variables then you need to know what the hell private variables are and how they are scoped.
Except it's not like that at all, since you can't accidentally use private variables.
Imagine if you could have public variables and private variables of the same name on a class, but to access the private ones you had to write "private" before the access:
myobj.x = 5; // public private myobj.x = 5; // privateIf you're writing in the class and accidentally leave out "private", you're accessing a different variable. That's what the whole JavaScript
newissue is like. Just having private variables can't compare at all because you can't mistakenly access them from outside the class (at least, you're not supposed to be able to).0
Dec 16 '12
[deleted]
2
u/ethraax Dec 16 '12
No, that's not what I mean. If you never use constructors in your code, than the "new" keyword would look strange. You would immediately know upon seeing it that something is wrong. On the other hand, if you use constructors, you have to inspect the action function/constructor being called to make sure "new" is being used correctly.
Also, the overhead (in terms of developer time) for not using constructors is far higher than using a new language and having to go through a compilation step (not to mention the inferior debugging support).
2
Dec 16 '12
[removed] — view removed comment
3
u/contantofaz Dec 16 '12
Dart is said to be dynamic because it has Optional Types when types don't even apply during runtime. I've said before that Dart errs on the side of static code declaration, because everything seems to be set in stone as declared and the Dart developers themselves have been striving to kill string as JavaScript knows it with events that are method calls instead like "el.on.click.add((ev) {});"
I just did a ton of renaming of private variables using the Dart Editor refactoring tool and I can't remember ever using refactoring like that. Despite some Editor glitches (it still is under develoment and is relatively slow as it's based on Java/Eclipse), I'm 95% done with the last class to go. My code barely used any type declarations, but as Dart is static like I said, the Editor could infer a lot and even warned me when some variable was a little outside of the scope of the change. I recently learned that renaming things is the #1 refactoring need of some folks, maybe of most folks? And it can be done very well in Dart despite the "unsound" types based just on static code declaration.
1
Dec 16 '12
[removed] — view removed comment
1
u/contantofaz Dec 16 '12 edited Dec 16 '12
People say that the Closure tools are under-advertised. I recall about one year ago taking a look at their mailing list and it was a little too quiet there. Then again, the Dart mailing list seems to be very quiet this December. Maybe it's a coincidence, or maybe Microsoft's TypeScript announcement deflated the hype of Dart.
What I think is an important difference between Dart and Closure toolkit is that in Dart we don't have to code defensively for performance as much as can get used to in JavaScript. I'm still recovering from trying to be the optimizer for the JavaScript code rather than letting the tools deal with it for me.
Not sure the Closure tools can fix the code for you the way the bigger translation can from Dart. Then again, if you're coding to extract the max performance possible out of your JavaScript, then maybe you can take your luck in your own hands too.
I was getting addicted to running the JavaScript profilers. I was wondering how to run the profiler for Dart code still. Once the Dart code hits JavaScript, we have to trust that the code will do a good job on its own as it's then too late to change it much.
Maybe the Closure tools also try to optimize your code for you so you don't need to pull your hair out for every line of code? I think most JavaScript programmers get too used to writing optimum code. It's good to forget to do that when coding in Dart.
1
1
u/bonch Dec 17 '12
nothing wrong with the language itself
Nothing at all is wrong with it? Seriously?
1
u/bonch Dec 17 '12
The only real problem with JavaScript in its current form is the lack of namespacing and module system
There's way more wrong with JavaScript than that.
0
Dec 16 '12
[deleted]
1
Dec 16 '12
I've been using typescript, and it solves most of my issues with JS. Howver it's far from perfect.
At times you can end up in a half world, where some code is well typed, some is built untyped and gets its type via massive casts, and some is just not typed as the type inference doesn't go far enough. You do also end up working in a small subset of typescript, even though it's a superset of JS (although this isn't so much of a bad thing).
82
u/ilikecomputahs Dec 15 '12
Why is object oriented programming considered so absolutely necessary?
/braces for shitstorm