"Scripting Language" is not a Four Letter Word
Sunday, June 6, 2010 at 05:46PM I can admit when I am short sighted and my initial reaction years ago to JavaScript was a little dismissive. Sure it could do some neat little tricks, but to my Math major mind, it lacked the shear number crunching and algorithmic powress of "real" languages like C. Over the past 12 years since I first saw JavaScript (I was only a little late to the game, it was invented in 1995), I've come around. Today I am going to tell you why I had that first unguided reaction, a little about the history of JavaScript since those days and why I think that what makes JavaScript different from compiled languages is exactly why you need to add some real control of this language to your programmer tool box.
So, Why My Reaction and a Little JavaScript History:
First off, as a programmer I believe in the power of names. Call a variable something stupid and your code becomes infinitely more difficult to read and understand. Through misunderstanding bugs are harder to see and errors are easier to make. So, maybe I can blame my reaction to JavaScript partially by its name. So, the script part of it is right, it is a scripting language, but the Java part is misleading. The only thing JavaScript has in common with Java is the same thing both have in common with C; slight syntax similarities. Originally, it had been named LiveScript. What a great name! LiveScript implies a little more a what we use it for now. But, back in the day it was created by Netscape (specifically Brendan Eich) to make Navagator's new Java support more accessible to non-programmers. Wait, there goes my bias again. Netscape wanted to allow an easier way to tie into page elements (like images/forms/frames/etc) without a bytecode compiler. Because seriously, as much fun as manipulating a bytecode compiler is, it does take time. JavaScript offers a way to harness events without jumping through a lot of hoops. I know one guy who back in the very early 90’s had to use COM object and dynamically hack into the kernal to get to those sorts of events. Needless to say, he loves JavaScript. It’s a much easier solution.
Of course, my first reaction to JavaScript was many years ago before it had become more robust. Back then it lacked simple things like exception handling. Also, it was too powerful for its own good. Try to do some simple things and your browser would crash. Then again, give me a programming language and I’ll show you a thousand examples of devs not leveraging resources properly.
Even then, I did not like that it did not force you to use propper OO techniques. It did not like that it was not compiled and I did not like that it was not type safe. Another way to say this is that I did not like how loose and flexible JavaScript is. Because it is so loose and flexible that you can really, really easily write some really, really bad code. And for whatever reason those really bad code snipets seem to be the ones that are the most quickly proliferated around the Internet and those are the ones that I had seen. Since it’s got such lose typing any object can at a whim modify any part of any other object leaving it very exposed to malware. And since it’s not compiled, it will never be as optimal as a C program could be, and any one can steal the logic you created.
But, loose and flexible doesn’t mean bad and just because you can do dumb things, doesn’t make the language dumb. And these very things that I disliked back then are part of the reason I like it now.
People with little to no experience can rip scripts off the Internet and coax their browsers to do some pretty cool things, opening up possibilities for amatures. But those of us with a deep theoretical background in computer science can learn quite a bit from this scripting language.
What Now?
I've grown a lot professionally since the mid-nineties. And so has JavaScript (damn, are we sure we can't re-re-name it back to LiveScript). The bulk of why I disliked JavaScript a decade ago is still there, but now we can leverage it more.
It’s not perfect, but it is a tool.
I’m not going to argue whether or not it’s an OO lanaguage. I don’t care. I care what it can DO. And, what it can do is a heck of a lot more than what CPM (copy paste modify) coders are leveraging. And that’s why you need to learn how to really write it.
What JavaScript is, is a Dynamic Language.
So, it’s got loose typing. As I said, that’s part of the reason I didn’t like it. But, what that means is that has Dynamic Typing. It has lambdas. It has great closure support. You can dymically add methods. So, say you get a bit of JSON from the server. On the client you can take that and mutate any function or property you want.
I also disliked that it was not compiled. But, what it is, is Interpreted. Don’t use JavaScript on your webpage when you don’t want to, but want to use it when it’s in the user’s best interest. You can evaluate things on the fly without having to bother the server.
One of the most popular JavaScript libraries today is jQuery. jQuery allows the developer to more quickly (by quikcly I mean with fewer lines of code in this case) do some really cool stuff with JavaScript by acting as a framework to extract away various JavaScript interactions. It’s not the only JavaScript library, but, it is much more lightweight than it’s competitors. It has many, many plugins that allow you to do things from dynamically sorting tables of data to asynchronously uploading documents to cascading drop downs that call back to the server when trying to populate the secondary drop down.
What about jQuery makes it be able to do these things? jQuery has a very fluid interaction with the DOM making it very simple to traverse. Those three examples above might sounds simple, but those are very complex client-side interactions. It’s the simplicity of these client side interactions that made me come around to JavaScript in the first place. jQuery is like JavaScript on steroids (where those steroids are a library to use JavaScript easier). And the beautiful thing is, since JavaScript is so dynamic, you can extend these functionalities however you want. Now, this is why it’s a real programming language. You’ve got nearly unbridaled access to the DOM. You’ve got the ability to ping little trickles back to the server. If you know what you are doing, there are some seriously cool things that you can start doing.
JavaScript and the extensions done on it by jQuery makes AJAX syntax very clean. The J in AJAX is for JavaScript, but without a powerful library can be clunky. We can forget about the update panel (which we should since all it does it break the page life cycle) and start using the HTTP protocol like it was meant to be used. We can post and get what we want when we want it. And that’s not a toy, my friends, that can get pretty complicated pretty quickly.
And, I love the Intelesense support in VS 2008. And I love how easy it is to download and how light weight it can be.
Well, I feel like I’ve just gone into a jQuery love-fest. But, that’s why I stopped hating JavaScript. I just love the powerful interactions it enables too much. And that’s what it comes down to. Everything scary about JavaScript is part of what ends up making it such a powerful tool in my coding arsenal.
In Conclusion
JavaScript is not the only tool to solve some of these problems and there are more libraries out there than just jQuery, but these are just tools. Flash and Sliverlight can accomplish many of the same dynamic (aka no page reload) benefits as JavaScript. But JavaScript, especially when leveraged by it’s jQuery library, can open up amazing doors in the web space. If Snobby computer science purists, I’m sorry. I understand where you are coming from, and I usually consider myself among your numbers, but if loving JavaScript is bad then I don’t want to be good.
There’s a lot more you need to know before you can start really using JavaScript and jQuery. I just hope that if you feel the way I used to feel about JavaScript, this article has helped you come around at least a little bit.



