Programming Languages: When Functional Programming Is the Best Choice

Jan 16, 2020

Where functional programming is used

This article won’t tell the programmers why they should quit using imperative languages and go and study some Lisp. Neither it will tell the PMs how wrong they have been all this time turning a blind eye to the great perks of Haskell. Rather, in this piece of writing I will share with you the facts on the applicability of functional programming and when it’s better for specific tasks than an imperative alternative.

I have always joked that a good way to run a project in finance is to write it in haskell,
the chances of finding a terrible haskell developer are rather low.
A piece of wisdom from the vaults of Quora

Before we find out why Functional Programming (FP) is so on today, let’s get to know how FP got its ticket to the World of Big Programming. The problem it was intended to solve will give us a clue why it’s better for solving specific issues.

Flashback

Functional programming owes its appearance to Alonzo Church, an American mathematician and logician. Back in 1920-s, he attended Princeton University where in 1924 he was awarded a Ph.D. in mathematics. An exceptional student, Alonzo was obsessed with abstract mathematical puzzles. What problems a machine with an infinite computational power would be able to solve? Would all sorts of problem be resolved automatically by a machine like that? Why a supermachine would be able to solve one sort of problems and couldn’t deal with another? – these, mainly philosophical, issues preoccupied his mind. Thus functional programming was born – with no physical limitations that could restrain the scientist’s imagination. Generally speaking, FP was intended to solve computational problems to make superpowerful machines work that yet didn’t exist at that time (and are unlikely to appear even in the nearest future).

Why functional programming is so great?

As you may guess, programmers wouldn’t write in a functional language only because a smart guy once developed it for a non-existing supermachine. There should be rock-solid arguments in favor of FP. The main of them is that FP is actually an ideal way-out for lazy programmers. Contrary to imperative languages where a programmer uses actions and sequences of actions within a program, functional programmer applies functions. It means that a functional programmer tells the computer what to do, without specifying how to do it and in what order. Supermachine could probably deal with the sequence itself, Alonzo presumably thought when creating FP.
What are other specific perks of using a functional language? See them below:

  • Shorter code containing fewer errors (4 to 10 times shorter than in the case with imperative programs). This is possible due to high-level abstractions, which allow hiding a lot of details of iterating or other routine operations
  • Easier unit testing. All you need to do is pass arguments that represent edge cases. If every function in your program passes unit tests you can be a lot more confident about quality of your software than if you were using an imperative language (Defmacro)
  • FP makes bug reproducing a piece of cake, as bug doesn’t root in the unrelated code strings which were executed earlier. On the contrary, in an imperative program, a bug may show up in time and to find it you’ll have to go through several steps that are not actually related to it
  • Code proofing and optimizing through mathematical reasoning reinforce security for mission critical industries and beyond. (See more FP benefits in detail here).

Application field

Where can one apply FP? It is considered that, as FP comes from academic circles, it’s used mainly for academic/scientific purposes. But, FP is widely used in many commercial projects pertaining to Big Data or Blockchain areas due to the fact that functional programs are parallelizable, which guarantees fast data processing. Along with that, exact the same reason – fast processing of vast data amount – make FP an ideal solution for AI and IoT cases.
Another use case refers to message processing. WhatsApp makes use of Erlang, a programming language following the functional programming paradigm, to enable its mere 100+ employees to manage the data belonging to over 1.5 billion people (Hackr).
Facebook uses Haskell, another widely-known functional language to fight the spam landslide.

FP has found its way into finance, too, as OCaml, Clojure, F#, Haskell and Scala are widely applied in concurrent computing (parallel processing mentioned above) in this industry.

Wrapping it up

As FP was intended for a supermachine with extreme computational powers, no wonder it allows solving extremely complex tasks in a clear, simple and laconic way. Does it mean that one should abandon imperative programming starting right now and indulge into FP? Surely, nope. Imperative languages suit better a whole lot of purposes. For example, no functional language will do better than C at system administration, drivers or computer games with high-quality graphics. And, frankly, guys, since imperative languages allowed us virtually do anything, we can’t say that FP is better, in general. We only can state what functional languages can do some operations better so far. Luis Atencio, author of Functional Programming in JavaScript, helped me in listing exactly when FP is better than imperative languages:

  1. Program verification and correctness, as the programs written in a functional language are closer to the underlying mathematics.
  2.  Communicating intent, as “…the best solution to almost any problem follows from a mixture of how you decide to evaluate (compose) higher-order functions”, Luis states.
  3. Straightforward compatibility due to a clear understanding of artifacts.

See the short summary of the FP applicability in the picture below.

What functional programming is good for

If you would like to harness the above perks, surely, you should give FP a try. Most definitely, for those starting to learn FP from scratch, it can be a challenge, as FP resembles so much good old math classes most of us were happy to skip. Maybe because of this restraining factor FP is still shroud in mystery, despite the fact that it was developed almost a century ago. Nevertheless, FP will definitely raise your qualifications to the new heights and open the doors that were even no use knocking at. Are you ready for a new breathtaking round in your career development?

Need more professional advice on programming? Contact our Support team or visit our website for the details on our projects, best practices and recommendations.

BadPoorAverageGoodExcellent (No Ratings Yet)
Loading...