A peace-sign

I’m British, working in America, for a German company, and part of my team is in Russia. I reckon if I told someone 20 years ago that this might happen in 20 year’s time, I’d probably have been thrown in a loony bin.

Or as one of my Russian co-workers put it, I’m a victim of globalisation. 🙂

Advertisements

Finally I get to use C# 2 closures

For the last couple of months I’ve been using .NET 2 for the first time. There are a bunch of great new language features in there, generics being the most obvious (and implemented properly in .NET rather than the hack in Java), but another biggy is anonymous delegates.

One use of anonymous delegates is just to inline methods, e.g.:

public void MyFunkyExample()
{
Timer timer = new Timer();
timer.Elapsed += DoSomething;
timer.Interval = 1000;
timer.Start();
}

private void DoSomething(object sender, ElapsedEventArgs e)
{
Console.WriteLine("Hello World! (at {0})", e.SignalTime);
}

becomes:

public void MyFunkyExample()
{
Timer timer = new Timer();
timer.Elapsed += delegate (object sender, ElapsedEventArgs e)
{
Console.WriteLine("Hello World! (at {0})", e.SignalTime);
};
timer.Interval = 1000;
timer.Start();
}

This is handy occasionally when you have a 1 or 2 line method that only gets used as a delegate.

However, the real power of anonymous delegates comes when using them as closures. Take the following code as an example:

public void MyFunkyClosureExample()
{
string[] names = new string[] { "Fred", "Bob", "Sue" };
foreach (string name in names)
{
SetupTimer(name);
}
}

private void SetupTimer(string name)
{
NamedWriter writer = new NamedWriter(name);
Timer timer = new Timer();
timer.Elapsed += writer.Write;
timer.Interval = 1000;
timer.Start();
}

private class NamedWriter
{
private readonly string name;

public NamedWriter(string name)
{
this.name = name;
}

public void Write(object sender, ElapsedEventArgs e)
{
Console.WriteLine("Hello {0}! (at {1})", name, e.SignalTime);
}
}

Now we want to have several timers, but the event of each has different behaviour depending on some local value when the timer event is bound. Pre C# 2.0 you have to setup a custom class to handle this, but compare with the anonymous delegate version:

public void MyFunkyClosureExample()
{
string[] names = new string[] { "Fred", "Bob", "Sue" };
foreach (string name in names)
{
SetupTimer(name);
}
}

private void SetupTimer(string name)
{
Timer timer = new Timer();
timer.Elapsed += delegate(object sender, ElapsedEventArgs e)
{
Console.WriteLine("Hello {0}! (at {1})", name, e.SignalTime);
};
timer.Interval = 1000;
timer.Start();
}

This is much cleaner since we don’t need to introduce that custom class.

Using closures can get a little hairy, especially when you’re trying to figure out which value of variables actually get bound – check out one of Jeremy Stell-Smith’s recent entries on the subject. But of course, you’ll have plenty of automated tests to check what you’re doing, right? 😉

PS – apologies for the lack of decent formatting – I’m still getting to grips with WordPress.

Farewell Top of the Pops

It was with some sadness today that I read that the BBC’s weekly music show, Top of the Pops, is to end its run of 42 years.

Top of the Pops was without a doubt one of the programs I watched the most as a child. My sister is 7 years older than me, so in 1983 when she was entering her teens and I was 5 I was surrounded by the Thomson Twins, Adam and the Ants, Duran Duran, etc. There were certainly darker periods (the late 80s and an excessive amount of Stock, Aitken, Waterman come to mind), but TotP was the one time of the week where I could sit for half an hour and actually see the bands I liked perform (I was a big fan of No Limits on BBC2, but that only had a brief run, and later on I quite enjoyed Chris Evan’s TFI Friday)

I’m surprised the BBC haven’t found a way to keep ratings up. Sure, the internet and satellite TV offers ‘yoof’ (like the Janet Street Porter reference there?) a million different ways to see and hear bands, but there was always something magical about artists getting up on stage and actually performing live (and in periods of TotP’s history often not miming.)

ITV’s similar CD:UK show remains and I now hope that it will keep going. Yes its for kids, but so was Top of the Pops, and I hope the internet doesn’t kill off all these kind of shows.

Picasa Web – Now I'm glad I didn't bother with Flickr

For a while now I’ve been a little envious of people using Flickr to have fancy photo galleries online. I’ve almost used it a couple of times, but for me the whole idea of using a web site to manage pictures just didn’t work. I’ve used Picasa as my photo library and I wasn’t prepared to move away from a desktop-based client to handle my (more than 1GB worth of) digital photos.

When I bought my Mac Mini I was hoping iPhoto might be amazing, and I was considering using it and .Mac to host photos. iPhoto just didn’t work for me either, and I kept using Picasa, and my Photo Blogger Blog (which Picasa integrates fairly nicely with) to publish the odd photo now and then.

But a Photo Blog isn’t the same as an online photo album, and what I really wanted was for Google to add online features to Picasa so that I could manage a web photo library from a desktop client.

Someone at Google must have been listening, since they’ve done just this, and its free. The first 250MB costs zilch, and if you want more its $25 / year for 6GB of storage. That means I can have my entire, full resolution, photo library online, share subsets of it with the world, and yet still have a local client for editting, viewing offline, etc. Flickr, your time is up [1].

My online Picasa gallery is at http://picasaweb.google.com/mike.b.roberts and this replaces my Photo Blog.

[1] OK, Mac-weenies will still use Flickr because Picasa doesn’t have a Mac client yet. But you can still upload/manage photos using the Picasa web interface, and I doubt it will be that long before a Mac-Picasa is released.

Stacked

I don’t normally blog about other people’s blog entries, but this is fascinating.

I’m a big fan of using a lot of delegation, with small methods and small classes, and I can understand when you’re sitting on a bunch of libraries that think the same way you can end up with these kind of stack traces.

Part of me wonders whether this is actually a problem. Modern IDE’s (e.g. IntelliJ, Resharper, Eclipse) make navigating abstracted delegation chains pretty easy (I’m forever pressing Ctrl(+Alt)+B or Ctrl+Alt+F7 in Resharper), and I would hope that the Managed VMs we use do clever stuff too. That said, I wouldn’t expect to have to understand library code more than a couple of calls outside of my own code, so libraries would have to make sure that their abstractions don’t leak too much.

Thanks to Simon Stewart for the link.