Jump to content

A (half-hearted) defense of JavaScript The JavaScript language has been mentioned in passing h...


G+_Lee Crocker
 Share

Recommended Posts

 

A (half-hearted) defense of JavaScript

 

The JavaScript language has been mentioned in passing here a few times, but I think its popularity and importance merit more detail.

 

JavaScript is an extremely powerful and flexible language with a few spectacularly bad features that give the language as a whole a bad rap. First among these is the name, which implies that it has some relationship with Java, with which it has absolutely nothing in common but some basic syntax that both languages got from C. Second is the fact that is has a global object that users can manipulate, causing modules to interfere with each other. Worse, the keyword "this" refers to user objects, newly created objects, or the global object depending on context, causing programmers to stomp on the global object unintentionally without warning. JavaScript's == and != do implicit type conversion with strange results, so they have to have === and !== to behave more sanely. Likewise, there are two "bottom" values: null and undefined, with different behavior. Finally, all values have true/false evaluations, but some are bizarre. For instance, Python programmers used to checking for empty arrays with "if myList:" will be surprised to discover that empty arrays are true in JavaScript, though empty strings are false as normal. Variables are function scoped rather than block scoped as most would expect from a language with C's block syntax, and are untyped and declared implicity so such errors are hard to find. Stack-frame based exceptions don't fit well with the rest of the language.

 

Despite these flaws and others for which the language suffers well-deserved ridicule, other parts of the language are quite elegant and powerful, and the language can be implemented quite efficiently, as demonstrated by Google's "V8" engine in Chrome (and used by Node.js and others). JavaScript's object system is prototype-based like Self and Lua, rather than class-based like more traditional OO languages. I personally think this is better, though enough people are so used to Java's way of doing things that they are trying to patch a class-like syntax onto the new version of the language that doesn't really fit it well. JavaScript has first-class function objects with full closure. It has an extremely simple and powerful object-literal notation similar to Python's that is so good it is often used to encode data by programs in other languages (where it is called "JSON"). These features fit together remarkably well, allowing some very powerful programming patterns that even Lisp programmers might envy.

 

If one takes great care to avoid the bad parts (perhaps helped by tools like JSLint), and one learns how to make the best of the good features, one can make (and people have made) quite large and elegant programs.

 

In short, JavaScript makes it very easy to do very, very bad things. But also fairly easy to do good things. Its expressive power and orthogonality and flexibility compare well with many other popular languages.

 

Because JavaScript is open, there are many implementations, not just in browsers but for standalone programming as well (I think Node.js is deservedly the most popular of these). Because of its flaws and oddities, there are also languages that modify, add to, or compile down to JavaScript while presenting what they think is a better programming experience. TypeScript, for example, adds a lot of type-checking to the otherwise typeless language, which makes finding certain types of bugs easier at the expense of some flexibility. CoffeeScript lets you program with a more Python-like syntax including things like list comprehensions and string interpolation. You can get compilers that translate languages like Python and Go directly to JavaScript. But frankly, I think if you're going to write code for a JavaScript target, learn the language as it is, warts and and all, and learn to use the good stuff to do it well.

Link to comment
Share on other sites

Stronger type checking, especially of function signatures, would certainly help find some bugs (though not, I think, the kind I tend to make most--maybe that's just a quirk of my brain). But I'm not fond of class hierarchies and constructors, which so many OO languages have and that constrain the expressiveness of the language with no benefit. I know that's how Java geeks learned that OO is supposed to work, but I think prototype inheritance more closely models the human brain: we think in prototypes, not classes. I love interfaces, though I prefer Go's style of never declaring implementation--the compiler can figure that out. And it's weird that TS adds generics--hmm, let's take a typeless language, strongly type it, and then add a way make almost-typeless functions again. If you have a base type like "Object", and interfaces, what are generics needed for?

 

Improving JavaScript is a noble goal. But if you're just going to turn it into Java, why bother? Write a Java compiler that produces JS.

Link to comment
Share on other sites

I've been using Javascript heavily over the past year. Not for web, but with a service called Appcelerator. You write your 'app' in Javascript and then get the Android and iOS builds, it also does Windows Phone and BlackBerry but I've never tried those - I wouldn't say it's 100% native code, but you get Java or Objective-C code after building, it just has a lot more helper functions and classes than it would if you did things natively. It does a pretty decent job, obviously it isn't the best solution but when you're a sole developer, needing to work on 2 apps at a time AND they're 'side projects' it's a lot faster for me to handle 1 code base than deal with Objective C (or Swift) and Java. It gets a bad rap, but you really just have to know how to handle memory youself and know when to open/close windows or use views, etc... but I digress.

 

I've really enjoyed using JS and have already started using it a lot more in my web development (used to just find what I needed because I don't have original ideas... Somebody has already always done what I needed to do ). It's a great language to learn, but I sometimes hate how loose it is. ?

 

Edit: I haven't watched the recording yet, but I think I tuned in yesterday to catch the tail end of Lou talking about these new cross platform development tools? Patrick's lens flare blinded me and I forgot what I saw before that...

Link to comment
Share on other sites

  • 5 weeks later...

FYI: As it turns out, Microsoft seems to have made good on most of its promises. VS 2015 Ultimate Preview compiled C code with designated initializers, compound literals, anonymous structs/unions, flexible array members, variadic macros, and other modern C constructs. The only thing it choked on was "restrict". So I can report that after years of not doing so, it seems that Microsoft now makes a C compiler.

Link to comment
Share on other sites

 Share

×
×
  • Create New...