Wednesday 1 September 2010

Why Every Developer Should Learn Functional Programming

Nineteen years ago I started my degree course. Our first term of programming was spent learning functional programming using the Miranda language (from which Haskell is derived). At the time I remember most people on the course, myself included, being puzzled by this approach. General consensus was that it must be being done to create a level playing field by invalidating any previous programming experience. When the term was complete we moved on to other languages: first Ada, then C and finally C++. Functional Programming was all but a distant memory.

Many years on (and a number of languages later) I'm now finding those early forays into the Functional world to be of more and more value.

A couple of years ago I learnt Ruby. With support for closures and being able to pass blocks as parameters to methods there were already some functional concepts in play. Now my language of choice is Scala (a hybrid object-functional language) and I'm finding myself developing more and more in the purely functional style, writing much less imperative code.

In fact I've recently dug out my old college notes on Miranda and read some books on Haskell. I've also worked through some examples and prototypes in Haskell in order to bring my Functional Programming skills back up to scratch.

I was amazed at how quickly I dropped back into the Functional style. It got me thinking and I looked back over many years of code. I was surprised to find that it had never really gone away! Even after many years of Java and C++ programming I could still see a heavy influence of Functional Programming in the code I had written and am still writing.

For example, I've always been a great user of immutable data and a far greater user of recursion than my peers. I've also always made regular use of passing functional objects as parameters to methods as a way of separating application specific behaviour from generic algorithms. Those tendrils of my early programming education were all over many years of code!

Then another thought came into my head. Project metrics have always shown that my defect rate is significantly lower than my peers. I'd always put this down to my attention to detail and thoroughness of testing. Could it in fact be the influence of the Functional style on my code? Code written using Functional Programming languages is known to be easier to reason about, and thus less bug prone, mainly due to its lack of side effects. By following elements of this style in my imperative language programming have I bought some of this benefit along with me? After thinking back over the years and looking at more code, I think that maybe I have.

Additionally, the approaches taught to us for Functional Programming were heavily based on a discipline of reasoning about code. Think about the inputs to the function; think about its outputs; think about all the cases to be covered; think about non-success cases; avoid side effects; create an uncomplicated solution and so on. Looking at how I write code today I can see this reasoning discipline present in my thought process. Comparing my thinking to other programmers that I know don't have a Functional Programming background I can clearly see the difference in the way I construct code to the way that they do. Could it be more than just Functional techniques that make the difference to my productivity and quality? I think perhaps it is. I think that that early discipline for reasoning about code and attacking problems with a Functional mindset really has made a difference to my ability to code. 

So, there we have it. Every developer should have a thorough grounding in Functional Programming. Not just because it's going to be vitally important in the future multi-core, multi-threaded, cloud based landscape. But because the influence of Functional Programming makes for better developers who are able to created higher quality code that is easier to reason about. Who would have thought that those University lecturers of 19 years ago would have got it so right. Thanks guys!

2 comments:

  1. Good view point Chris. Impressed with your analysis and writing as you always have been.
    Nanda
    (Nortel)

    ReplyDelete
  2. Thanks for the feedback Nanda. Good to hear from you.

    ReplyDelete