Home > Uncategorized > jQuery thoughts

jQuery thoughts

So my project for the last … *checks FTP logs * … 6 months has been a PHP-over-MySQL web interface for accessing the MySQL data and doing all kinds of representative statistics and generally taking a lot of raw data and compressing it in to useful information. Hmm; I guess that’s basically “Business Intelligence” in a nutshell.

So anyways, over the months, I’ve done a lot of revamping, including taking a strictly HTML-based set of form selectors into the unfamiliar waters of AJAX. Overall, it has been pretty good: JavaScript is a (purposely) very C-like language, which means that it works, syntactically, very similar to Java, C, etc. It has wacky functions and weird ways of doing some things — getElementById, I’m looking at you! — but it maps pretty easily on to previous knowledge.

So, in this context, expanding to use AJAX was just a matter of learning to use the JavaScript faculties for such things. They are, admittedly, pretty primitive, but there is something very pedagogical about reinventing the AJAX wheel by writing your own code to craft and send each asynchronous request.

But it all seemed very 1999: surely people don’t accomplish huge projects in JavaScript. So I decided I would take a look at what was purported to be the best JavaScript library at mixing simplicity and functionality with terseness: jQuery.

The problem with terseness is that it’s a two-edged sword. As a programmer, I almost always err on the side of verbosity. Code should, generally, document itself, which means it has to be relatively plain in its operation.

So this philosophy has come into sharp relief running up against jQuery. What am I talking about? Here’s some jQuery code:

 $(document).ready(function() {
   $("#reset").click(function() {
     $("form").each(function() {

That says that, when the document finishes loading, get all the page elements with the “#reset” id, and bind each’s .click to reset the form that it is a member of. Okay, problem one that I’m finding with jQuery: there is next to no assignment. From what I have seen (which isn’t a lot, so bear with me), you never set an item’s .click() operation by saying foo.click() = bar(), you set it by calling foo.click(bar()).

Read in the mindset of C-style syntax, that looks like you’re calling foo‘s .click() on bar(). But, whatever. It has its own code style: that’s cool. Get used to it.

The idea, I assume, is to make everything effectively fit on one line, logically. The above snippet is 7 lines, with 4 semi-colons, but it’s all nesting. That is, programmatically, one line. Which means you could shoe-horn that whole thing in to an onclick on a JavaScript element, or some such.

If you haven’t figured out the problem with this mindset yet, here you go:

$(document).ready(function() {
   $('#faq').find('dd').hide().end().find('dt').click(function() {

Notice the extreme chaining of functions one on to another in there? Each one does something, but all the statements are stuck on the same line. And then we reach that .end() call, which basically says, “We’re done here”. Its a way to put two discrete statements on the same line. It’s an in-line semi-colon. I’m utterly baffled why the code can’t be split up on to two lines as follows:

$(document).ready(function() {
   $('#faq').find('dt').click(function() {

I mean, it’s two discrete code operations that just so happen to operate on the same item. The only thing I can reckon is that by splitting the statement among two lines, you save having to look up $('#faq') twice because neither JavaScript nor jQuery can cache a pointer to the object. Okay, fine.

It’s still client-side scripting code that will have a rather minimal overhead in doing the look-up twice and increase readability immensely, at least to this beginning jQuerier.

I hope that in my studies, I come across some kind of enlightening revelation as to why this design is encouraged from the first moments you touch jQuery. Maybe there’s a good reason. But from where I’m sitting, it just seems like the developers lost their footing on the slippery slope of extreme terseness and began to value packing everything on to one line over maintainability.

They must not be paid by the line.

Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: