Monday, September 17th, 2007

Douglas Crockford’s Elements of JavaScript Style

Category: Articles, JavaScript

Douglas Crockford micro-blogged it best:

I have been at Yahoo for two years. One of the first things I did when I got there was to do a View Source of the front page. That gave me a lot of visibility into how things were done. I also saw some things to improve. That inspired me to write the first two chapters of The Elements of JavaScript Style. Part One. Part Two.

He explains through example, the following:

  • Avoid archaic constructions.
  • Always use blocks in structured statements.
  • Avoid assignment expressions.
  • Use object augmentation.
  • Use common libraries.
  • Watch out for type coercion when using ==.
  • Use the ?: operator to select one of two values.
  • Never use implicit global variables.
  • Do not use the ?: operator to select one of two actions.
  • Use the || operator to specify a default value.
  • Global variables are evil.
  • Use inner functions to avoid global variables.

What’s your style?

Posted by Dion Almaer at 7:54 am
17 Comments

+++--
3.8 rating from 37 votes

17 Comments »

Comments feed TrackBack URI

Nice. If more people had a better reference for writing well styled javascript code it might help eliminate some of the perceptions that js isn’t a real language.

Comment by Jesse Kuhnert — September 17, 2007

Is Crockford the leading authority on JS? He seems to be one of the people who care most about it and coming up with standardized usage, compatibility, etc.

Aside from Dean Edwards, who else is putting out JS stuff like this?

Comment by emehrkay — September 17, 2007

@emehrkay
Don’t know if anyone is the leading authority on style in any language but the Dojo style guide helped clear up a lot of questions for me a long time ago.

http://dojotoolkit.org/developer/StyleGuide

Comment by Jesse Kuhnert — September 17, 2007

100 % fit… not one thing I would question, and I use all of it without even thinking about it everyday. This would be my top ten hint list also.
Maybe I would add proper singleton and class code (especially private/public, closures) and namespaces to it. But thats it.

Comment by Frank Thuerigen — September 17, 2007

A great list! As an imperative, compiled language coder still quite new to javascript, this is extremely useful.

Comment by Ben Bodien — September 17, 2007

+1 for the Dojo style guide. It is the closest thing we have to an “official” JS style guide.

Comment by Dean Edwards — September 17, 2007

Just as a head’s up, Doug wrote these in 2005–they are not new at all.

Comment by Tom Trenka — September 17, 2007

Oh, and thanks for the vote of confidence on the Dojo Style Guide :)

Comment by Tom Trenka — September 17, 2007

Here is another one I would add to the list:
Frameworks/libraries shouldn’t add new properties to Object.prototype or Array.prototype. This forces developers that use for in loops to have to check each property with hasOwnProperty. Some may say this is a good practice, but something forces developers to use a basic construct of the language in a different way should not be thrust upon developers. And json.js breaks this principal…

Comment by Kris Zyp — September 17, 2007

@Kris:
though I know it is disputable, I totally agree. I´d say all core JS objects are taboo anyway…

Comment by Frank Thuerigen — September 17, 2007

Great post! Yes these have been around, but well worth reiterating and emphasizing. I like the Dojo style guide as well.. also reference the Mozilla JavaScript 1.5 guide: (http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide).

Comment by Mark Holton — September 17, 2007

@emehrkay: from my readings, in addition to Dean Edwards, I’d say other Crockford-like postings come from people like John Resig and Alex Russell Obviously there are other ‘titans’ of JS (Brendan Eich, Sam Stephenson, Joe Hewitt, etc), but John and Alex address more coding specific items and they are very active in posting on their blogs, and John is often posting about coding specifics in JS. Often times it relates to John’s library, jQuery, but also delves into other fundamental items with JS. http://ejohn.org/blog/ || http://alex.dojotoolkit.org/ …my two cents

Comment by Mark Holton — September 17, 2007

Mark thanks for the heads up and thanks for the dojo guidelines. They are great and i’m sure you could follow them in other languages as well

Comment by emehrkay — September 17, 2007

I would sure like to see some of his explanations as to “why”. Assertions are interesting but explanations are better. Some of these seem to be arbitrary.

Comment by Nate Grover — September 17, 2007

Nate, it would help if you would identify which ones seem arbitary. Shit, I might even be able to explain why some of these things are “rules.”

Comment by emehrkay — September 17, 2007

If you’re interested in the fortune cookie file to use in your email signatures, it’s here: http://bluesmoon.info/tech/javascript-style.tar.bz2

There’s also the elements of programming style done many years ago: http://db.ilug-bom.org.in/lug-authors/philip/misc/fortune-mod-prog-style.tar.gz

Comment by Philip Tellis — September 18, 2007

A JS coding priniciple I strongly endorse – make variable and function names meaningful. It takes more space, but man does it make it easier to follow code. Taking a look through Douglas Crawford’s examples was painful with the one and two letter variable names. If you are really interested in compact code, start big and run a compactor later.

Comment by Nathan Derksen — September 22, 2007

Leave a comment

You must be logged in to post a comment.