By Michael Woloszynowicz

By Michael Woloszynowicz

Sunday, January 16, 2011

Why You Need to Learn JavaScript

No more than 5 years ago, back-end programmers viewed JavaScript (JS) with a great deal of disdain, seeing it as little more than a means for hacking around browser inconsistencies and achieving some basic level of interactivity. For the most part they stayed away from it and laughed at those that called themselves JS programmers. To say the tables have turned would be incorrect as the back-end remains a vital part of any web application, particularly when complex analysis is needed and scalability issues arise. What we have seen is a dramatic increase in the importance of JS to the web community. Today users expect a high degree of interaction and responsiveness from their web applications that simply cannot be achieved through simple page submissions, hence the birth of AJAX. Although AJAX was a monumental step in boosting JavaScript's importance, it didn't result in an immediate shift in sentiments towards JS. A high level of interaction was out of reach for most companies as the majority of their client-side developers lacked the programming fundamentals to develop large scale JS based tools, while back-end developers refused to venture into client-side programming.

Where we are today is very different. A great deal of the work that was once done on the server has now moved into the client side, and as the complexity of the web UI's grows, the need for individuals with a background in CS that can develop well structured and robust JS applications will grow with it. Couple this with the expansion of mobile development using tools like PhoneGap and Sencha Touch, server side frameworks like Node.js, and the growing shift of desktop applications into "the cloud", it is clear that JS is here to stay. Despite its growing relevance, many professional developers still refuse to embrace it and leave it as something to be dealt with by client side programmers. The problem with this attitude is that web developers are often not well suited for the demands of today's JS applications. Web developers are trained to design appealing and usable interfaces and transfer these designs into accessible HTML/CSS pages, not to write 5000+ line object oriented or functional* applications. What we need are individuals with a strong knowledge of design patterns, data structures, and OO or functional programming to produce code that is maintainable and robust. The best way to make this transition is to start with a framework that removes the drudgery of cross browser compatibility and activates the power of JS. For me this was the outstanding Dojo toolkit which has turned JS programming from a chore to a joy. While the learning curve for Dojo can be a bit steep, it lends itself well to server side developers as it centers around packages, classes, and all the other goodies of OO programming. With a proper toolkit in hand you'll find that JS is not the steaming pile you thought it to be, but rather another valuable programming language to be mastered. Your versatility and value as a programmer will grow, you'll find that developing highly interactive and innovative UI's is both challenging and rewarding, and you'll be well positioned to develop class leading web applications from start to finish.

Although toolkits like Dojo are excellent and necessary for creating production applications, you can't fully utilize them without first learning the fundamentals of JavaScript and the document object model. Developing with Dojo often involves studying its source code so a working knowledge of JS is needed. After all, you wouldn't want to jump into Scala without learning Java first. Study some books on JS (JavaScript: The Good Parts is a nice quick read) and practice writing some basic DOM manipulation, event handling, and object based code. Once you have a good understanding of how pointers are passed around in JavaScript, the various ways methods can be called and referenced, the way attributes can be accessed, how scope works (closures), and how objects can be encapsulated, you should then begin reading some of the Dojo source code. I recommend studying the dojo.data.read API (particularly the ItemFileReadStore) first as it is very useful, well documented, and exemplifies the power of JS and the techniques you can use within your own applications. Once you've become comfortable with JS, dive further into Dojo (see my previous post on Lessons with Dojo) and enjoy writing JS applications, just as you would any other languages. JS is no longer an option for true programmers, it's a requirement...and it's not the nightmare you think it is.

Note: Although I've focused largely on Dojo in the last part of this post, I'm not saying that you should ignore other toolkits like jQuery. jQuery is a great framework in its own right, but given that I have geared this post towards server side programmers I've chosen Dojo as it's syntactically familiar and heavily object driven, and hence more accessible this group of users. 

* A thanks to HN poster gibsonf1 for pointing out my omission of functional programming. Whether you are a server side functional or object oriented programmer is irrelevant. Functional frameworks like backbone.js are great if you have a lot of experience with LISP, Clojure, Haskell or any other functional language. The important thing is to pick a framework that matches your style of server side programming and apply the same rigor towards writing great JS code. 

If you liked this post please follow me on Twitter for more.

5 comments:

  1. Thanks for the wonderful post.

    ReplyDelete
  2. This comment has been removed by a blog administrator.

    ReplyDelete
  3. You dont need to learn the DOM to write JavaScript applications. It is needed only if you want to write client side applications, not server side. Just think about Node.js or Rhino for writing server side application in JS.

    ReplyDelete
  4. Thin Server Architecture will wipe out server side components. There will no untestable messy mix of server side UI components, HTML and Javascript. Using frameworks like JavascriptMVC, there is better separation of concerns. Work can be devided between app programmer and web designer. Code is also better suited for testing.

    ReplyDelete
  5. Thanks for the feedback, and thanks for the correction on the typo keemor.

    Poetro, I agree that you don't need to learn DOM for pure JS coding but the likelihood is that you will need to manipulate the UI to some extent when developing the client side of web-apps. It depends on what type of programming you’ll be doing.

    ReplyDelete