[ Content | View menu ]

Functional Programming, Tests, and the Future

Mark Mzyk | February 21, 2008

Michael Feathers has an interesting post up on the Beautiful Code blog.  In the post he notes the seeming oncoming rise of functional languages and then bemoans this because they are not object oriented and therefore cannot be easily tested.

Michael feels that a strength of object oriented programming is its ability to overwrite methods so that a single method can be isolated and then tested.  He calls this ability a seam

I agree on this point.  It is easy to test well written objected oriented code that has loose decoupling.  Of course, poor code that is tightly coupled can be damn near impossible to test, but with enough refactoring, eventually anything is possible.

And now, the disagreement.

Michael thinks that because functional languages don’t have the same kind of seams that object oriented languages do, that in the long term, functional programs will not be maintainable.

I think this is very short sighted on his part.  If functional programing languages jump into the mainstream, it will be because they offer solutions to problems that today’s mainstream object oriented languages don’t provide or don’t provide well.  Concurrency, I’m looking at you.

Nothing is going to stop the adoption of functional languages if they offer better solutions than what is now available .  As the languages become ever more popular, we’ll see new ways of utilizing the languages emerge, including new ways to test them.  TDD and its like will not go the way of the dodo.  Instead, TDD and testing will evolve to fit functional languages, and new tools that facilitate using TDD and testing with functional languages will appear.

Just because there isn’t an easy way to test a functional language today doesn’t mean there won’t be one tomorrow.

Still, let’s stop a moment and humor Micheal’s assertion.  Suppose that an effective testing method is, for some reason, never developed.  Will the industry be worse off than it is today?  I’m going to argue no.  While it is possible that the issues testing and TDD solve might reappear, at the same time the issues that functional languages solve will be of great benefit to the industry.  It’s a wash.  The industry gains and loses in the worst case.

It is once the next crop of languages are developed that the real value will be seen.  Enough programmers will have been exposed to functional programming that its strengths will get incorporated into new languages that will also be created with easily testable frameworks.

In the end, every one wins.  The only real questions are how long will it take and what amount of pain might have to be endured to get there.  I can’t predict the amount of time it will take, but I don’t think the pain will be any worse than it is today.