Sunstorm Labs Blog

April 29, 2008

I hate Javascript, but I love JQuery

Filed under: Web — Tags: , , — admin @ 10:34 pm

When it comes to the web, I prefer to stay on the server, where it’s safe. I love the stability of the environment I get in server side code, and I detest the client side chaos that goes on in the faggotry of browser wars and their incompatibilities and differences.

If I can, I try to avoid scripting completely. In the current state of browser affairs, I don’t feel like I can rely on Javascript for anything more than decoration and minor UI enhancements. When I do have to work with Javascript (and by that I mean any client side scripting language), I do it in a very conservative, “1.0″ way.

This post explains my feelings well, and even goes as far as to propose the term “JUE”, for “Javascript Usability Enhancement”:

AJAX [Javascript on the whole], should only be implemented after the page has complete functionality, and should only be used to enhance the usability of the webpage. Every function which is possible through the AJAX, should also be possible in the same or similar way without the AJAX (but usually with an extra page-load or two).

This isn’t just about usabilty, unobtrusiveness, or separation of code though. There are plenty of advantages on starting from the server side and working your way up. Some of my favorites are…

  • Security: You can’t rely on client side validation because it’s so easy to by pass that even my computer illiterate mother accidentally does it on occasion. HTTP is an open protocol; it’s stupid to think that well behaved, Javascript-running browsers are the only thing that can get my pages. 
  • Compatibility: Static HTML makes very few assumptions about the environment it’s in. It just tells the client of the information it wants to pass along, and lets the client figure out how it wants to render that. This makes it the lowest common denominator for the Web. Javascript, on the other hand, makes a lot of assumptions. Any scripting will only work well in the environment it was written for, while static HTML has a much greater reach.
  • Transparency: Static HTML is the closest thing to the true model of the web, while scripting is a higher level layer over it. Focusing primarily on scripting makes it that much easier to miss the right way to do things under the web model, especially for someone for the people that don’t know what they’re doing.
  • Purpose: Javascript is designed to manipulate bits of document. The fact that it grew into into a full blown UI framework is a mind boggling technological abomination. If you realy want to make a rich, web-based application, why not use something designed for rich UIs, like Flash or Silverlight?
  • Performance: Despite all its recent advances, browser scripting performance still doesn’t hold a candle to desktop applications in terms of speed and responsiveness. Static HTML is still what browsers were designed for, and the features that it supports will always have the best performance. While a static HTML interface might not be as feature rich as a scripted one, its endurance shows that it’s good enough to be efficient. Being used to the efficiency of static interfaces, the added weight of all of the “rich” interfaces makes for an uncomfortably noticeable break in flow.

But despite all that, scripting does help address the very real, and always increasing need for rich interfaces on the web. If used right, scripting can have great results for a lot of people.

The real reason why I don’t like client side scripting is because Javascript, and the way it’s designed, doesn’t make it easy to do anything worthwhile. Take the fact that it’s modeled as a low level interface for document manipulation, add the problems that come from browser inconsistencies, top it off with poor debugging and IDE support, and you got yourself a clusterfuck that can be only beaten by the current state of CSS. Every time I pick up Javascript, it’s a productivity struggle of monumental proportions.

Javascript is like the C++ of web programming: very flexible, but too low level to be adequately productive. So the solution is not to use it at its lowest level, just like I don’t use Win32 in my apps when I want to get things done. It turns out that there are tons of great libraries that already implement all of those things that you would have to do over and over again in Javascript.

I suppose I should have thought of it before, that maybe a million people have already done this a million times before, and maybe some of those people put it together in a way that takes away the need for me to do for the millionth-first time. I guess I just never saw Javascript as a serious enough language to make “libraries” for. Back when I started, Javascript “libraries” were the things you copypaste from Javascripts.com to make gay rainbows on your text.

The problem is, every time I tried to research Javascript or AJAX, I always find tons of resources, merrily spewing forth explanations on how to use document.getElementById(), make XMLHttpRequests, or animate things using setTimeout and setInterval, and enumerating the myriads of caveats and their workarounds between browsers. Not once I’ve seen a site stop, and tell me “Hey, you! You’re doing stuff that’s already been done a million times! Why not just use a library that handles all of this for you?”

Point in case, I was recently asked to do a small widget that cycles images, “like the thing on Yahoo Movies. After several hours of browsing sites that explained in great detail all the pages of code I would have to do, and tons of custom implementations of said code that did almost kinda what I wanted, I came across JQuery. And it felt like God almighty came down in an angelic choir, and slapped the Truth into my face with a tire iron. I was enlightened.

JQuery delivered an excelent first impression. Having had no previous experience with it whatsoever, it took me 10 minutes, and a grand total of two lines of code to make my animated scroller. It already had everything done for me, starting from a CSS-like element finder syntax, to automatic tweening of any value. I don’t usually enjoy adding non-standard, external libraries to my code, but this one of those things that really should be standard, and only isn’t because all the other JS toolkits would cry their eyes out.

It seems Javascript coders suffer a lot from the “Not Invented Here” syndrome. Maybe it’s because you don’t need anything but a browser to write Javascript. Maybe watching your page come to life after all of that hard work is very satisfying. Maybe they’re afraid that if someone looks at their code and sees them use a library, they won’t seem as hardcore. Or maybe someone just needs to clean up the internet of all of the feces that the last generation of Javascripters left behind.

I, however, don’t have the patience for it. From now on, I’ll just be dropping JQuery into all of my projects.

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress