Forwarding events in C#

March 7th, 2010

For a long time I’ve had a problem with events in C#: I want to wrap one class in another, which offers a higher level of abstraction, but this means that all the events have to be copied and lots of boilerplate code has to be written to do the forwarding. The obvious thing to do would be to have an event exposed as a property, which would give clients of the abstract interface read-only access to the event:

public class HigherLevelClass
{
   public event SomeDelegate MyEvent
   {
      get { return impl.MyEvent; }
   };

   private LowerLevelClass impl;

   /* ... */
}

Unfortunately this isn’t valid syntax, and at first glance that seems to be the end of it. However, I discovered today that the designers of C# have actually thought of this, and provided a way round it:

public class HigherLevelClass
{
   public event SomeDelegate MyEvent
   {
      add { impl.MyEvent += value; }
      remove { impl.MyEvent -= value; }
   };
}

Simple, once you know how. But this illustrates an interesting problem with keyword search when applied to programming techniques: I’ve searched for just this information on several occasions, but wasn’t able to find it until I hit on the magic phrase “event forwarding”. In my experience it’s pretty common to know how to describe what you’d like something to do, but not be able to find the same words as the person who knows how to do it.

I’m sure this is far from the only reason, but perhaps it goes some way to explaining why there are eight separate implementations of every single open source library type, each of which is partially complete.

Four things that are going to change whether the publishing industry likes it or not

February 6th, 2010

It was an accident of circumstance that led the music industry to experience a major transformation, from selling objects to selling information, sooner than the book-publishing industry. Simplifying wildly, the fact that headphones are much cheaper to produce than screens is all that has held up the status quo in publishing for so long.

Currently some of the discussion in publishing centers on the fact that music is now sold in $1 downloads rather than $15 albums. But this too is an accident of circumstance. The length of a CD album is not a golden standard of the quantity in which people like to enjoy music, but one person’s personal taste: Sony vice-president Norio Ohga wanted a CD to hold the entire of Beethoven’s 9th symphony, and so for over a decade that is how we purchased our music.

Current commentary sometimes seems to imply that the music industry allowed the business model to be shifted to single-track purchases (to its cost) due to poor negotiation, and that the print industry might avoid making the same mistake. This is a delusion: certain changes in technology simply invalidate the old assumptions.

I expect the Amazon / Macmillan negotiation soap opera will resolve itself before long, but is in danger of drowning out discussion of the lasting shifts that the industry will inevitably go through. Here are a few of my predictions:

More competition with out-of-copyright works

In a physical book shop works that are out of copyright tend to sell for almost as much as recent releases. This is not unreasonable, since they still require editing, desigining and promoting, not to mention the cost of printing the physical book. Shelf space is a major cost in high street book shops, and costs no less when the work is out of copyright.

In a market for electronic books, some of these costs go away. The rest can be defrayed over a much longer period of time, since there is no opportunity cost to keeping books on the shelf. Competition with amateurish free copies will drive down the cost of a professionally-edited version to far below the cost of a new work, while still allowing publishers to turn a profit.

Most authors are not blessed with the wisdom of Marcus Aurelius, the wit of Jerome K. Jerome or the inventiveness of Arthur Conan Doyle (just three authors I’ve enjoyed for free on my Kindle). Of course the modern world has its own share of geniuses, and even those of middle rank can offer great value to readers by writing about the subjects that interest them, but it would be a sad world indeed if people didn’t capitalise on the opportunity to enjoy classic works for peanuts.

More flexibility in length of published works

It used to be that a book had to be of a certain length to be worth publishing. This is going to change, since the fixed costs of printing have gone away. The only question remaining is whether people will consent to pay small amounts for small amounts of content. There is no rational reason why not, but the field of paid content isn’t a shining example of rational consumer behaviour to date.

More competition from the back catalogue

The ‘long tail’ effect, where stock can diversify as shelving cost drops to zero, can flourish in the electronic market as the cost of warehousing drops out of the model. No book need ever again be out of print. This is going to help some authors and hurt others: books that have fallen from prominence aren’t necessarily bad, and if priced keenly they might prove a worthy substitute for recent publications at hardback prices.

More competition from the ‘gift economy’

Once you discard the requirement that books have to be printed on paper to be consumed, the barrier to entry to the market is much lower. Vast amounts of text in blogs, wiki articles and social network postings become readable in exactly the same circumstances as published books.

People who infer that this will drive the price of all textual content down to zero are unjustified in their conclusion and hopefully wrong. A well-researched, professionally-written and carefully-edited document is worth substantially more than an amateurish one, and a rational consumer will be prepared to pay more for it. However, for the portions of our reading time (hopefully not all) that are idle escapism, it may well be that the difference isn’t worth caring about.

Don’t shoot the messenger

I’m not saying the above out of ideology, but because I think the trends are inescapable, in direction if not in extent. More important is what won’t change: people will still exchange money for items that are of value to them. The lesson from the music industry’s experience is that the publishers that embrace the new rules and figure out how to turn them to their advantage will prosper.

Why Haiti? Why now?

January 26th, 2010

Before I start, let me say that the earthquake in Haiti was a tragedy, and the public response to it has been laudable. Nothing I say here is intended to undermine the generosity of those who stepped forward to donate. But I feel the aftermath exposes a worrying pattern.

A natural disaster befalls a certain part of the world, and people wring their hands over it. Soon this settles down and people devote their energies to providing aid: governments and NGOs swing into action, and the general public mobilise their best efforts to provide funds from T-shirts, concerts, bake sales and good old-fashioned donations. The sense of hopelessness is temporarily assuaged until news gets back that aid can’t get through: Haiti’s lack of infrastructure combined with the crippling effects of the quake mean that aid is blocked by sheer logistical difficulties—and we’re back to hand-wringing again.

Nobody likes to feel powerless. But if you want to prevent unnecessary deaths, then I have good news for you: it’s easier than you think. Around one million people die from malaria each year, and nearly two million from tuberculosis (TB). An incredible 36 million die each year from causes related to malnutrition (this number very likely overlaps with the numbers for malaria and TB). The vast majority of these deaths are preventable, and at surprisingly low cost, since the limiting factor isn’t the Herculean logistical task of getting emergency aid in through narrow transport channels already at maximum capacity—it’s simply lack of funds.

Haiti may not be getting all the help it needs right now, but it’s quite possibly getting all the help it can handle for the moment. Meanwhile, poverty hasn’t let up elsewhere in the world, and by the third day of the Haiti relief effort an equivalent number of preventable deaths had happened elsewhere in the world.

I don’t pretend to have any answers to global poverty, but I’d like to suggest two questions that could usefully guide our human desire to make the world a better place. Firstly, how can I target my giving to optimise the amount of suffering saved in the world? Secondly, how can I target my giving to be sustainable and continue to pay dividends over time? If we can satisfy these, then I don’t see what more can be asked of us.