Tuesday 21 September 2010

A literal improvement

JavaScript literals are getting better. Until recently, the grammar for object literals didn't explicitly allow a trailing comma, like this:

var obj = {
foo: 42,
bar: 27, // <== This is the problem
};
SpiderMonkey (Firefox), V8 (Chrome), and whatever Safari and Opera use don't care, but JScript (IE) prior to JScript 6 (IE8) throws a parsing exception on the comma and your script dies. (JScript 6 / IE8 fix this.)

A trailing comma in an array literal has a different issue:
var a = [1, 2, 3, ];
All versions of JScript so far (including JScript 6 / IE8) create an array with four (yes, four) entries, the last of which is undefined. This isn't unreasonable, because the spec wasn't explicit about it and we were always allowed to have blank entries (e.g., var a = [1, , 3];) and those entries defaulted to undefined — but everyone else went the other way and created an array with three entries instead.

Fortunately, ECMAScript 5 clears this up. The trailing comma is explicitly allowed in object literals (Section 11.1.5), and in array literals (Section 11.1.4). In the case of array literals, the trailing comma doesn't add to the length of the array (a.length above is 3).

The team behind IE9 are very engaged with standards bodies now, so hopefully that includes the JScript folks and they'll change the array behavior, though you know it must be a much harder sell for them than the object literal was — it involves changing the behavior of something that did work. Still, here's hoping.

Sunday 19 September 2010

Don't Default Destroy

Here's a little tidbit for UI designers: Don't default to destroying things. You'd think that would have been obvious, no?

No. VirtualBox is an excellent virtualization environment, one of the best in the world and possibly a contender for the top spot. But the default UI has a big UI fault.

VirtualBox, like most good VM technologies, lets you take "snapshots" of VMs that you can then restore, going back in time. Very, very handy for when you're about to do a tricky update and want to be able to roll it back.

In my case, the tricky update hard-crashed the box, which means I had to use the handy Machine | Close to terminate the VM. And lo! The Machine | Close dialog box has a handy tickbox for "Restore most recent snapshot". So I ticked the box, and it worked a treat. I went ahead and kept using the VM for several days.

Then something unrelated made the VM crash, and I did a quick Machine | Close. The next time I fired up the VM, something seemed wrong — files were missing, configuration changes I made a while back were undone, something was amiss!

You guessed it: The Machine | Close dialog box had remembered the "restore last snapshot" setting and so VirtualBox happily destroyed my data.

Should I have noticed the tickbox was ticked? Yes, but it's one UI element of about eight on that window, and the penalty for failing to notice it is unacceptably high. If I had just ticked the box, I could see not asking for confirmation — but not when the box was ticked by default.

Amazingly, this very thing was pointed out in a bug report, and the report was closed as "fixed" when they added the ability to have VMs override defaults if you explicitly set that up.

Um. Yeah. Because that addresses the usability problem. If you know you might make this mistake in the future, you can go update each and every VM you have to make it impossible to use that feature of the dialog box. And then remember to do it for all VMs you create in the future. All because some UI designer wants the purity of either remembering all the options on the dialog, or none of them. I don't think so.

So I've opened a new bug report on it. Hopefully they'll see this as the bug it is, but the point of this post is: Don't destroy things by default, you're likely to piss people off.

Thursday 16 September 2010

Double-take

There's an issue with Microsoft's JScript interpreter (the one used by IE, Windows Scripting Host, and others) that you see mentioned deep in discussions of other things. I thought it would be worth just briefly talking about on its own.

Update: The newer version of JScript used by IE9 and up doesn't have this bug anymore. Yay! Still in IE8 and earlier, though.
Basically, if you use a named function expression in your JavaScript code, JScript will process it twice, creating two separate function objects, at two separate times: First it treats it as though it were a function declaration (even though it isn't), and then it treats it as the expression it is. This is not a distinction without a difference, either, as we'll see below. (Amongst other things, it creates "symbol bleed," putting the function's name in the enclosing scope in clear violation of Section 13 of the specification, which says that it should only be defined within the function's own scope).

What do I mean by function declaration vs. function expression (named or otherwise)? Here's a function declaration:
function foo() {
}
Here's an anonymous function expression:
var foo = function() {
};
And here's a named function expression — this is the one JScript (IE) has an issue with:
var f1 = function foo() {
};
The easiest way to tell whether you have a declaration or an expression is to ask yourself: Are you using it as a right-hand value? E.g., are you assigning it to a variable/property or passing it into a function as an argument? If so, it's an expression. If it's standalone, it's a declaration. Here are some further examples of named function expressions:
bar(function foo(){});

var obj = {
nifty: function foo() {
}
};
So first off, how do we know JScript is creating two function objects? Here's the easiest way:
var f1 = function foo() {
alert(f1 === foo); // alerts "false" on IE, "true" on other browsers.
};
f1();
So, okay, but what do we care? Well, let's say you want to hook up an event handler and have it unhook itself later if some condition is met:

Prototype example:
$('foo').observe('click', function fooClickHandler() {
if (/* ...some condition... */) {
this.stopObserving('click', fooClickHandler);
}
});
jQuery example:
$('#foo').click(function fooClickHandler() {
if (/* ...some condition... */) {
$(this).unbind('click', fooClickHandler);
}
});
Perfectly reasonable, but won't work on IE. The handler will remain attached, because when you unhook a specific event handler, the function reference you give has to be the same as the reference you want to remove. On IE, the above, it isn't: fooClickHandler isn't the same function that we hooked up. The expression returned a different function.

So how do you work around it? Just make sure you're using declarations, like this:

Prototype example:
function fooClickHandler() {
if (/* ...some condition... */) {
this.stopObserving('click', fooClickHandler);
}
}
$('foo').observe('click', fooClickHandler);
jQuery example:
function fooClickHandler() {
if (/* ...some condition... */) {
$(this).unbind('click', fooClickHandler);
}
}
$('#foo').click(fooClickHandler);
If you don't want fooClickHandler to be a symbol in that scope, wrap it up in a scoping function, like so:

Prototype example:
(function() {
function fooClickHandler() {
if (/* ...some condition... */) {
this.stopObserving('click', fooClickHandler);
}
}
$('foo').observe('click', fooClickHandler);
})();
jQuery example:
(function() {
function fooClickHandler() {
if (/* ...some condition... */) {
$(this).unbind('click', fooClickHandler);
}
}
$('#foo').click(fooClickHandler);
})();
Alternately, you could just not use names (and use arguments.callee to unhook the handler), but there are lots of good reasons not to do that (arguments.callee is slow on most browsers, not allowed in ECMAScript's new "strict" mode, and besides, names are good).

So what's this "symbol bleed" issue I mentioned? Well, according to the specification, the scope of the function name in a function expression is confined to the function itself, not the encompassing scope. So:
var f1 = function foo() {
// `foo` is defined here
};
// but not here
Whereas, of course, if that were a function declaration, the foo symbol would (of course!) be defined in the scope in which the function is declared.

(You can see this coming, can't you?) Since one of the times IE processes the named function expression it treats it as a declaration, it incorrectly defines the symbol in the enclosing scope — much like we would do if we didn't use the scoping functions above — which is incorrect.

Happy coding!

Sunday 12 September 2010

Boot, Ubuntu, boot! Good dog!

This isn't about writing software, but it's a snippet, so...

I have a fresh new desktop based on the Intel DH57JG board on which I've happily installed Ubuntu 10.04 LTS desktop. Aside from a video mode detection issue, things are great, but I had an odd symptom: Ubuntu could successfully shut down and power off, but it would crash if I asked it to reboot. Just an annoyance, but surprisingly...annoying.

So naturally I consulted a Major Search Engine(tm)(r), and while I found several threads related to this problem, I never found one where the original questioner actually said their had been solved. I did find one where someone chimed in with "When I had that problem, adding reboot=bios to the kernel options fixed it." So I tried that, but no go. I also found a thread where someone said "I've tried the reboot b, c, and h options but..." So I figured there had to be more options for this reboot thing than just "bios" or none.

Well, it took some digging and installing the linux source (which is nicely packaged, so not like that's hard), and my machine now happily reboots when I ask it to. Because the options for reboot are a bit hard to find, here they are (for the x86 architecture) as of the Linux kernel 2.6.32-24:

warmDon't set the cold reboot flag
coldSet the cold reboot flag
biosReboot by jumping through the BIOS (only for X86_32)
smpReboot by executing reset on BSP or other CPU (only for X86_32)
tripleForce a triple fault (init)
kbdUse the keyboard controller. cold reset (default)
acpiUse the RESET_REG in the FADT
efiUse efi reset_system runtime service
pciUse the so-called "PCI reset register", CF9
forceAvoid anything that could hang.
(If you're curious, the answer in my case was reboot=acpi. At least, that was the first one I tried that worked, so I stuck with that.)

If you need a more up-to-date list, or a list for a different architecture, here's how I got that:
  1. I installed the linux-source package, which dumps a bzip2'd source tarball in /usr/src

  2. Uncompressed and untarred the tarball

  3. Looked at the arch/[architecture]/kernel/reboot.c file. Since I'm on an x86 processor on the 2.6.32 kernel, the full path in my case was /usr/src/linux-source-2.6.32/arch/x86/kernel/reboot.c but of course YMMV.
(Lest you think I was terribly clever finding the options in the source, the first place I looked was in the kernel parameters documentation —/usr/share/doc/linux-doc/kernel-parameters.txt.gz — but all that said was to look in reboot.c. :-) )

I applied the change by editing /etc/default/grub, adding that to the GRUB_CMDLINE_LINUX_DEFAULT variable, running update-grub, and rebooting (er, that is, shutting down and then starting up — rebooting would have crashed, of course).

Hope this saves someone else some time.

(If the title of this post seems oddly familiar but you can't place it, here.)

Thursday 9 September 2010

Modern Youth

My son has now unequivocally demanded his own proper computer.

My son is not yet three years old.

At this rate, by the time he's six he'll have a better StackOverflow rep than I have.

Sunday 5 September 2010

Beyond Either/Or

So I was doing a massive file comparison operation (hundreds of thousands of files, >100 GB of data) today on one of my Windows boxes and so naturally fired up the excellent WinMerge, my favorite — by a wide margin — Windows-based visual diff/merge tool. And it did something that is so smart, I just had to mention it.

Usually when you ask software to do an operation on a set of files you identify by wildcard or what-have-you, the software does one of two things: It either goes off and finds all of the files first, and then starts processing them; or it just starts processing and discovers the files as it goes. The former is useful for progress bars, for an early indication that maybe you've messed up your filters, etc.; and the latter is useful for those "I don't care how many there are, just get on with it!" situations.

So what was the smart thing the developers of WinMerge did? They did both. One thread went off to discover files, and another thread got on with the business of doing the comparison for me. It was exactly what I didn't know I wanted.

Are there trade-offs to this approach? Probably. You'll be asking the devices you're reading from to do two things at the same time for a while (list files and read files), which could impact overall performance. Or not, it depends a lot on what the devices are — do they have thrashing issues, are you maxing out your channel to them or does your post-receipt processing take most of the time, etc., etc. In my case, I was dealing with HDDs which presumably did have to thrash a bit (or a lot) early on, but for me it was still a really useful user experience.

So the point is, as it frequently is, to remember ask whether "both and" is an option when looking at an either/or choice. (One of probably three questions you should automatically ask when faced with such a thing, the other two being "is 'neither' an option?" and "are there more options?")

Or maybe the point is just to say "nice one" to the WinMerge devs. Either way.

Happy coding. -- T.J. ;-)

Friday 3 September 2010

Brilliant - A periodic table of elements

Josh Duck has done a Periodic Table of the Elements for HTML5. Brilliant (and pretty). In the 20 or so years of HTML, surely someone has thought of this concept before — but if so, I'm certainly not aware of it. Props to Josh!