Donnerstag, 18. Oktober 2012

Functional Programming Misunderstandings

Functional Programming Misunderstandings

While talking with friends and colleagues I noticed that there are (at least)  three widespread misunderstandings associated with the term "functional programming". In this post I will state those misunderstandings and explain for each one of them, why it is not true.
  1. The primary utility of functional programming is the ability to pass functions as parameters to other functions (higher order programming; functions as first-class citizens).
  2. Functional programming cannot be achieved in imperative programming or object oriented programming languages such as Java, Python or C.
  3. Functional programming is just of academic interest and quite useless in practice for its inability to manipulate state.
The following paragraphs explain what is wrong with the statements above.

Primary Utility of Functional Programming

While passing functions as parameters is very beneficial to write clean and short code by using higher order functions such as fold,  filter and map [2] known from functional languages such as Haskell or Scheme, it is not the most important aspect of functional programming.

Instead the most fundamental and beneficial part of functional programming is the isolation of side effects and the explicit flow of information over function parameters and function return values. This isolation of side effects makes code easier to understand, refactor and maintain. Moreover it allows for easier parallelization, which cannot be overestimated using the emergence of multicore processors and multi-machine clusters over the past years. This isolation of side effects also reduces the use of global variables, which are considered evil when they are used in cases where local variables can be used [1].

Functional Programming in Non-Functional Languages

While most imperative and object oriented programming languages, functions cannot be used as values (parameters and return values of other functions), higher order programming can be mimicked using function objects, i.e. objects with only a single method. The Guava Java library [5] adds support for higher order programming by adding function objects, and custom implementations for map and filter.

Yet not only higher order programming can be achieved by using function objects but side effects can be isolated just in the same way as functional programming languages enforce isolation of side effects by not supplying a global state for a program under execution. By reducing the number of functions that modify their parameters or modify global variables, we can make programs just as easy to refactor and reason about as programs written in functional languages are.

Use of Functional Programming in Industry

While originally invented as a research language, Haskell has gained significantly more users in industry over time. The Haskell Wiki has an impressive list of applications that were written in Haskell [3]. Also functional programming (i.e. reduction of side effects, use of pure functions) in imperative or object oriented languages, and using higher order programming in Ruby, Javascript, Scala, etc to remove boiler-plate code is getting more and more popular since the advantages of this coding style have been accepted by mainstream programmers.

References



Keine Kommentare:

Kommentar veröffentlichen