Trying out Functional Programming: Episode 1

I have started looking into Functional Programming as a potential saviour of software quality in general and scalability to multi-core, multi-processor machines in specific. Much of what I have read so far suggests that Imperative and Object-oriented Programming do not work well when formal methods of quality assurance need to be applied. Beyond that there have been 30 years of nearly fruitless efforts to auto-parallelize code written in those styles.

Functional Programming seems to offer the promise of trivial support for testability and concurrency. Both of these are enabled - so far as I understand it so far - by the absolute enforcement of side-effect free code. In other words a function is guaranteed absolutely - in a mathematically provable sense - to return the same value for the same arguments. This makes 100% test coverage possible in theory, and rock-solid range of expected input testing easy.

An imperative or OO program confounds even range of input testing since unrelated function calls can influence the output of the unit being tested. In the ideal OO program, good programming practice would suggest that this would not be the case, but in practice developers take short cuts to meet deadlines and the create cross-module stateful interactions for convenience reasons. In the ideal OO case you could test a module assuming that the output of a function can only be altered by other function calls to the module. That's the best case: a guarantee of needing to do stateful testing at the module level. At best that is substantially more complicated that testing individually idempotent functions. Functional Programming makes the functions the units and thus greatly simplifies the testing effort.

Automatic Concurrency is made possible because the standalone nature of functions makes it possible to trivially distribute them. A smart compiler is able to leave a program semantically unchanged while automatically determining and distributing the independent function calls across processors or cores. I am in way over my head as far as the theory goes here, but the idea of writing a program in a form that describes what I want and letting the compiler do the work to achieve it is very compelling.

For learning purposes I am focussing on Haskell, since it has a great tutorial , a decently large user base, and is a pure functional language. As I keep digging in on Haskell I will post my thoughts. Stay tuned…