Wednesday, August 27, 2008

The Great Migration

We moved house 12 days ago, the house we were renting that was so great over summer turned into a cold and damp hole in winter. Moving is hard. My big mistake was assuming, given I knew there was an existing broadband connection at the new house, that it would be a simple task to switch my broadband connection from the old house to the new house. So after a week without internet connectivity and wanting to be connected to aid the Great Job Hunt, I was getting severe withdrawals and some filthy looks from my partner Anne who had earlier suggested I should perhaps make some arrangements to move the connection. Anne's a project manager in the real world.


At the same time I had ordered a couple of gigs of RAM (to better run Vista under Parallels) and a 7200RPM 320GB drive for my MacBook Pro from Other World Computing plus a copy of Leopard so I can run Boot Camp. Moving is hard. My big mistake was assuming I had a screwdriver that would be suitable for removing the screws from my laptop. After locating the screwdriver in the chaos of small children and boxes and establishing that it was not in fact suitable I had to wait another day before tackling the delicate task of dismantling my MBP. The screws aren't really a problem as long as you get all of them out, including the two torx screws hiding beneath the RAM cover. The problem is the clips and knowing when it's ok to keep pulling and when you should stop and have a look for those two screws you missed.


Ultimately it's all good and I'm enjoying the feeling of a lot of free space, much better performance, and a clean install. And with the restoration of our broadband connection I've been able to read an excellent article on scalability on InfoQ.

Tuesday, August 5, 2008

C# 3.0 Extension Methods

I'm getting stuck into C# 3.0 and really enjoying some of the new features. How often do you go looking for a class method to perform a specific task only to discover the method doesn't exist or exists but only does part of the job. Inevitably my programs contain methods I've created specifically to address this issue. One of the downsides of this approach is that there's often no obvious place to code the method, it just gets stuck close to where it is used or in a utility class if it is used in more than one location. And that is bad because it increases coupling/lowers cohesion for classes. Extension methods are a great way to address this situation.


I have an ASP.Net application that began life as a real application. The version I use to explore new stuff now bears little resemblance to the original but when I wrote it I was surprised to find that System.Web.UI.Control.FindControl() doesn't recurse down through the child controls. As a result I wrote the following method in the code-behind for a MasterPage:



private static Control LocateControl( Control Ctrl, string Id)
{
   Control ctrlRet = Ctrl.FindControl( Id);
   if (ctrlRet == null)
   {
      for (int i = 0; i < Ctrl.Controls.Count && ctrlRet == null; i++)
      {
         ctrlRet = LocateControl( Ctrl.Controls[i], Id);
      }
   }
   return ctrlRet;
}


Using extension methods I can now instead code a new class ControlExtensions:



public static class ControlExtensions
{
    public static Control LocateControl(this Control Ctrl, string Id)
   {
      Control ctrlRet = Ctrl.FindControl(Id);
      if (null == ctrlRet)
      {
         foreach (Control childCtrl in Ctrl.Controls)
         {
            ctrlRet = childCtrl.LocateControl(Id);
            if (null != ctrlRet)
               break;
         }
      }
      return ctrlRet;
   }
}

Calling the original method is clunky —


Control ctrl = LocateControl( parentControl, controlId);

when compared to the new extension method —


Control ctrl = parentControl.LocateControl( controlId);

Very nice.