@ayende You ought to try Mercurial. in reply to ayende 1 week ago

November 2008

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

Update: There is a memory leak issue with using <msxsl:script> tags in your stylesheets. For details on the problem and how to mitigate it, see here.

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.