Happenings

Film Fight: July 2008

Only 4 films this month:

First up is Frank Darabont’s take on Stephen King’s The Mist. Amongst filmed versions of King’s work, it ranks amongst the most subtle and controlled pieces. Rather than explicitly showing everything that happened (creatures coming from another dimension and covering the world in a deep fog) and giving us another brainless monster/horror piece, Darabont has focussed on the real heart of the story: the tension between believers and non-believers, through a fairly cunningly chosen bunch of set-pieces. Slowly, different fears and prejudices seep through a group trapped in a supermarket until they inevitably turn on one another. The way this is done is masterful, and the climax is pretty heartbreaking, cheapened only slightly by an unnecessary denouement. The characters themselves are fairly one-dimensional, but that’s the point: they’re only aspects of ourselves. Very strong, subtle film-making, though perhaps a shade on the long side. Recommended.

Jack Black continues to dominate Hollywood, moving into animated shenanigans with Kung-Fu Panda, the story of a martial arts-obsessed panda who becomes entangled in a years old grudge match to become the best at kung-fu. It’s happy enough, has plenty of laughs, and neat pieces, but is let down by a fairly mediocre performance by the titular character. Black is doing a watered down version of his normal schtick, without really emoting. He’s a great presence, but not much of a voice actor. Still, there’s enough in the film to make it watchable and even enjoyable.

Pixar, on the other hand, are a powerhouse of animated movies and their latest release, Wall-E, is no exception. This story of a lonely robot and his first love is set against the backdrop of environmental disaster, and humanity’s laziness. Suffice to say it all works out the way you’d expect, but the story is still worthwhile. More interesting is the style in which the story is told: almost no dialogue, beautiful animation and lots of brilliant realised characters. While it doesn’t go into great depth on any of the key themes, something that is to be expected from a kid’s film, it is a great story and well told. Another hit for Pixar.

Finally, Christopher Nolan continues to redefine the superhero genre in The Dark Knight. I’ve discussed this film more than any other in recent memory and I agree with the pervading feeling: it’s extremely good. Heath Ledger is incredible as the Joker, the tone is well set, the cinematography is exceptional, and it’s a thoroughly enjoyable film. That said, it’s not without flaws. Most notable is the burgeoning length and lack of clear focus. Building up Harvey Dent for the fall to become Two-Face was necessary, but then cramming so much of the character into the back half of the movie? It felt forced and pushed the film longer than it could comfortably sustain; that strand could and should have been picked up in the next part of the series. There’s plenty of other cuts that could be made: most of the cast being rendered mere scenery compared to the scene-stealing joker. I could go on (and have in conversation) with list of problems with the movie, but ultimately I enjoyed it, and thought it was a compelling piece of film-making. I recommend it highly.

I’m a little torn for this months’ winner: should it be a subtle horror movie, a brilliant but light story, or a flawed but enjoyable blockbuster? They’re all great films, but I think it’s going to be The Dark Knight, largely due to Ledger’s perfectly-pitched attempt at the classic Joker character.

Fixing a Gap in Javascript

An interesting problem: a bunch of Struts Nested tags generate HTML. Separately, we write some Javascript that makes use of getElementById() to find some of the generated elements. This works in Internet Explorer, but fails miserably in FireFox. Why?

Well, it turns out that Struts Nested tags generate elements that have a name attribute but not an id. Separately, IE actually does the wrong thing with getElementById(): if a search by id fails, it will try to match by name; that’s fundamentally broken. It’d be fine if a bunch of libraries and other functions didn’t prefer the use of ids, and if other browsers did the same thing. They don’t and we’re stuck with the mess. Let’s clean it up.

The right thing to do would be to cripple the IE version into the correct behaviour. Now everyone agrees on what should happen.

Let’s assume, though, that this isn’t possible, that there’s a large dependency on this behaviour that cannot be changed for whatever reason: no access to the JS, unwieldy codebase that we don’t have time to fix straight away (bad!), or something else. It’s not really that important. If you need to go for the wrong solution, here’s how:

`

document.nativeGetById = document.getElementById;
document.getElementById = function(id){
  var el = document.nativeGetById(id);
  if(el == null){
    el = document.getElementsByName(id)[0];
    if(el != null){el.id=id;}
  }
  return el;
}

` That’ll work and it’ll even make sure subsequent calls use the native version of the function for extra speed. Now, there are a couple of small issues with this function and some pretty obvious optimisations, but I’ll leave that as an exercise for the reader.

Neater Java

In Java 5, as all good Java developers will know, Sun introduced the enum type. Since I’m sure anyone who is interested in what will follow knows that already, I won’t bore you with the details but there’s some decent historical information on Sun’s reasons available. I’m going to be talking a little bit about operations on enums, so let’s get started by defining one:

`

public enum OrderState{
PENDING, CHECKING_CREDIT, PROCESSING, SHIPPED, RECEIVED, CANCELLED, RETURNED
}

` Each of those items in the enumeration represent some state that an order can be in; an order being anything you can buy from an online store. We’re also going to have a variable called `currentState` which will hold one of these enumerated items. Let’s not think too hard about how accurate the model is, it’s really just there as a simple example. Now imagine that one of our requirements is to enable a button if the currentState is one of several possible states, but not in any other state. For example, the “Cancel” button is enabled while the current state is `PENDING`, `PROCESSING`, or `CHECKING_CREDIT`. We’re doing a containment check, a pretty common operation. The standard Java approach would be: `

if(currentState.equals(Order_State.PENDING)|| currentState.equals(Order_State.PROCESSING)|| currentState.equals(Order_State.CHECKING_CREDIT)){
    cancelButton.enable();
}

` Yuck! It gets the job done but that is some verbose and ugly code. We know that there’s got to be a better way, and there is: the [EnumSet](http://java.sun.com/j2se/1.5.0/docs/api/java/util/EnumSet.html) class. EnumSet is a somewhat overlooked Set implementation, specifically for grouping together items from the same Enum. I’ll let you look into the details of the API, but there are some very handy methods, such as `range()` and `complementOf()`. As a bonus, they’re also very efficient. How would our example look using an EnumSet? `

if(EnumSet.of(Order_State.PENDING, Order_State.PROCESSING, Order_State.CHECKING_CREDIT).contains(currentState)){
    cancelButton.enable();
}

` That’s much better. Rather than a bunch of ORs that look disconnected and have a lot of repeated syntax, we state pretty clearly what we want: build a set of states, and then see whether the current state is part of that set. There is another neater way of doing this, purely in terms of how syntactically nice it looks, though it requires a little helper function in the Enum. Add the following `in()` method directly to the Enum type: `

public boolean in(Order_State... args){
    return EnumSet.copyOf(Arrays.asList(args)).contains(this);
}

` Now, we can do our contains check like this: `

if(currentState.in(Order_State.PENDING, Order_State.PROCESSING, Order_State.CHECKING_CREDIT)){
    cancelButton.enable();
}

` Very tidy: we’ve got a single method call that expresses exactly what we’re aiming for in a fluent manner. Now, it does have it’s downsides like the helper function in the enum, and the fact that EnumSet is more efficient. I’d suggest, though, that in 99% of cases you’re not going to be caring about those few extra nanoseconds required to convert to a List first, and that the few seconds of developer time you save every time someone has to read and understand each version more than makes up for it.

Postcode Lookups Suck

Websites which have address forms for UK residences, whether for buying new products, paying bills or registering interest, I have a simple request: do not presume your database knows my address better than me.

For the benefit of non-UK readers, let me explain. The UK has a system originally designed for mail delivery called the Postcode, which is similar to the US zip code system. Unlike zip codes, it’s far more granular, generally resolving to a handful of addresses. In fact, most of the time, if you have a post code and house number, you can uniquely identify a particular delivery address. That’s all that is needed: a G1 4BQ and a 35.

In 7 characters, most people can be readily identified. That’s the power of postcodes.

A lot of UK websites take this as an absolute fact with no room for error. They take the data from the Post Office, see that it works for them, and forget about it. You either fit the pattern, or your house doesn’t exist. I’m sure you can see where I’m going with this.

One big problem is that large parts of the UK have some form of communal housing: tenement blocks, modern apartment blocks, high-rises etc. This is where things break down. As well as having a house number (the 35 above), they will also have a flat number like 3/2 (floor 3, flat 2), 4D (floor 4, flat D) or any other variation on the above. This is where things get very hairy:

  • Most post-code lookup systems will offer you a list of possible matches, which often miss some of the variations. That’s incomplete reference data.
  • Most post-code lookup systems are never given updated data (compile once and you’re done). That’s poor data management.
  • Most post-code lookup systems don’t allow for changes to addresses. That’s, again, poor data management.
  • Most systems don’t allow for even more complex forms, where a flat has a house number (35, two part flat number (3/2), and a block number (D); the combinations of these can come in endless forms (D3/2, 35 Example St vs. Flat 2, Floor 3, Block D, 35 Example St).

Most systems will fall flat in the face of any of these. While fixing these problems directly is hard (to near enough impossible), there’s a very simple solution originally posited by Postel:

Be conservative in what you do; be liberal in what you accept from others

Yes, it’s the simple: just accept whatever your users, the people who live at the address, put into your form. They know the correct answer far better than you ever will, so believe them when they put something in. If you want to help them by offering a post-code lookup, that’s fine, encouraged even; but don’t insist that they use it. That just hurts the usability of your system.

Film Fight: June 2008

5 more films for the fight…

Gone Baby Gone is the long delayed directorial debut of Ben Affleck (delayed due to the theme of child kidnapping being a little too close to the bone over the last year), and as debuts go it’s not bad. The mainstay of the plot is reasonable enough: kid gets kidnapped, private investigators who know the locals get involved, there’s more than meets the eye. The performances are for the most part decent, with one outstanding performance put in by Ed Harris; one scene in particular, we see some absolute brilliance in his act. All that said, the film is too clever for its own good. There are too many attempts at pulling the wool over the audiences eyes or twisting a little bit too much for comfort. A shame, because the film is solid, if nothing outstanding.

With the bar set to ridiculously low by Ang Lee’s Hulk, the franchise reboot starring Ed Norton doesn’t really have to work all that hard to improve. The Incredible Hulk is, then, a pleasant surprise, in that it manages to get a lot of things right for the genre. It doesn’t waste time on back story, origins or explaining relationships that are apparent by the interactions, accepting that people know everything they need to know about the Hulk mythos already. It gets moving, keeps a steady enough pace, holds interest, and then plummets with the action sequences towards the end. Sorry, but two CGI monsters who are not grounded in any approachable form of physics break any suspension of disbelief. It had been beating expectations until the final scenes, so to see a mindless fight that turns around for no good reason is a real waste.

Imagine, for a second, taking an entire season of Prison Break and cramming it into 100 minutes; no wasting time on pointless threads that spur things on, no wasted attempts and feeling cheated by do-overs, just the escape: welcome to The Escapist. Although it falls into a Brit crime caper directorial style at times, it manages to show a surprisingly amount of depth and care. The editing is also inspired. While often cutting early parts of a story (chronologically speaking) in amongst the ending is often just an awkward gimmick, cutting the escape and it’s problems in amongst the planning scenes is surprisingly effective. In places that would otherwise be dead weight, tension is kept high. The “But”? The very final moments of the film pull back a needless reveal that cheapens the whole. Without it, it’d be a strong film.

The second of the Narnia films is, unexpectedly, worse than the first entry. Prince Caspian is relatively rich source material (caveats about CS Lewis’ various leanings aside), so it’s disappointing to see such uninspired cinema. We have a largely fluffer plot, terrible performances (some of the kids have gotten worse), dreadful accents, poor CGI battles and an afterthought love interest. If it wasn’t for the cute mice, there’d be next to nothing good to say here. Not worth seeing.

Finally, Wanted is the Hollywood debut of Timur Bekmambetov, previously of Night Watch/Day Watch fame. Based, barely, on a graphic novel of the same name, Wanted is about a fraternity of assassins who follow the path set out by the seemingly random machinations of fate and them bringing someone into the fold who begins to disrupt their way of life. There’s plenty you could do with that as source material but sadly Timur has opted to make yet another piss-poor CGI heavy action yawnfest. The inconsistencies in this film make it painful to watch, the accents are atrocious (I’ve seen James McAvoy act elsewhere, what happened here?), and the whole thing turgid. There’s nothing worthwhile to recommend this film. It fails as an action film, as a story, and on every other level a film can. Awful.

So, despite a regrettable finale, The Escapist is this months winner!