I Am Not Myself


I Am Sold on Pair Programming

The benefits of pair programming keep presenting themselves to me the more I actively participate in the practice. One of the greatest benefits for me is the opportunity for accelerated learning and teaching.

A quick example While working at Russell Investments pairing with Trevor Redfern I was amazed at his masterful use of the linq method syntax to condense large block of code into tight succinct chained statements. I started to see alot of the code I wrote as manipulation of various collections of objects and my functions as a set of actions that funneled those objects into different applications.

At The New Gig That Shall Not Be Named, I have had the opportunity to pair with Robert Ream. His use of the linq query syntax is just as impressive and mind blowing. It is currently twisting my brain a bit and getting me interested in learning more about functional programming.

Let me give you an example. We have a function in our application that takes a 2-dimensional array and serves it up as an IEnumerable of some business object. This bit of code is near the interface between the .NET and the APL portions of our application so it has a bit of primitive obsession at these boundaries. This particular function had a bug in that it could not handle empty char multidimensional arrays.

My initial fix looked something like this. _AplData is a private member of the class that has this method and is stored as an Array and can actually be of type object[,] or char[,] or other types. Note that these are true 2-dimmensional arrays not arrays of arrays ala object[][].

public IEnumerable<BizObj> Rows()
  for (var i = 0; i <= _AplData.GetUpperBound(0); i++)
    var items = new object[_AplData.GetUpperBound(1) + 1];
    for (var j = 0; j < items.Length; j++)
    items[j] = _AplData.GetValue(i, j);
    yield return new BizObj(items.ToArray());

This code worked and passed our failing unit test, but I was unhappy about the creation and tracking of a new array for every object created. Robert Suggested we could refactor the method and make it a bit more functional like this.

public IEnumerable<BizObj> Rows()
  return from i in 0.Through(_AplData.GetUpperBound(0))
         let row = from j in 0.Through(_AplData.GetUpperBound(1))
                   select _AplData.GetValue(i, j)
         select new BizObj(row);

This is still blowing my mind today and really makes we want to dig into F#. So through two different experiences pair programming, I am successfully adding features, becoming a better more well rounded programmer and being inspired to explore new concepts. And that is just one benefit of the practice.

3 responses to “I Am Sold on Pair Programming

  1. Liam October 23, 2011 at 2:15 pm

    That syntax is similar to coffeescriprt’s array comprehension.nice.

  2. Michael Bradley October 26, 2011 at 4:26 pm

    I can see what .Through() does, but where does that method come from? Is that a custom extension method on the int (i.e., Int32) type?

    • Bobby Johnson October 26, 2011 at 4:48 pm

      Yeah, it’s an extension method wrapper around Enumerable.Range() Looks like this:

      public static IEnumerable<int> Through(this int startValue, int endValue)
                  return Enumerable.Range(startValue, endValue - startValue);

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: