Tuesday, December 14, 2010

You Really Need to Learn JavaScript

PHP developers have typically worn a few hats. Few of us are "pure" PHP developers, where we only write PHP and don't scribble together some HTML at least once in a while, or paste and edit some JavaScript on occasion. A lot of us — myself included — came to PHP as a way of adding new functionality to our flat HTML web sites, and PHP's roots as a language that intermingles with HTML are a big reason for its popularity. It's likely that PHP wouldn't be as useful as it is on the web if it wasn't driven by the practical needs of front-end developers.

For the most part, these skills have suited us well. Web apps have typically been focused on the server side. The web pages we view have been static results of computation done on the server, parsed for presentation purposes on the client side. We might have some flashes of dynamic HTML (remember that?) here or there. But with few exceptions, all of the real work is done on the server.

In the past few years, this has started to change. Clients are becoming richer, more complex, and are responsible for more aspects of the app. Web apps are becoming more balanced, with more heavy lifting on the client side. The server side is increasingly becoming a service to be called upon when the client needs more data or wants to save something for access elsewhere. Fewer and fewer server-side apps will follow the model PHP has excelled at, where it generates all of the code that's then parsed and presented by the client.

Yeah, you know all that. Ajax, web APIs, Twitter, &c. It's all very exciting, and you can just have your framework generate that junk for you. You'll be awesome, right?

Probably not. How well does auto-generated PHP work for you? Not so well. You know how you make fun of PHP "developers" who just copy and paste code, or let a tool write it for them? Well, if you don't know JavaScript, you're in danger of becoming that person.

When I say "you need to learn JavaScript," I don't mean "learn how to copy and paste an example," or "learn how to generate JavaScript with PHP." I mean learn it as well as you already know PHP — or better.

Why? Because JavaScript drives rich clients, and all indications are that clients will be getting richer. You won't be able to get away with just adding small snippets of JS to add tooltips and simple animations. Complex processing and UI management will require real knowledge of how to write JS, and how in interacts with the client's APIs — the DOM, local storage, networking, OS features, and more.

The bad news is that this will take a bit of effort. JavaScript's syntax is fairly similar to PHP's, but some fundamental stuff is quite different. The object model will probably confuse you at first. The scoping is very different from PHP. The malleability — how you can change fundamental behavior of built-in language constructs at will — is going to freak you out a little.

The good news is this: you will be a smarter, better, and more in-demand developer because of it. Browser makers are investing plenty of time to create better tools for client-side developers. JavaScript runs orders of magnitude faster in most browsers than it did just 3 years ago.  Local storage APIs, WebWorker APIs (for spawning JS processes), native JSON encoding and decoding: all of these things are recent additions to most browser engines, and all of them add significant speed and/or capability for developers.

What's been more interesting to me, beyond browser-based rich clients, is the introduction of Web Runtime platforms (WRTs). These are platforms for building desktop and mobile apps with traditionally web-oriented technologies, like HTML, CSS, and JavaScript. This approach has some significant advantages over deployment in the browser, mostly related to consistency of rendering and additional APIs. Titanium Desktop from Appcelerator is especially interesting to the PHP developer, as it allows you to develop in PHP (as well as in Python and Ruby) in addition to JavaScript.

Mobile apps seem to be gaining dramatically in popularity as their devices gain market share. WRTs are available for many major mobile platforms, such as:

Using a WRT to create your mobile apps is typically faster than using a device-specific SDK, especially if you're not already intimately familiar with the platform's tools. Additionally, most of the code that runs on one web runtime can be reused on another device with little or no effort. It is because of the similarities between WRTs on various devices and platforms that I was able to create SpazCore — a component library for JavaScript apps that runs on Adobe AIR, Titanium Desktop, and Palm webOS.

Now that you're super-pumped to learn JavaScript, you should know that the vast majority of the books and resources on the Web suck. Typically, they'll focus on simple recipies and JavaScript in the browser, and less on the core JS language. Understanding JS (independent of browser APIs) is, I think, essential to getting a good handle on the language. Fortunately, there are a few good resources for the core JS language:

Once you get into JavaScript, you'll find one thing very similar to PHP: a rich, diverse, and creative community. Both languages are popular, open technologies that aren't owned by a particular company. It's a big reason I'm still excited about JavaScript two and a half years after diving in with Spaz Desktop. Creating and sharing what I've learned is what's really fullfilling for me, just as it has been with PHP.

PHP has proven to be an excellent tool for the server side of web apps, whether we're generating web pages or serving up serialized data structures. As rich clients—in the browser and on WRTs—increase in popularity, PHP developers who are fluent in JavaScript will be well-positioned to create next-generation apps in the browser, on the desktop, and in mobile devices.

 

No comments :

Post a Comment