Monday, February 14, 2011

Some first impressions of Javascript

In the past couple of weeks I've been learning Javascript. I guess I put it off for so long because I thought it would be simple to pick-up and learn. Unfortunately, I've discovered that's not the case. The complexity (for want of a more suitable term) is definitely an encumbrance to learning.

After two weeks of reading and writing, I view Javascript as a comparatively crude procedural language with an overly-liberal free-form structure that relies heavily on programmer technique and understanding of internals to code solidly. For the beginner, the language seems quite confusing, but it's possible to write stuff.

It seems there is no 'correct' approach to author good code. Should you write Javascript in a defensive, yet contrived, object-oriented manner or a direct, simple functional manner? Is the goal to write small code or to write protected code? There is plenty of documentation out there that will advocate both approaches. Unlike C and C++ there is no single, revered, authoritative source of information.

Javascript seems to be a language that has evolved to keep pace with changing standards in HTML and associated technologies. It has a big job to do. It integrates fully with and can substitute for many functions of CSS and HTML, of which there are many versions.

Javascript has internal libraries to support a bunch of things to do with browser content, control and interaction, and many external libraries such as JQuery, Prototype, Dojo, YUI and others that will do anything from compensating for deficiencies in Javascript to offering complex widgets, like popout calendars, forms and animations.

In fact, when all the hens come home to roost the amount of information to learn is mind-boggling, disorganized - and complicated. To further steepen the learning curve, programmers must consider issues related to performance, browser compatibility, event handling, security, scoping, user and programmer friendliness.

There are not many languages that compare to Javascript. Simply the fact that the interpreter is a specialized language hosted by a web browser host makes it unique. But Javascript carries some noticeable similarities to other scripting languages like Perl and Ruby.

Like Perl, it is not really object oriented, but can be carefully used in such a way as to mimic object-oriented features to provide some form of inheritance, member access protection, and possibly classes and class members. I don't think polymorphism enters the picture since there are not really any meaningful types. Like Ruby, it's object-oriented model is soft, mostly typeless, and you can mutate "objects" with new members easily after instantiating them. The idea of hidden prototypes is similar to Perl's symbol table and Ruby's "shadow classes". Execution context and reliance on closures are other things which bear similarity to Ruby and Perl - but closure oriented programming is hard to understand and diagnose.

The whole "a function is an object is a hash" thing perhaps has gone too far when you discover Javascript arrays are quite inefficient, being hash tables (or at least associative arrays) themselves. And there are no integers. Or something called the scope chain (still learning about that) can easily kill off performance if you aren't aware of its behind the scenes behaviors.

But in the end, the language itself is double-edged, with features that are simultaneously beneficial and detrimental. I suspect it is a language of trade-offs.

I should mention the supporting toolsets, especially debuggers, are fairly low-quality when compared to C and C++ counterparts, although the profilers are pretty good. It depends on the browser. Probably the best debugger I've found is the one included with Chrome - but it goes off into the weeds, has a few questionable behaviors, and isn't comprehensive.

It's a little sad to think that a language so muddled is the pillar of the future -- the future being cloud applications. Yet, this is kind of the regressed world we live in, muddled and uncertain.

No comments:

Post a Comment