JavaScript Best Practices

February 10, 2012

I recently came across two good resources that discuss some best practices when it comes to JavaScript. The first is an article at Developer Drive, titled Top 10 “Must Follow” JavaScript Best Practices, written by Salman Siddiqui (it’s a good enough article that I’ll ignore the dubious use of quotation marks). It’s a pretty good list, with a range of beginner to advanced topics. I would disagree with one, however: “Better to avoid DOM.” While DOM manipulation is expensive, it’s often required, so I think it’s better to acknowledge that this is a necessary evil we’re stuck with and hope that browsers continue to make improvements in this area. I liken it to the common economic theory that getting a loan to buy a car makes poor monetary sense (because it’s a loan on something that depreciates). The fact is most people need a car to work and make money, so a car loan is often necessary. But I digresss…

The second resource is a presentation titled JavaScript “Best Practices” (again, the quotation marks), by Christian Heilmann. Heilmann presents about 20 specific suggestions and then explains them all in detail. The presentation itself lacks pretty much any style whatsoever, but content is more important, no? There’s lots of code in the presentation precisely demonstrating both good and bad behaviors, along with explanations as to why something is problematic.

I’m in the process of writing my own article, titled “The 10 Best JavaScript Development and Design Habits”, to be published at Peachpit.com. I’ll post a link to that when it goes online.