james mckay dot net

because there are few things that are less logical than business logic

February 2008

16
Feb

Pro JavaScript Techniques

Since I started taking JavaScript seriously just over a year ago, I’ve found myself a bit disappointed with most of the online resources for it that are knocking around. The main ones seem to concentrate solely on the basics, and tend to be aimed at beginners — people who are happy to write code in a purely procedural manner and just want the basic information needed to get the job done, even if it does mean writing gratuitous amounts of copy and paste code.

Personally, I’ve felt a bit disappointed by this. I’ve said before that I think of JavaScript as the new Scheme — so with that in mind, anything that treats it as if it were merely client-side PHP will naturally be something of a disappointment. Perhaps this is a case of quidquid latine dictum sit, altum viditur on my part, but I like to use closures, lambdas, iterators, generics, Linq and so on in my code to maximum effect. I am also firmly of the opinion that every professional developer needs to be familiar with these concepts too — after all, they show that you have the kind of mind that can handle the complexities of software development, and won’t stumble over the FizzBuzz problem.

pro-jsSo when I came across Pro JavaScript Techniques by John Resig on Thursday, I thought it sounded like a breath of fresh air. Resig is something of a JavaScript guru: he is the lead developer of jQuery, and really knows his stuff, so I expected it to hit the mark in this respect. I promptly ordered it through Amazon.co.uk, and it arrived pleasingly promptly yesterday lunchtime.

The book certainly does not disappoint. Following a short introductory chapter, it gets right down to business with a chapter on the more advanced features of the language such as closures, currying, scoping rules, and how to make full use of JavaScript’s somewhat unorthodox, prototype-based approach to object oriented programming. This is followed by advice on how to write reusable code, unit tests for your scripts, and how to enforce good code conventions with tools such as JSLint. The rest of the book focuses on the practicalities of real world JavaScript as it works in the browser, with chapters on the DOM, events, CSS and forms, and Ajax, and ties it altogether with several practical examples including an image gallery, an Ajax search box, an Ajax wiki, and a Google Reader style "never ending" WordPress theme. He treats the subject in a fair amount of depth, about as thoroughly as you can in 350 pages, covering gotchas and issues with common browsers along the way, and points to resources on the web where you can find out further information.

This isn’t a book for complete JavaScript novices — it assumes a certain amount of familiarity with the language, and is written more from a perspective of adopting professional best practices and producing high quality code rather than from simply getting you up and running quickly, so at least some experience of JavaScript is necessary. However, it is not a difficult read, and a competent developer with at least some basic JavaScript experience should find it fairly accessible.

The only downside to it is that as it is now two years old, there are already one or two omissions that date it somewhat: it does not cover Internet Explorer 7 or Safari for Windows, for instance, and it still recommends testing your code against Internet Explorer 5.5, which has since pretty much fallen off the radar. However, all the content is still applicable today, and no doubt will remain so for quite some time to come. Personally, I would recommend it to any professional web developer who wants to improve their JavaScript skills. (And if you are a professional web developer, you jolly well should be improving your JavaScript skills.)

07
Feb

恭喜发财, now give me some Unicode

Since it is the Chinese New Year, I thought I would say 恭喜发财 to all my Chinese readers and entertain you with a little tidbit of information about C# that you may not be aware of.

You would undoubtedly expect this code to compile correctly:

string HappyNewYear = "恭喜发财";

However, did you know that this will compile correctly as well?

string 恭喜发财 = "Happy New Year";

As an explanation: If the Unicode standard considers something to be a letter, C# allows it in identifiers. This means that Chinese ideographs are all fair game. Unfortunately, however, this is likely to make you rather unpopular in code reviews with any of your co-workers who can’t read Chinese.

Incidentally, 恭喜发财 (pronounced “gong xi fett choi”) translates approximately as “Live long and prosper.” If you are a Trekkie, remember to give a Vulcan salute when you say it.

04
Feb

Seventy-one

I surpassed 70 words per minute on Colemak for the first time today. I have now all but abandoned qwerty…

(For those of you who are getting bored with me being a noisy Colemak fanboy, this will be the last of it round here — promise. I’ve started up a separate blog for that.)

03
Feb

Published

My blog entry on Volta, GWT and leaky abstractions has been reproduced in the .NET Developers’ Journal.

This was a particularly interesting post because a lot of people are pretty impressed with Volta, and on the face of it, I was pointing out what I saw as its potential shortcomings. It has also attracted some comments from fairly accomplished developers — Bruce Johnson of Google’s GWT team, and Mats Helander, who wrote one of the first O/R mappers for the .net framework. However, while I may have come across a bit negatively, my opinion is not actually so much an anti-Volta/GWT/RJS one as a pro-JavaScript one. Since I started taking JavaScript seriously a year or so ago, I’ve really started to appreciate it, and to be honest, I think that developers who hide from it altogether behind abstraction layers of whatever nature are really losing out.

I’ve also had an e-mail from a journalist asking me for my opinion on MySpace for an article that she’s writing. I’m not sure I’m the best person to ask on that one to be honest: my experience of MySpace is pretty much limited to opening an account, discovering it doesn’t work with Windows Live Writer, closing it again, getting spammed, and experiencing all the yuk-that-is-gross reactions that any respectable, standards compliant, XHTML addicted web developer experiences on seeing your average profile page with gratuitous background images, animated GIFs, thrash metal background music, and broken rendering in Firefox.

It’s interesting where blogging can take you…