01
Jan

What did James McKay discover?

The answer: that someone reached my blog by typing that very question into Google. It’s been a while since I last went through my Google Analytics search results, but when I did recently, I also discovered that people arrived here by searching for curiosities such as “busker in kilt bath” or “cool stuff that ten year olds like to put on descktop” (sic) or “dentist torture” or “deliverance ministry in horsham uk”. And so, as another year has drawn to a close, here are some more particularly interesting searches from 2008, that I will take a moment or two to comment on:

“corporate dressing, corridors of power”

Does Google know too much about me?! When I came across this one, I had said almost nothing about my new job on my blog. But yes I am expected to wear a tie (except during recess and on non-sitting Fridays) — however, it doesn’t really bother me.

“curiosities of the number 23″

Wikipedia has a pretty comprehensive list. Including, as ever, “in popular culture.”

“how not to look a prat in a cycle helmet”

Simple: don’t wear one. Obviously, if you are concerned about safety, or live in a jurisdiction where the law demands it, this is not an option. You’ll just have to put up with looking a prat.

“how do you pronounce mckay”

It rhymes with “eye,” not with “day.” Even Stargate Atlantis and the folks on the History Channel who interview a well known exobiologist who shares his surname with me get it wrong. Sigh. But talking of Stargate Atlantis, someone asked:

“what programming language do they use on stargate atlantis for the machines”

Hmmm, I don’t know whether Stargate Command use .NET and C#, as whoever searched for “stargate c# dot net” must have been thinking, but apparently, it turns out that the Replicators are programmed in JavaScript. It also seems that one visitor to my blog thinks that there is a “stargate near m6 motorway”.

You really wonder sometimes…

27
Dec

Django: ValueError: too many values to unpack

I came across this error this morning on a Django project that I’m working on in my spare time. I was trying to add some custom permissions to one of the models, as outlined in the documentation, only to have it choke with this message when I ran manage.py syncdb.

A bit of Googling got me to this site, but unfortunately it’s in Polish. Never mind, Google Translate managed to give me enough information. Turns out that I had missed out a comma that I didn’t think was necessary:

class Meta:
  permissions = (
    ("can_drive", "Can drive"),
    ("can_vote", "Can vote in elections"),
    ("can_drink", "Can drink alcohol"),   # <== This comma is important!
  )

Hmmm, I always thought that last comma in Python was only an optional extra in case you decided to add an extra item to the end of a tuple and forgot to add it in to the previous line. Seems it does have some obscure but significant semantic importance after all.

22
Dec

Time for a new job

It’s now just under three years since I started working for EurekaStep Ltd, and it’s been a pretty good time. EurekaStep is a small but friendly company and a great place to work, and you get to serve some very interesting clients with some very interesting technology.

However, the time has now come for me to move on, and so in the middle of January I will be taking up full time employment in the Houses of Parliament.

My job title will be Analyst Programmer and my responsibilities will be to take what goes on in the Commons Chamber, the various Select Committees, and so on, turn it into XML, mash it about a bit with copious quantities of XSLT, and spit out web pages and Word documents that then get sent on to news agencies around the world.

This isn’t actually entirely new to me. I’ve spent the past three and a half months in Parliament already on placement from EurekaStep, doing pretty much that anyway. This was intended to be a stop-gap for them while they recruited some new developers, but I ended up applying for one of the jobs myself, and to cut a long story short, I got it. It’s quite an exciting change nonetheless, since I’m working on-site in a team of about ten or so developers, plus a whole raft of project managers, Enterprise Architects, web designers, producers and other key stakeholders. Up until three years ago, I was the only developer on the projects I worked on, and over the past three years, most of the other developers that I’ve had to work with have been based off-site. That works up to a point, but you can get so much more out of the real-time collaboration that comes from being in the same room, bouncing ideas off your fellow developers, and so on.

05
Dec

Do antivirus scans really need to brick your computer?

Pretty much every antivirus software package that I have ever used has a really annoying flaw. Whenever it launches into a full system scan, it slows your computer down so much that it hardly even responds to your keyboard and mouse. Given that a typical PC has up to half a million files knocking around on the hard disk, such a scan takes a good couple of hours at least.

It seems that when they perform a scan, they process the files back to back, which results in a lot of churning of your hard disk as the heads continually seek all over the place to find the next one. The fact that they’re both processor intensive (with fairly complex heuristics and pattern matching algorithms) and hard disk intensive means that during this time, they effectively brick your PC for a good two hours or so.

I’d have thought it would be fairly easy for them to fix this, by introducing a pause between each file (or each chunk of a file, for larger files) to give your computer a chance to respond to user input in a timely fashion. They could go in to a fast mode if you were running a screensaver, but while you’re actually trying to get some work done, you really want them to ease off the gas.

Unfortunately, I’ve not yet come across any antivirus programs that do this. Why not?

25
Nov

Del Boy versus Turing

It seems that in the world of software development, it is quite common for sales guys to come up with totally unrealistic quotes when pitching to potential clients. First get the contract, then let the developers worry about fulfilling it afterwards.

Only_fools_original_cast I call it the Del Boy approach to marketing, after the character of that name in Only Fools and Horses, who would do things such as sweet talking his way into cleaning antique seventeenth century chandeliers using Windolene and old handkerchiefs. The big problem is that you run the risk of delivering late, over budget, incomplete and buggy software — if indeed you deliver anything at all — and you then have to rely on having sufficient social skills to be able to blag your way out of a corner when everything hits the fan.

One particularly juicy example is this project request on GetACoder.com:

The purpose of this project is to create a debugger program. This program will take as input the source code another program, and will analyze that other program and determine if it will run to completion, or have an error, or go into an infinite loop.

To state that another way, given a function f and input x, determine if f(x) will halt.

At present, there are eighteen bids, with serious offers ranging from US$300 to US$1800, with time estimates between 10 and 25 days.

I just love the way one bidder says, “Its about doing it professionally exactly according to the requirements.” All I can say to him is good luck because you’re going to need all the social skills you can get with this one. This particular assignment is called the Halting Problem, and it was proven in 1936 by Alan Turing that it is, in actual fact, unsolvable.

(Update: It seems that the nice folks as getacoder.com got wise to the fact that this item was a joke and deleted all the responses. However, not before it had received several apparently serious bids from the Del Boy types.)

24
Nov

Another XSLT sweetener

XSLT can be pretty perplexing to newbies. It gives you a small number of string manipulation and other functions, but the selection at your disposal often seems very limiting. It also doesn’t have variables, so unless you’re familiar with recursion and other concepts of pure functional programming, it can seem very much like a language that doesn’t have nouns. It is easy to wonder how you could possibly do anything fancy with it.

Fortunately, the .NET framework allows you to extend XSLT, using the Microsoft-specific <msxsl:script> extension. So, for a simple example, here is a stylesheet that will give you a comma-separated list of the titles of all the items in an RSS feed, converted to uppercase:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:msxsl="urn:schemas-microsoft-com:xslt"
  xmlns:my="http://example.com/my-stuff"
  exclude-result-prefixes="msxsl">

  <msxsl:script language="c#" implements-prefix="my">
    <![CDATA[
      public string join(string separator, XPathNodeIterator nodes)
      {
        StringBuilder sb = new StringBuilder();
        foreach (XPathNavigator node in nodes) {
          if (sb.Length > 0) {
            sb.Append(separator);
          }
          sb.Append(node.Value.ToUpper());
        }
        return sb.ToString();
      }
    ]]>
  </msxsl:script>

  <xsl:template match="/">
    <xsl:value-of select="my:join(', ', rss/channel/item/title)"/>
  </xsl:template>
</xsl:stylesheet>

In order to use this, however, you need to specifically enable it when you are performing your transform. To do this, you need to pass a XsltSettings object in the call to your XmlCompiledTransform’s Load method:

var transform = new XslCompiledTransform();
transform.Load("test.xsl", new XsltSettings(true, true), 
    new XmlUrlResolver());
transform.Transform("test.rss", new XmlTextWriter(Console.Out));

An alternative, cleaner approach is to use an extension object, which has the added advantage that you can use Linq and other C# 3.0 goodies as well:

var transform = new XslCompiledTransform();
var transformArgs = new XsltArgumentList();
transformArgs.AddExtensionObject
	("http://example.com/my-stuff", new MyExtensions());
transform.Load(@"test.xsl");
transform.Transform(@"test.rss", 
	transformArgs, new XmlTextWriter(Console.Out));

where your extension object might look like this:

using System;
using System.Linq;
using System.Xml.XPath;

namespace ConsoleApplication1
{
  public class MyExtensions
  {
    public string join(string separator, XPathNodeIterator iterator)
    {
      return String.Join(separator,
        (from XPathNavigator i in iterator 
         select i.Value.ToUpper()).ToArray()
      );
    }
  }
}

and your stylesheet might look like this:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    xmlns:my="http://example.com/my-stuff"
    exclude-result-prefixes="msxsl">

  <xsl:template match="/">
    <xsl:value-of select="my:join(', ', rss/channel/item/title)"/>
  </xsl:template>
</xsl:stylesheet>

Your code needs to be executing under full trust in order to use XSLT extension objects.

21
Nov

What to drink when you don’t drink

Although I don’t drink alcohol, I am not averse to socialising in a pub from time to time, especially now that smoking in enclosed public places is illegal. However, you naturally expect a few raised eyebrows when you order something non-alcoholic, if not outright peer pressure to go for something inebriating.

Surprisingly, I find that almost nobody ever bats an eyelid, and I suspect that it’s because I always order a Diet Coke. It helps if you insist on Diet Coke rather than normal Coke, with ice cubes and a slice of lemon: the embellishments have a somewhat distracting effect that makes it easier to overlook the fact that none of them contain C2H5OH.

On the other hand, I don’t recommend orange juice, unless you want to draw attention to the fact that you intend to remain sober for the entire evening. In situations like that, it makes you look like a stereotype.

17
Nov

An XSLT sweetener

If you’ve ever done anything with XSLT, you’ll no doubt be wondering why anyone would want to use such a verbose programming language, given that you need to churn out code such as this all over the place:

<xsl:element name="foo">
  <xsl:attribute name="bar">
    <xsl:value-of select="/some/xpath/@expression" />
  </xsl:attribute>
</xsl:element>

However, did you know that you can do this instead? Much cleaner and easier to read:

<foo bar="{/some/xpath/@expression}" />

(Hat tip: Ned Batchelder)

11
Nov

A train of thought

I’ve made a few observations after ten weeks of daily commuting to London.

The unpleasantries of the rush hour can be alleviated somewhat by choosing your train carefully and getting on at the right place. I always catch the 07:25 train to London Victoria at Horsham and go for the rearmost carriage, and so far I have had a 100% success rate at getting a window seat. Most of the stations between Bognor Regis and Crawley have short platforms, and consequently the front of the train is generally much more packed out than the rear. I do not recommend getting on at Littlehaven: its platform is only four coaches long, but more people get on the train there than at Horsham, and it can be a bit of a crush at times. Then you have to jostle past people through several coaches in order to get a seat.

This effect is even more pronounced on the way home in the evenings. On Thursday I made the mistake of going for one of the front four coaches, and even though I had a seat, it was not a pleasant experience. The entire Littlehaven crowd pile in to the front four coaches right from the word go, rather than spreading themselves more evenly through the train and moving forward once we get to Crawley. This means that the back of the train is definitely the place to be if you are disembarking elsewhere.

I avoid the Underground like the plague. London buses may be a bit of a lottery in terms of overcrowding — I’ve had both good and bad experiences on the 507 — but at rush hour, the Underground is guaranteed to be so packed out that it makes a Kenyan matatu look like an intergalactic void. However, as I am working just under a mile from Victoria, it is cheaper, less crowded and more healthy to walk, as well as giving you a good bit more elbow room at only minimal cost in terms of time.

Finally, the best place to sit in a train is near the middle of a carriage. You get a pretty smooth ride there, whereas by contrast you get jolted about quite a lot near the ends of the carriage. It’s much the same reason as why the middle of a boat is where you’re least likely to be seasick: the carriage acts a bit like a lever, so the ends tend to wiggle about a lot more. Another thing about the ends of the carriage is that they can be a lot noisier if the doors at the end get jammed open.

27
Oct

Unit testing URL generation and Html.ActionLink in ASP.NET MVC

As I’ve been working with ASP.NET MVC lately on a couple of websites, the one thing I’ve found the hardest to get used to is the routing engine. Getting it set up to parse your URL to give you a route is straightforward enough — the hard, and often confusing, part is the helper functions such as Html.ActionLink that generate URLs from route data. Sometimes the URLs look different to what you expect, but they work nonetheless; at other times, they are just plain wrong, especially if you have a complex routing table set up.

The logic behind constructing the URLs is fairly complex, and depends not only on the route data that you pass into the ActionLink method, but also on the route data that comes from the original URL that you used to access the page in the first place. If you have routes that are any more complex than the fairly trivial examples that come in the out of the box application templates, it can quickly get pretty confusing. Furthermore, chopping and changing the order in which you register your routes can get things totally out of kilter, so you really need a comprehensive suite of unit tests to be able to tackle it with any hope of retaining your sanity whatsoever.

The subject of unit testing your routes to make sure that you are getting the correct route data out of them has been covered by Phil Haack and Stephen Walther, so I won’t go into any further detail about that aspect here. However, I’m going to expand a bit on Phil’s methods to show how to test things the other way round: making sure that when you pass some route data in to Html.ActionLink, it gives you the URL that you expect.

As with Phil’s sample code, I’ve used Moq to mock the context, request and response, and I’m using Eilon Lipton’s technique of using an anonymous class as a dictionary literal. You can download the code as a Visual Studio solution if you want to get up and running with it straight away. Here’s a quick look at the methods that do all the work:

string FindUrlToRoute(string currentPage, object routeData)
{
	var mockContext = new Mock<HttpContextBase>();
	var mockRequest = new Mock<HttpRequestBase>();
	var mockResponse = new Mock<HttpResponseBase>();

	mockContext.Expect(c => c.Request).Returns(mockRequest.Object);
	mockContext.Expect(c => c.Response).Returns(mockResponse.Object);
	mockRequest.Expect(c => c.AppRelativeCurrentExecutionFilePath)
		.Returns(currentPage);
	mockResponse
		.Expect(c => c.ApplyAppPathModifier(It.IsAny<string>()))
		.Returns((string s) => s);

	var route = routes.GetRouteData(mockContext.Object);

	var requestContext 
		= new RequestContext(mockContext.Object, route);
	var url = new UrlHelper(requestContext);
	var dict = new RouteValueDictionary();

	foreach (PropertyValue property in GetProperties(routeData)) {
		dict[property.Name] = property.Value;
	}

	var path = routes.GetVirtualPath(requestContext, dict);
	return (path != null ? path.VirtualPath : null);
}

protected void AssertRouteUrl(string currentPage, string expectedUrl, 
	object routeData)
{
	var str = FindUrlToRoute(currentPage, routeData);
	Assert.AreEqual(expectedUrl, str, "URL was wrong!");
}

The sharp eyed among you will note that I am not actually calling Html.ActionLink() itself, but a different method, routes.GetVirtualPath. When you construct an action link, or when you use Url.Action(...) in your views, ASP.NET MVC ends up one way or another running your data through the GetVirtualPath method of your route table. You have to mock the Response.ApplyAppPathModifier method — a fact that wasn’t immediately obvious, and it took a bit of digging around in the System.Web.Routing assembly with Reflector to find out exactly what needed to be done.

You can then check to see whether you are getting the correct URL out by calling in to the AssertRouteUrl method as follows:

[Test]
public void TestBlogPath()
{
	AssertRouteUrl(
		/*
		 * currentPage is an app-relative URL, so it must be
		 * prefixed with a tilde (~). Note that this is required
		 * and must point to a valid route.
		 */
		"~/blog/2008/10/25",
		/*
		 * expectedUrl, on the other hand, is an absolute path,
		 * so it shouldn't.
		 */
		"/blog/2008/10/10",
		/*
		 * The route data. Note that you specify your controller and
		 * action here as the controller and action properties 
		 * respectively.
		 */
		new { 
			controller = "blog",
			action = "index",
			year = "2008",
			month = "10",
			day = "10"
		}
	);
}

Download: Unit testing route URL generation - Visual Studio solution