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.

Follow me on Mastodon!