Douglas Crockford Answers DZone's Questions on JavaScript

DZone 's Guide to

Douglas Crockford Answers DZone's Questions on JavaScript

· Web Dev Zone ·
Free Resource

CoffeeScript, node.js, ECMAScript, and even Google's new Dart usurper language were topics of discussion last week when I had the unique opportunity to meet one of the major luminaries in the world of JavaScript while he was speaking at North Carolina State University.  The speaker was Douglas Crockford, and his talk was called "Programming and Your Brain."

For those of you who don't know of Crockford, he has a long list of accomplishments in multiple industries, but his most recent ones include becoming the Chief JavaScript Architect at Yahoo!, being the author of the definitve JavaScript book: "JavaScript: The Good Parts", and discovering JSON.

Programming and Your Brain

The talk was inspired by the book "The Science of Fear", which hypothosizes that the brain, in simple terms, can be divided into two parts: the "Head" and the "Gut".  The Head is where our logic and rational analysis comes from, and the Gut is supposedly evolved from our 'fight or flight' response, which means it's very fast, and can often overpower our more rational Head.  This is how, for example, advertising can tend to work against our better judgement.

What does this have to do with programming?  Well, if you look at the story of how JavaScript was created, you begin to understand.  Brendan Eich, while working at Netscape, wrote JavaScript in just 10 days in order to meet the demands of the business.  While anyone who's ever used JavaScript knows that this speedy development undoubtedly led to a lot of nasty problems, it also led to some great insights.

How we program with the help of our "gut"

Crockford believes it's that "Gut" part of our brain that allows us to not only make programming possible, but also to make great leaps of insight.  Eich's language had a mountain of problems, but Crockford also believes that JavaScript was in many ways a more highly-evolved language than we'd ever seen in programming.  Here were some of the things that Crockford believes has kept JavaScript on throne as the language of the browser:

  1. Functions are first-class objects in JavaScript
  2. It brought Lambdas into the mainstream of programming
  3. Classes became unneccesary in JavaScript

"Programs are one of the most (if not the most) complex things that humans make," said Crockford.  "Programs need to be perfect, but humans are not good at perfect."  Crockford believes that if humans had no "Gut", they'd never release software, because software (along with the languages that it is built on) can never be perfect. 

Tips for JavaScript and any programming

From here, the talk moved into specific issues with JavaScript, and how to minimize their effect on your programs:

  • In JavaScript, there are less errors when you put your curly braces on the right.  For other languages, WHO CARES!  Just be consistent!  (another key theme in the talk)
  • Always use ===
  • Avoid multi-line string literals
  • Global variables are evil



Crockford also encouraged programmers to use a tool he created called JSLint, which highlights sections of your JavaScript code that could be problematic or buggy.  This helps keep the code maintainable in the long term and keeps it easy to read.  It's a humbling tool according to Crockford.  In fact he used JSLint on the JSLint code and it came up with errors, so its even helpful for experts like Crockford. 

"It will make you cry," Crockford said in reference to JSLint.  "I've literally seen people cry after using it."

In sum, this is a major theme that you see in Crockford's famous book: "JavaScript: The Good Parts".  The goal is to use the best parts of a language, avoid confusion for those who will have to read your code, and in general just do things that move the odds in your favor for building a well-running program.


Douglas Crockford: The Good Parts

Now to the part where he answered some of the audience's questions, and mine.

The big question on a lot of people's mind was: What do you think of languages like CoffeeScript and ClojureScript that strip away the bad parts of JavaScript and then compile to JS?

Crockford's answer was mixed - he definitely likes it a lot.  It's very elegant and he wishes that JavaScript was more like it in many ways.  In fact, the ECMAScript steering committee is actually trying to make JavaScript more like CoffeeScript.  However, in keeping with the theme of the talk, Crockford said that like all languages, there are good parts and bad parts to CoffeeScript.  Right now, the language isn't stable and all of your code could become useless if another major change comes through.  So he currently does not recommend CoffeeScript for production.

Next, we got his opinion on node.js - which he thinks is "brilliant."  He said that Yahoo's YUI 3 library now runs on node.js. It gives you a lot more options for where to run things (server or client), so that you have the best performance, whether it's Chrome 13 or IE7. 

Crockford also says that he'd like to see more cooperation and integration between the efforts of the W3C and the ECMA steering committees.  Unfortnunately, the two sides have had one meeting in their history and it didn't go well.  The ECMA committee is willing, but the W3C doesn't see the need to synchronize.  Crockford believes that the browser is a horrible model, mostly because of the W3C.

Here were the questions from me that he answered:

ECMAScript.next/Next version of JavaScript

DZone: We're in the early stages of a new revision for ECMAScript called ECMAScript.next.  What has your involvement in this revision been in this standard revision and what are a few of the ideas that you are excited about?  What's the latest you've heard about its progress?

Crockford: I represent Yahoo! at ECMA TC39, the committee that is the steward of the ECMAScript standard. There is a lot of interesting stuff proposed, but I am not certain what will make it into the next edition.

I hope it will contain a module system that will allow units of code to have good interfaces. Currently, all global variables get tossed together, which is bad for reliability and security. I am also looking forward to having proper tail calls. This will allow use of amazing programming patterns in JavaScript like Continuation Passing Style.

Crockford's New JS Benchmark and IE10

DZone: I think it was in April when you released a new JS benchmark (JSMeter?).  Could you tell me a little about the benchmark (perhaps in comparison to the incumbent SunSpider) and why the IE10 preview ended up being the fastest?  Is Microsoft finally on the right track with IE?

Crockford: Most of the JavaScript benchmarks do not predict real program performance. I created the JSLint benchmark by measuring the time it takes JSLint to analyze itself. V8 was initially slowest on this benchmark, but they quickly corrected it. JSLint helped show that tuning to benchmarks does not necessarily help real performance.

I am impressed with what Microsoft has done with IE10. I hope it quickly replaces all of the earlier, awful editions of IE.

Cloud/Web IDEs 

DZone: Any thoughts about "Cloud" or web-based IDEs, like Mozilla's Skywriter (formerly Bespin?)

JavaScript has gotten fast enough that it can be used to replace traditional applications. This means that the set of applications that can be delivered by the web is still expanding.

The most interesting feature of the so-called HTML5 collection is the Canvas, which allows us to abandon HTML entirely, avoiding the DOM and building our own displays out of pixels. This demonstrates how misfeatured the DOM is. The DOM needs to be replaced. It is slow, poorly specified, inconvenient, and insecure.

Dart as a replacement for JavaScript?

DZone: Finally, what did you think about the news of Google's new "Dart" language to replace JS?

Crockford: Google has not shared any information with me. Of the little bits they have leaked, some of it looks good. Some of it looks very, very bad. It is too early to draw any conclusions.

Overall, a great talk and some interesting opinions from one of the world's leaders in JavaScript programming.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}