Saturday, September 29, 2012

Its templates all over again!


Many years ago I worked in the world of C++ developer tools and had the job of creating libraries that some other poor sod had to buy, understand and use. Through many years of doing this, I came to a few conclusions about how reusable architectures should be created and the sort of programming techniques that should be used.

Sort of in the middle of my time as a tools developer, ATL, which, if you don't know it, is a system based upon the principles of C++ templates that essentially rely on massive amounts of multiply inherited classes. Its a very "clever" way of programming to be sure but is incredibly hard to read and trying to follow program flow within a templated class is a nightmare.

I think that Anders Hjelsberg actually had recurring nightmares about C++ templates and so invented C# which is singly inherited apart from interfaces and is generally straightforward to work with and eminently readable.

One of my colleagues was a template guru. Lovely fellow, very productive and wrote nice tools but having a style of programming that just made me wince every time I read through his code. It was so esoteric and overly templatised that the whole lot of it just became a blur. The customers had a hard job understanding it too.

So, years pass and Anders decides to add some nice little language features to C#. Features that start off with generics and go on to anonymous methods, lambdas and so-on.

Today I've been programming an Android application in C# using Monodroid from Xamarin and I suddenly realised that my classes only have one method. All the event handling, asynchronous calls, and even marshalling back to the UI thread is done with lambda expressions. I am horrifically reminded of templates just simply because trying to follow the program flow through an activity that uses reverse-geocoding via Google's map APIs, progress bar indication on the phone screen and reporting to a web-service of user actions puts me in mind of the code complexity of C++ templates.

I have become my own worst nightmare!

No comments: