Saturday, August 18, 2012

JavaScript: More than a Scripting Language?

With the help of HTML5 and modern Web development, JavaScript is finally getting regarded as a real programming language.  But is JavaScript really equipped to handle all of its new responsibilities?

Background

JavaScript/ECMAScript has spent most of its life being written by people who learned just enough of it to create simple scripts, and those scripts were then copied and pasted by those who normally didn't know more than how to set variables in those scripts.  Thanks to things like AJAX, jQuery, and modern Web browsers JavaScript is now more powerful, easier to work with, and an essential part of any modern Web application.

There is a relatively new trend of powerful JavaScript libraries and frameworks to create complete applications in JavaScript and allow for MVC style development in the client.  Node.js has even moved this to the server side.

Why JavaScript Rocks

Node.js is a particularly interesting case because JavaScript was chosen not out of a desire to move JavaScript to the server, but because JavaScript had the qualities that were desired: most notably painless support for asynchronous programming.  To be honest, I've been meaning to tinker with Node for a while now, but it keeps getting preempted on my list of technologies to explore.

JavaScript has a lightweight syntax and has evolved to handle asynchronous evented programming better than most languages.  Its dynamic typing, prototypal inheritance, and first class functions can allow for rapid implementation of complex functionality.  JavaScript almost certainly allows for some of the most rapid development of any popular language available (from a language perspective, the platform as a whole is still relatively sparse).

JavaScript is dynamite!...

It can destroy your requirements faster than most anything else...but it can also take your foot along with it.  The most maligned feature of JavaScript is the global namespace.  Rather than go into the features individually, I'll instead use the global namespace as a symbol for the limitations of JavaScript: it allows for faster, worry-free development at the cost of structure.  JavaScript was, as the name suggests, created to be a scripting language written in relatively short snippets to glue other pieces together within a host environment.  The prototypal inheritance and general dynamism further support this: a script could be written to evolve as it executes rather than being designed before hand.  The immense productivity provided comes with an immense danger of writing tangled, co-incidental code particularly if you mix in some temporal concerns due to asynchronicity.

Should you use JavaScript

Whether to use JavaScript can be reduced to the structure vs flexibility debate that flickers around dynamic vs static languages.  From my perspective that question leads to a human factor.  Structure is always needed for a maintainable system and so the variable factor becomes how much of that structure is provided by the language (and supported by tools) vs how much is required to be maintained by developer discipline.  JavaScript is a particularly dynamic language, and therefore places particular onus on the developers to code in away that allows for the application to be maintainable.  As a project and its associated team size grows larger this is likely to become increasingly difficult.  

JavaScript is a great language for small pieces of functionality.  It also provides an attractive and viable option to produce small applications.  As the size of the application grows, however, the dynamism is likely to get increasingly difficult to manage.  JavaScript is therefore best used in manageable chunks within a larger infrastructure.  A single piece of functionality or the analog of the functionality provided by a small mobile app, essentially a single namespace/package, would be a good constraint for the extent of a JavaScript library's reach.   In that context JavaScript is very good at what it does and it should certainly be used (or CoffeeScript).  

The Future

One of the elephants in the room still remains things like prototypal inheritance and first class functions: those JavaScript features that are alien for most developers.  This has led to many people trying to jam JavaScript to behave more conventionally.  Prototypal inheritance is not something that has any momentum, and as much as I hope for wider adoption of first class functions and techniques like high order functions, I think much of that is too abstract to be as digestible as something like object orientation.  The present JavaScript space is a very exciting one and it will provide a lot of useful fodder for the future.  There seem to be fundamental issues with JavaScript, however, and whether it is able to evolve thoroughly and quickly enough to increase its reach beyond small packages and reach a wider audience of developers is still uncertain.  Most importantly the primary focus of JavaScript must remain to continue to serve its present function as well as it does now, which may always be at odds with extending its role.  

No comments :

Post a Comment