Why you should think about functional programming.

Hi, Habr! I present to you the translation of my article “Why you should think about functional programming” , dedicated to functional programming.


Why should you think about functional programming? Let's answer the following questions:

  • Are your projects always completed on time?
  • Did users have any complaints?
  • Project support never took a lot of time?
  • New functionality always fits well with the existing architecture?

If the answers to all the above questions are positive, you do not need to change anything, your team is a rare example of harmonious personnel, methodology and tools. Otherwise, you should be open to new approaches to solving your problems, including a critical look at the technical tools and programming languages ​​used.

First look

As you know, programmers are extremely creative people, but at the same time they tend to follow certain trends when choosing a programming language. Among the vast number of languages, functional languages ​​are increasingly being acquired by fans and are increasingly being used by companies around the world. Using functional languages ​​can significantly increase the productivity and quality of programmers. Naturally, this depends on the combination of tasks, language and programming skills. In this case, the programmer simply describes what he wants, instead of listing the sequence of actions necessary to get the result. Thus, the developer focuses on a high-level “what is required” instead of getting stuck on a low-level “how to do it”.

Functional programming is the style of writing programs by compiling a set of functions. Its basic principle is to wrap almost everything in a function, write a lot of small reusable functions, and then just call them one by one. In addition, the structure of these functions must comply with certain rules and solve some problems.

So, if any task can be solved by combining calls of several functions, then:

  1. How to implement if-else conditions?
  2. How to solve tricks with Null Exception?
  3. How to make sure that the function is really "reusable" and can be used anywhere?
  4. How to make so that the data transferred to our functions, have been changed and could be used in other places?
  5. If a function takes several values, but when chaining you can only transfer one function, then how do we make this function a part of the chain?

To solve all these problems, functional languages ​​provide tools and solutions from mathematics, such as monads, functors, and so on.

The benefits of functional programming have long been recognized by the general public. Successful software development often comes down to the maximum simplification of existing mechanisms that allow new applications to adapt to the requirements of modern users. And at the same time, we must hurry, having managed in a short time to present products to customers with unlimited possibilities. Changing requirements are much easier to follow when developed applications can be divided into several simple functions that are easy to verify. In such algorithms there are no tricky side effects and abstract formulations intended for results on a global scale.

So why do experts miss functional programming for so long? And why is this becoming so common today?

Where is functional programming used in the real world?

Since functional programming is primarily an approach to writing code, you can use its principles in any language. However, there are languages ​​specifically honed by a functional approach. More modern functional languages, such as Elm and Elixir, according to GitHub and Stack Overflow, are gradually and surely gaining popularity. The growing popularity of JavaScript has also led to increased interest in functional programming concepts for use in this language. In addition, experienced developers in the field of functional programming subsequently began working on Single Web Application – SPA environments, and as a result we have Redux, React, MobX, and other libraries used by millions of people.


Examples from real life:

  • Apache spark
  • Scalding (from Twitter)
  • Apache kafka
  • Finagle (from Twitter)
  • Akka
  • Autocad
  • emacs (LISP)

So what is functional programming, where did such a boom come from and why is it worth considering? Let's see.

About functional programming

A few years ago, only a few experts had an idea of ​​functional programming, but over the past three years, almost every code base of large applications actively used ideas taken from the world of functional programming. And for this there are objective reasons:

  • functional programming allows you to write more concise and predictable code
  • it's easier to check (although learning from scratch is not easy)

Key features of software development using functional programming

  • Pure function is very simple. It must always return the same result. For the same values ​​of X and Y, we always get the same function result. Predictability plays an important role during program operation in functional programming.
  • Avoid general health, changeable data and side effects;
    An immutable object is an object whose state cannot be changed after its creation. Immutable objects are more thread-oriented than mutable objects. If the function does not work as predictably, it will lead to undesirable side effects. In imperative languages, a function in the implementation process can read and change the values ​​of global variables and perform input / output. Therefore, if we call the same function twice with the same argument, it may happen that we get two different results. This feature is called a side effect.

    Functional programming helps us write flow-oriented code.
  • Prevalence of declarative, not imperative approach.

Composition of functions

Functional composition is a functional programming approach that includes calling some functions as arguments to others to create a complex composition of simpler functions. This layout method allows you to take two or more simple functions and combine them into one more complex function that performs sub-functions in a logical order with any data. To get this result, you put one function inside another and perform operations with external functions on the result of the internal function until you get the result. And the result may be different, depending on the order in which the functions are applied.

As a result, with a different logical order of calling pure functions and the same argument value, we get more complex functionality that gives us the desired result and makes it predictable.


Benefits of Functional Programming

Functional programming helps make the code more understandable, predictable and easy to read. Using the principles of functional programming helps to get rid of unnecessary abstractions with unpredictable behavior, so make the program more predictable and reduce the number of possible errors.

In functional languages, functions can be passed to other functions as an argument or returned as a result. Functions that take functional arguments are called higher-order functions or functionals. The most popular functionality is the map. a map that applies a certain function to all elements of the list, forming another list from the results.

In purely functional programming, the assignment operator is absent, objects cannot be changed and destroyed, new ones can be created only by decoding and synthesizing existing ones. The garbage collector built into the language will take care of unnecessary objects. Thanks to this, in a pure functional language, all functions are free from side effects. However, this does not prevent this language from imitating some useful imperative properties, such as exceptions and mutable arrays. Of course, there are special tricks for this.

Lack of functional programming

  • One of the problems with functional programming is that it is different from what you already know. You must re-learn much of what you already know in an imperative setting! Many experienced developers who already know what they already know are not going to solve problems differently. It takes time and effort to think differently.
  • It’s easy to write clean functions, but merging them into a complete application is where things get trickier.
  • Big problem with predictable performance. Using only fixed values ​​and recursion can potentially lead to performance problems, including the use of RAM and speed, so most functional languages ​​are not particularly good choices for soft or hard real-time systems or embedded computing.
  • Functional programming tends to write code in a too abstract form when the programmer himself no longer understands what he wrote after a while.
  • Pure functions and I / O don't really mix.


If it seems to you and your development team that your coding style does not give you the necessary level of control over the complexity of the business area, cluttering your code with unnecessary syntax junk, which leads to a loss of time, try functional programming. Thus, in areas related to a large number of calculations or data transformations, parallel / asynchronous programming, you can get significant advantages by immersing yourself in functional programming. By developing your solution using the functional programming paradigm, you will accelerate the entire development process faster and faster, becoming more experienced. It looks like you are driving your car in high gear, compared to how imperative programmers move.

Writing code in a functional language gives you the opportunity to look at the problem from the other side, where the development of your solution can be more efficient. And it will simply increase the number of ways to express your ideas.

Functional programming can change your coding style for the better. But mastering it is quite difficult and time consuming, and many posts and tutorials do not take into account details (for example, monads, applicative functors, etc.) and do not provide practical examples that would help beginners to use powerful functional programming methods every day. But you can bring already experienced programmers to the team, which will save you time, effort and money.


If you do it right, the result will be clearer, more concise and you will have a readable code. Isn't that what we all want?

Roman Taluev,
Software Developer at Quasarbyte

Also popular now: