Pattern Matching to the Rescue

Today I was pair programming with a colleague to add some new functionality to an existing, and slightly gnarly, codebase.  Whilst there we a found an existing class which we should reuse, if only it wasn’t so tightly coupled to another object.

It’s purpose was to filter out a lost of parent objects by a value in a child object. It would loop through the whole list of parents examining each child. We wanted to reuse the class, but we were only handling the child objects and creating a bunch of parent objects for this purpose would be incredibly wrong. We needed to refactor to decouple, but how?

IList filteredParents = _filter.FindMatch(parents);

Suddenly, a light bulb goes off in my colleague’s eyes and he proceeds to turn the problem on its head – pattern matching.

We refactored the filter class so that it accepted a single child object and returned a bool indicating whether the match was successful. Then, using Linq we applied this pattern to the list of parent objects to filter out those with matching children. It was beautiful.

IList filteredParents = parents.Where(x => _filter.FindMatch(x.Child));

We can now reuse the filter class on our list of child objects without a need for the parents.

I realise that this is normal practice in the functional world, and I’ve toyed with it before, so I’m disappointed that I didn’t figure this out sooner, but it’s amazingly simple when put into action.

Roman Numerals Kata

I figured I’d start doing some of the coding Katas, but to add a small twist, I’d only do them in MonoDevelop on Ubuntu in order to get more familiar with Linux and Mono.

My attempt can be found here on github: https://github.com/ChrisAnn/Roman-Numerals-Kata

I have to say that I found some of the MonoDevelop defaults to be quite irritating.  I prefer the Allman style when it comes to the placement of braces in C#, while MonoDevelop defaulted to K&R style automatically moving my braces and would not allow me to manually alter their position.  I don’t mind that it defaulted to something I’m unaccostumed to, but I found the fact that it ignore my repeated hits on the return key to move it down a line to be extremely frustrating – if you’re going to move my braces, at least let me move them around to my liking!

With some digging around in the preferences, I managed to set up my own custom profile and it finally ceased tampering with my braces. (Edit > Preferences > Code Formatting > C# Source Code > C# Format > Edit > Change ‘Category’ to ‘Braces’ > Change ‘Method Declaration’ to ‘Next Line’.  Simples…)

Another thing I did not like was its habit of inserting spaces between the method name and the brackets to look like:

myMethod (parameter);

This is non-standard in C# and I had to go back and remove all the spaces, at least it let me do this without me having to amend my custom profile.

These complaints may seem trivial, but suppose I was to work in MonoDevelop and the rest of my team used Visual Studio, we’d create messy files with competing formatting – preventing this is why coding standards are employed.  Surely, MonoDevelop should default to the standards of the language being used?

I had no idea how to run my unit tests in MonoDevelop – I wasn’t expecting a test runner, there isnt one in VisualStudio unless you pay for the higher levels, but I was stumped until I discovered that you can install the NUnit gui via apt-get using the following command:

$ sudo apt-get install nunit-gui

I also made a point of not checking any existing completed roman numeral katas, but having looked a couple after I completed it, it seems that that I arrived at a solution very similar to that of other people, which gave me a good sense of validation.

I shall endeavour to complete more katas using MonoDevelop on Ubuntu as I found it to be a fun experience.