Originally I shared this translation to Syndicode blog.

This article is the translation based on the original material by Irina Linnik. I decided that these thoughts and considerations she wrote about can be quite useful for the English-speaking audience. Here you will find out some basic pros and cons of functional programming and why you should consider it.

Programmers are very creative people but at the same time, they tend to follow some trends in choosing their programming language. For instance, PHP is considered ‘lazy’ language, and JavaScript is ‘difficult predictable magic’. Nowadays, functional languages becoming more and more popular and getting the attention of world biggest companies. According to different analytics and a common evaluation of the popularity of languages on GitHub and Stack Overflow, the functional languages (like Elm or Elixir) slowly gaining the momentum. The enormous increase in the popularity of JavaScript also leads to increased interest in functional programming. In addition, developers with experience in functional programming subsequently began to work on SPA frameworks, and as a result, we have Redux, React, MobX and other libraries that millions of people use.

So, what functional programming is and why it became so popular? Let’s take a closer look.

Basics

It is worth to start from the start.

The JavaScript world is very active. A few years ago only a few developers had the idea of functional programming. But over the last three years, almost every large application code base was using the ideas from functional programming. And there are objective reasons for that:

functional programming allows you to write more compressed and predictable code, it’s easier to test (although learning from scratch is not easy).

The main distinguishing features of software development that uses functional programming (FP):

the combination of pure functions;

avoiding shared state, mutable data, and side-effects;

the prevalence of declarative approach rather than imperative approach.

Functional programming is essentially based on the fundamental, determining principles of the above. When you start with FP it is necessary to switch to the ‘academic’ mode and study all the definitions or terms that you will come across: pure functions, function composition, shared state avoidance, etc. After that, you will be completely different in looking at both the functional and the programming as a whole. Here the example of some terms:

Pure functions are deterministic functions without side-effects. A deterministic function means that it returns the same result for the same set of input values. For FP, the properties of such functions are very important. For example, pure functions have reference transparency — you can replace the function call with its final value, without changing the values of the program.

Function composition means the process of combining two or more functions in order to create a new function or perform calculations.

What is shared state? The main problem of shared states is to understand the effects of a function. You need to know the whole history of each variable that the function uses. Therefore, functional programming avoids shared states, instead relying on immutable data structures and pure computations to extract new data from existing ones. Another nuance that occurs when working with shared states — changing the order of functions calls can lead to the avalanche of errors. That is why avoiding shared states will help you to avoid this problem.

Immutability is the core of functional programming (don’t mix it up with constants). ‘const’ creates is used for variable’s name that cannot be reassigned after creation. But it does not create immutable objects. You will not be able to change the object to which the binding relates, but you can still change the properties of this object.

Side-effects mean that in addition to the return value, the function also interacts with an external variable state. Why FP avoids side-effects? Because in this way the effects of the program are much easier to understand and test. Haskell, for example, uses monads to isolate side effects from pure functions.

Why declarative approach?

The imperative approach follows the principle of workflow control and answers the question ‘how to do’. The declarative approach describes the data flow and answers the question ‘what to do’. In addition, the imperative code most often uses instructions (operators), and declarative relies more on expressions.

So, we seem to figure out what functional programming is and what you need to know about it. And before we move on to discuss its advantages, let’s go through the shortcomings. What is the essence of the stereotype that ‘functional programming is unnatural’.

Be the Code

Some programmers do still think that functional programming is weird. And that is somewhat true. Here’s the quote from the Internet that describes FP with the next words:

Writing a functional code is like writing the code backwards. And it’s more similar to solving the puzzle, rather than explaining the process to a computer.

In fact, this is rather a subjective attitude to the functional programming formed by people who do not want to spend enough time to understand the FP nuances.

Now we’ll move on to

FP pros and cons

Disadvantages

There is no efficient vocabulary for functional languages. Purely functional vocabularies work slower than the hash tables, and for some applications, this can be critical. Secondly, there is no purely functional weak hash map. Nevertheless, for most developers, this defect may remain unnoticed.

Functional programming is not suitable for algorithms in graphs (due to slow work) and in general for those solutions that for decades were based on imperative programming.

Check the lists of FP disadvantages on Quora and in the article by Alexander Alvin too.

Okay, the last point we mentioned is not very related to cons because we cannot blame functional programming for what it was not meant to be.

Advantages

Functional programming is famous for its high-level abstractions that hide a large number of details of such routine operations like iterating. This makes the code shorter and, as a consequence, guarantees a smaller number of errors that can be tolerated.

In the functional programming, there is a smaller number of language primitives. Well-known classes are not used in FP. Instead of creating a unique description of an object with operations in the form of methods, in functional programming, there are several basic language primitives that are well optimized inside.

Due to the language and structures flexibility, as a functional programming developer, you can bring the language closer to the problem. And not vice versa. In addition, FP offers some new and interesting tools for solving complex tasks that OOP developers often neglect.

Working with functional languages provides accurate and fast code writing, facilitates testing and debugging. You’re working with high-level programs, and the functions signatures are more informative.

You can find other functional programming benefits here.

Functional programming use cases

These days developers try to solve the problem of parallel processing and working with Big Data. By parallelizing the processing of these data you get the desired result quite fast. Plus, do not forget about decentralized (distributed) calculations like blockchain. Due to functional programming principles, the functional code is the most suitable for such computations. The use of all basic API techniques facilitates the parallel execution of the code and its support.

In the beginning, functional programming was used only for solving specific problems. But now it is applied even in classic projects created by large IT companies.

If you want to learn functional programming

Here you will find some tips that will help you to start with FP.

Relax. At first, it will be difficult. Put aside what you knew before and try to learn new approaches and principles. Start with a micro-tasks. Start with learning Haskell and then go to Scala (or F #) for better understanding the principles of functional programming. You may find these resources useful:

— The book Learn You a Haskell

— The book ‘Real-World Functional Programming: With Examples in F # and C #’

— Course Programming Languages, Part A (for studying the concepts of FP using SML, Racket, Ruby)

— Course Introduction to Functional Programming

— GitHub (for JS) + Awesome FP JS

Good luck!

Not so long ago I have published a short comparison of Elixir and Ruby. That might be a useful reading too!