Thursday 17 July 2014

What is jQuery and Why Do Developers Love It?

This a post about something called jQuery and you probably have never heard of it if you don't build websites.

As some context, I've been programming for the web since 2000.  It's been a pretty rough ride at times, especially in the heady days when everything was achieved through a mix of standard PHP files, HTML and Macromedia Flash.  This saw you needing a plugin for your browser that took an age to download over dial-up and not much shorter if you were lucky enough to have early 256k 'broadband'.
But there's always been JavaScript, too.  A plucky little scripting programming language that's almost as old as the web, it was designed to allow user interactivity on web pages and in direct competition with Microsoft's Visual Basic Script.  In essence, JavaScript was a product of the Browser Wars of the mid-1990s; but without claiming a winner, I don't know anyone that uses VB Scripts anymore.  JavaScript allowed buttons to change when you clicked them, disable text boxes on forms and move items on the page depending on user input.  If you've ever encountered a little bit of text pop up telling you that your password isn't long enough or your email address isn't correct - you've probably been using JavaScript.
The problem that JavaScript faced, like many other web technologies both present and past, was one of cross-browser adoption and support.  Your browser (such as Internet Explorer, Google Chrome, Mozilla Firefox, Apple Safari) needs to support any language used on a given webpage.  There's a three main languages on many webpages today: HTML (HyperText Markup Language) describes the content of a page, CSS (Cascading StyleSheets) describe the look and design of a page and JavaScript controls the majority of the user interaction elements on a page.  All of these have had to fight for their place as the language for the job and have very good adoption nowadays as they've been standardised to describe how the browser should understand and implement their rules and instructions.  This has meant that you can reasonably expect any site to work in any browser.

JavaScript is a neat little language that is perfectly suited for the web.  Web development can be fast-paced, should be data-size efficient and genuinely do something useful.  This roughly can be translated as being experimental, lightweight and making things easier for users.  JavaScript can do all these things, but from a programmer's perspective it means a lot of lines of code and a lot of repetition.  A solution to this is home brewed libraries - that's a book of snippets of code that can be used as a recipe whenever you need to do things over and over.  So developers over time can build up a home brewed library with useful code snippets that make them faster at completing a task - they make a generalised library of snippets to do almost anything.  That was the situation on the web circa 2005.

One problem with generalised libraries in JavaScript is one of file size growing.  A browser needs to download the whole library when a page loads, so a large library can quickly grow to a megabyte or more.  If every developer has their own library, you need to download it if you haven't before.  This makes sites slow to load the first time, giving users a bad experience.  Browsers can cache a library so they remember they have that file and needn't download it every page load, but the problem remains for that first load.  So the solution is a ubiquitous library that every site uses such that you only need visit one site with that library and forever you have a copy of it for use on subsequent sites.  Voila, you've solved the problem.  For many years now, the Web has been using jQuery as this library.
The logo of jQuery has the strap line "write less, do more" and that's pretty much what's going on.  Before jQuery was released in 2005 programmers had to write the same lines over and over, precious development time given over to repetitious and tedious tasks which sap concentration and leads to mistakes.  jQuery takes these tedious tasks and makes shorthand available to cover a multitude of everyday things to free up development time so web developers can develop and build exciting and useful things.  Since many, many sites use it, it's never a problem to include it.  It's also grown up in a mobile-aware web, so there's a version with extensions written to better work with small touch screens.

What this means is that developers can throw something together to experiment, see how an idea might work with much less time cost to do so.  Developers can share other snippets based on this library to improve user experience or further shorten development time.  Sites can now do some really interactive things and allow users to better enjoy their visit.

Personally, time and again, jQuery surprises me with yet another feature that allows me to do something in a couple of minutes I thought would take a lot of code and a lot of time to write.

That's why I ♥ jQuery.

No comments:

Post a Comment