What is Functional Programming and what does it do for you?

This is a good example here of a bad explanation of FP: basically saying that FP is intelligent whilst non-FP is dumb.

I recently read a couple of blog posts by Kris Jenkins which give his answers to the questions:

  • What is functional programming?
  • Which languages could be said to be functional programming languages?

I recommend you go read them if you have not already (here and here).

In summary, Kris’s answer to the first question is: functional programming is a style of programming which prefers pure functions over functions with side effects (or as Kris puts it well: side-causes and side-effects).

This corresponds closely to the first sentence of the definition in Wikipedia:

… functional programming is a programming paradigm … that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data.

What is the benefit of code without side effects?

The PyToolz package documentation has an excellent answer (see here):

Impure functions are often more efficient but also require that the programmer “keep track” of the state of several variables. Keeping track of this state becomes increasingly difficult as programs grow in size. By eschewing state programmers are able to conceptually scale out to solve much larger problems. The loss of performance is often negligible compared to the freedom to trust that your functions work as expected on your inputs.
Maintaining state provides efficiency at the cost of surprises. Pure functions produce no surprises and so lighten the mental load of the programmer.

Immutability = Keeping things close

Another common feature of a functional approach is that variables are immutable — you can set their value only once. Often the variables are set in the context of a ‘let … in …’.

When your data is immutable two things happen:

  • you tend to create many more objects on the fly,
  • some objects stick around for a long time.

This is great: the objects that stick around for a long time never change, and objects that you create on the fly were probably done quite near to where they get used, so you don’t find yourself going on some long hunt to find out how they got to be in the state that they are in.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s