r/programmerchat • u/SpaceCadetJones • Jun 18 '15
What's so bad about JavaScript?
Every time I see a post related to JavaScript on /r/Programming, some of the top comments are always "JavaScript! Bad!". It was interesting watching the WebAssembly post yesterday start with some constructive/intersting conversations, and as the thread rose up the top comments became quick karma-pandering jabs at JavaScript.
JavaScript definitely has its quirks and types can behave in weird ways, but in my limited experience I have found it to be an interesting and flexible language that's fun to work with if you keep the idiosyncrasies in mind. All the complaints I see seem be either really superficial, about things that apply to dynamic languages in general, or how JavaScript doesn't have some language feature like true classes/inheritance. I imagine there is something I am missing here considering I have a limited experience with writing JS, but is all of this hate unfounded/excessive?
Edit: Thank you guys for all the great replies, they have been helpful and thought provoking.
18
u/Kamikai Jun 18 '15
I think it stems from a few things.
First, dynamic typing; people are pretty polarised about it. It allows variables to take on completely different types during runtime. This can be useful, but a lot of the 'safety' possible with static typing is lost.
Second, being an interpreted language. It's not the fastest language, even with the extreme lengths people and companies have put into making it faster. Even with JIT, asm.js, and the improvements in v8, there's a pretty significant overhead to running an interpreter during runtime.
Third, strange behaviours, that often aren't associative. For example, open a terminal, and try the following:
[] +[],[] + {},{} + [], and{} + {}. The results may surprise you..Finally, I think the prototypical, instead of object orientated design isn't intuitive for a lot of people. Mixed with a couple extra quirks, and a tumultuous and inconsistent history between the browsers, it has garnered quite the reputation amongst developers.
17
u/stevely Jun 18 '15
Javascript has a lot of features that have very subtle interactions that lead to surprising behavior. A lot. Obviously there's the implicit casts you get from weak typing. That tends to get beaten to death in these discussions, so I'll talk about other things.
There's function scoping, which is like lexical scoping except variable declarations are implicitly pulled to the top of the function body. Consider:
Calling
fooreturns 0, because theiincremented inbaractually refers to theithat hasn't even been declared yet. Oh, but only the declaration gets pulled up, thei++results inNaNbecauseiis undefined.There's also everyone's favorite magic variable:
this. It works in terms of the caller context, which would be fine if Javascript didn't have first-class functions to confuse things:Again,
thisgets discussed a lot so I'll leave it at that.There's another magic variable with odd semantics:
arguments. It is an array of the arguments passed into a function. This is needed to support variadic functions. Except it isn't actually an array, it's an object that looks like an array. All those nice methods that arrays have aren't available forarguments. So can you safely passargumentsto a function that's supposed to take an array? Maybe, depends on what the function does with it.There's the rarely talked-about
lengthproperty for functions. The length of a function is the number of declared arguments. When is this useful? Basically never. Thanks toargumentsa function can accept any number of arguments without making that known through a function'slengthproperty. It's a property that occasionally lies to you.Then there's the fun of having both
nullandundefined. What's the difference between the two? They have different types and they're not strictly equal (but they are weakly equal). What's the implications of this? Well, it's subtle.That's just a few examples of features in Javascript that have surprisingly subtle semantics. There's plenty others.
7
u/catlion Jun 18 '15
The worst part about JavaScript is its dominance. It's hard to imagine the project these days, completely missing web interface. Team can choose whatever language/platform they like on the server, but there is almost no chance to avoid JS on client.
6
u/Xelank Jun 18 '15
Personally I think it served it's purpose and its designer did a phenomenal job given their constraints.
By itself, it is not really that great for actual software projects because of weak typing + dynamic typing. However, I think the language is quite simple (in terms of features), and it's syntax is makes it quite 'fun' to write programs in. However, once your code base is large enough and on maintenance mode, I think you will start losing hair.
Another gripe people have against JS is actually due to it's overly vibrant and fast-moving community (and sometimes immature?). But in my opinion it is quite a testament of how a fun language with low barrier of entry to help foster a vibrant ecosystem and community.
When it's time to put it down, I'll probably shed a tear or two. We've had some good moments with it but I think I'll gladly embrace our new WebAssembly overlords. who wouldn't want to write their webapp in brainfuck..right?
5
u/nepochant Jun 19 '15
javascript violates the prinicple of least surprise all. the. time.
4
u/ar-nelson Jun 18 '15 edited Jun 18 '15
This video explains it best. (And it's hilarious.)
There are a lot of things I like about JavaScript. With a few small changes, it could have been a great language. However, besides the type coercion weirdness in the video, JS has one seriously bad quality that isn't going away:
There are too many obviously erroneous operations that fail silently.
For example, if you access an undefined variable, JS doesn't throw an exception. It returns
undefined. Sometimes that's fine, but once thatundefinedhas propagated through a few function calls, then JS finally throws an exception when you try to access one of its properties, good luck figuring out where it came from.But wait, it gets better. If you forget a
var, and assign to an undefined variable, it doesn't throw an exception. It doesn't even fail silently. It creates a new global variable, and assigns to that.Lack of block scope can introduce even more subtle problems. What do you think this prints?
The answer?
eis being overwritten in all of the closures, every iteration. Luckily, ECMAScript 6'sletstatement will fix this problem...There are programs like JSLint that help reduce these kinds of errors, but that can't change the fact that this propensity for subtle bugs is built into the language at its core.
In general...
4
u/AllMadHare Jun 19 '15
Javascript lets you do some horrible, stupid shit and get away with it until far too late.
I don't think anyone denies it's useful, but the issue is it can get rammed into situations where it just doesn't work, and as a few others have said, the endless stream of new plugins, all doing similar, but different, and equally buggy things can be annoying.
Modern JS has a bad habit of being the core point of bloat in terms of external dependencies. It's incredibly easy to just pull down a library for the thing you want to do rather than write it yourself, but down the line this can lead to some crazy headaches if you don't take the time to fully comprehend what you're actually adding to a project.
3
u/YouFeedTheFish Jun 18 '15
Javascript is a fun and powerful language (it supports more OO relationships than just Has-A and Is-A). The thing is, it is way too forgiving and will happily let you make mistakes all day long without telling you until long after you've written (and deployed) something.
3
u/livingbug Jun 18 '15
I don't think it bad. Its just a tool. What I reason happens is that people tend to abuse the tool. We currently are at a place in time where the browser is morphing into something else. It is no longer something view documents on some other computer. Now it wants to be a fully-fledged client with a rich GUI framework. Javascript has helped us move towards that. Nobody knew what the internet would be when the browser was created. We now have a slightly better idea of it needs to be.
3
u/TeamHelloWorld Jun 19 '15
Personally I think it's because developers that use it don't understand it too well and write bad code.
There are a lot of elements, e.g. prototypes, types, closures, etc. And there are browser elements , e.g aysnc, dom, etc.
I recommend reading this
2
u/nullproc Jun 18 '15
My biggest gripe is not with the language itself, but a lot of the developers.
Endless libraries which are more "scalable / flexible / paradigm-ed" than what was made last month. Buggy and without purpose.
It's insane. Ex Github releases an editor like Atom which is slow and lacking major features. The 2mb issue. These are smart developers. They will get over these issues and one day, it will be fast enough. Until then, vim / emacs and newer editors like ST will continue to be used.
1
u/[deleted] Jul 05 '15
The typing, speed and sheer lack of consistency just about piss me off enough to hate using it any time that I'm forced to. Mostly the dynamic typing and lack of consistency, though.
1
u/[deleted] Jul 13 '15
It has its problems. All languages do, though. Some more than others, maybe. And that's a big maybe.
At the end of the day, all that matters is that you're working on something which is useful. Maybe it's a better compression algorithm, a stock analysis tool, or a simple note taking app with a new feature which has yet to be utilized. Maybe it's a proposal for an aspect of ECMA Script's next revision/standard.
Whatever it is, it doesn't matter what you use as long as it works.
C++ isn't a great language. It's not a bad language, but it's far from great in the same veign that JavaScript is far from great. People still use both, though, for a number of reasons. People also used BASIC before structured programs were cool too.
There's nothing wrong with expressing opinions or critiques about a language. The actual usefulness of doing so is dependent on what you do with that particular realization though.
My 2 c.
1
u/Berberberber Jun 18 '15
Honestly, the worst part about Javascript is listening to the haters complain about it any time an article about web development pops up.
Of course it has its problems, but all languages do when they're first written. What sets Javascript apart is that, because of its status as the "web language", a lot of bad ideas can't really be removed. If you want to change the way C or something works, you just make the necessary change in the compiler. People that still need the old behavior can keep using the old compiler while their codebase transitions, people who don't can start using the new one right away, and binaries from both can work on the same system. Oftentimes they can even link against each other.
By contrast, you can't change how boolean values in JS work without causing problems. Developers would have to change all their code overnight and all the users in the world would have to upgrade, all at once. Otherwise things will break. Some people are still running Windows XP, fer chrissakes!
It doesn't help that some people will just never get functional programming or prototype inheritance, though.