October 2007 - Posts
I've grown fond of the JavaScript || idiom:
function FrobImage(img) {
var width = img.width || 400;
var height = img.height || 300;
// ...
}
FrobImage({height: 100, name: "example.png"});
If img.width exists and it's truthy,
then width = img.width; otherwise, width = 400.
Here, it will be 400 since the img hash
has no width property.
More than two alternatives may be used:
x = a || b || c || ... || q;
A few weeks ago, while cleaning up the error handling in some batch files,
I came across a similar idiom:
foo.exe bar 123 "some stuff" || goto :Error
Only if foo.exe fails (exit() returns a non-zero value),
is the second clause executed.
Perl's die is typically used in a very similar idom:
chdir '/usr/spool/news' || die "Can't cd to spool: $!\n"
though the or keyword seems to be preferred nowadays to ||.
This morning, I came across the ?? operator in C# 2.0,
aka the null coalescing operator:
Customer cust = getCustomer(id) ?? new Customer();
If getCustomer(id) is not null, then that's the value that cust gets;
otherwise it's set to new Customer().
All of these idioms are syntactic sugar and all of them are in my toolbox.
Picture this.
An external USB hard drive plugged in to a machine running Win64.
The OS has virtualized the underlying transport so that it's
essentially indistinguishable from an internal IDE, SCSi, or SATA drive.
Call the machine, Boss, and the USB drive, L:.
Boss is running Virtual PC, which is hosting a 32-bit virtual machine
on top of Boss's 64-bit OS.
Let's call the 32-bit VM, Sidekick.
Sidekick is not only a VM, but a virtual network host.
Boss is bridging connections to Sidekick, and
Sidekick and Boss both appear on the LAN as separate network hosts.
The USB drive has several ISO images, which Sidekick wants to use.
Sidekick connects to \\Master\L$ over the virtual network,
and uses a tool like VcdTool to mount the remote ISO on a
virtual CD drive.
Amazingly enough, it all just worked for me last night.
I'm trying to set up an environment where I can build
Vim with various 32-bit and 64-bit Microsoft compilers and,
more importantly, run the Win64 binary.
I have a set of VM images with distinct flavors of MSVC,
which was necessary to update INSTALLpc.txt
and to keep Make_mvc.mak building.
In previous iterations, I got Remote Desktop access to
a colleague's Win64 machine, but that was at Atlas,
so it's no longer an option.
I bought a new AMD64 desktop system a few months ago
and over the weekend set it up to dual boot.
I've been meaning to play around with Greasemonkey for a couple of years.
Greasemonkey is a Firefox extension that allows users to install
scripts that make on-the-fly changes to the look and feel of third-party websites.
For example, adding price comparisons to Amazon
or thumbnail images to Google search results.
UserScripts.org has a large repository of Greasemonkey scripts.
I finally built my first script the other day.
We're putting together a new feature at Cozi that integrates
partner websites with our site.
Since the feature is not yet announced, I'll just say that partners
will add a link to Cozi on many of their database-driven pages.
That link has a complex, page-dependent querystring.
Until the partners do the work to add the link to their sites,
we were limited to testing and demoing with hand-modified pages.
I wrote a little Greasemonkey script that finds the right spot
on the partner pages to place the link,
scrapes some context to construct the querystring,
and inserts the link.
Now we can test against the real sites and show a compelling demo.
Of course, it only works on Firefox and it requires you to
install both Greasemonkey and this script.
Our partners will have to make minor changes to their sites
before ordinary users can take advantage of the feature.
Some gotchas with Greasemonkey.
Inserting, say, <b>Click here</b> is as simple as
document.getElementById('spot').innerHTML = <b>Click here</b>.
However, inserting a <script> node requires:
var scr = document.createElement('script');
scr.type = 'text/javascript';
scr.text = 'createLink(' + p1 + ', ' + p2 + ', ' + p3 + ');';
document.getElementById('spot').appendChild(scr);
Greasemonkey will definitely become part of my repertoire.
More Posts