JavaScript is not your enemy

This post was updated on 2012-07-10 to make the tone of the article consistent throughout.

Too often I hear developers saying that they hate JavaScript. They’ll claim it’s a terrible language that necessitates bad code, and that developing anything complex in it is an endless battle of workarounds and hacks.

This attitude isn’t entirely unwarranted - but it isn’t constructive either. Worse still, by taking this attitude you run the risk of becoming part of the problem. A big issue, in my experience, is that developers will write JavaScript code without really understanding it. This leads to misunderstanding and abuse of the language, which causes the cycle to repeat - reinforcing the negative perception of the language.

If this sounds like you (or someone you know), then I implore you to let go of your preconceptions. You’ll discover that JavaScript isn’t so different from other languages you might use. Yes, it has its quirks - but it isn’t hard to write high quality, maintainable code. There are even some nice language features which lend themselves to simple and elegant code.

If your project is suffering from low quality JavaScript, the solution is often as simple as making it a first class citizen of your project. Give it the same treatment that you would give to the other languages in your technology stack. Coding standards, style guides, code reviews and unit tests are all practices which can be applied to JavaScript.

Building complex applications in JavaScript continues to get easier and more rewarding: there is now a huge community ecosystem which is producing excellent JavaScript libraries in volumes. There are too many good libraries to name them all, but these are some excellent starting points.

If you embrace JavaScript rather than reject it, you can (and will) develop a rhythm of producing high quality code. As a seasoned JavaScripter, I can provide a few hints that will help you on your way to achieving this:

  • Learn the language! It may look like [insert other language]. It isn’t.
  • Incorporate a JavaScript component in to your build scripts. Your JavaScript source code should be structured and organised - let your build script do the work of combining and optimising that code for delivery over the web.
  • Use a code checking tool like JSHint. This will help you to avoid the quirky parts of the language.
  • Resist the urge to treat your HTML and JavaScript as a single unit. This will make your code much more difficult to maintain. It also encourages some bad JavaScript practices. A better approach is to build your web application with Unobtrusive JavaScript.

For some more perspective on what’s good and bad in JavaScript, I highly recommend Douglas Crockford’s JavaScriptThe Good Parts.

Until next time.