Why you should care about functional programming

Microsoft has spent a lot of time and effort developing its main functional language, F#. Over time, it has been given many cutting-edge features and tooling, far better than anything available in any other language on the .NET platform.

This has got the software development community excited and the official Visual Studio Magazine article where the announcement was made soon became one of the most visited pages on the magazine’s website. This is yet another proof that there is an interest in functional programming.

There are skeptics out there who would say that there is no real demand for programming languages that were primarily intended to be functional. To some extent, they are right.

Although there are uses for certain concepts from the functional programming paradigm, most of those can be implemented by any popular programming language, such as JavaScript and C#. Also, functional languages, such as F# and Scala, are nowhere near as popular as C# and Java, so some developers spend their entire careers without having to use any of those languages first-hand.

However, there are some very good and pragmatic reasons why functional languages are worth learning. Let’s dive into them.

If you have an electric screwdriver, why use a manual one?

There are scenarios where functional programming beats any other programming paradigm outright. Any problems that are most easily solved with the help of mathematical functions fit under this category. In computer science, such problems include machine learning, statistical analysis, and any scientific application. Any software that outputs complex graphics also can take advantage of functional programming.

However, some critics will argue that most of the popular modern languages, while not primarily intended to be functional, still allow you to write software in a functional way. C#, Java, and JavaScript, among many others, have been evolving to embrace more and more tools and techniques that have previously only existed in “pure” functional languages. Those include lambda expressions, function pointers, and the ability to assign functions to variables.

So, if the languages that virtually any programmer is familiar with already allow you to write your code in a functional way, why bother learning a language that was specifically intended to be functional? Well, the best analogy is the difference between a mechanical and an electric screwdriver. Using a general-purpose language for functional programming is just like using a manual screwdriver while using functional language for the same purpose is like using an electric one.

Both tools can do the job. However, the power tool will do the job much faster and you won’t have to tire yourself out. For a small job, any of the tools will do. However, as the job becomes bigger and increases in complexity, eventually there comes a point where using the manual screwdriver will no longer be scalable.

What makes functional languages better

Functional languages have been designed in such a way that very complex expressions can be represented by only a few lines of code. For example, a function written in C# would look like this:

public static int SumOfSquares(int n)
    int sum = 0;
    for (int i = 1; i <= n; i++)
        sum += i * i;
    return sum;

The equivalent in F# would look like this:

let sumOfSquares n =
    [1..n] |> List.map square |> List.sum

It’s possible to do something slightly similar in C# by using lambda syntax and LINQ, but it is harder and the results will be more verbose and less elegant.

Not having to write as much code means that there is a reduced risk of introducing bugs. Plus, who would want to debug an excessively complex function that contains several potential points of failure?

Functional languages allow you to write functions as first-class members without the need to define containing objects first like it’s done in object-oriented languages. This allows you to easily unit-test the functions individually, often without even having to compile your code. In the .NET world, for example, F# is the only compiled language that allows you to test your functions interactively as you type your code.

As functions can be defined independently of any objects, statelessness is another aspect of functional programming languages. This means that a given function is guaranteed to return a specific output value if specific input values are used, regardless of how many times you call it.

When a function is a member of a stateful object, you cannot be sure whether there are any private variables defined within the object that interfere with the logic within the function. So, if not enough attention was paid to the object’s behavior during the development stage, a bug may pop up if a value that is imputed into the function in production is well outside of the range of values used during testing.

Functional languages generally compile into the same low-level instructions as some general-purpose object-oriented languages from the same family. Scala compiles into JVM bytecode, just like Java. F# compiles into MSIL, just like C# or VB.NET. However, it is much easier to make functional languages faster than their object-oriented counterparts.

We have already covered the fact that, as functional languages are concise and much more easily unit-testable, they significantly simplify the process of writing code that contains some very complex calculations. As well as the conciseness of the code also makes it much easier to write the code in such a way that it compiles into a faster executable.

A relatively low popularity of a language is its advantage

There are developers who have never encountered a valid use case for a language with “pure” functional programming features in their careers. However, this is not because these languages are hardly ever used by anyone. The real reason for it is that the bulk of software companies just don’t make software that performs any particularly complex calculations.

Yes, if the company you work at makes a content management system, a mobile utility app, or an e-commerce website, using a functional language may be overkill. It will also be harder to find developers for such a company, as the knowledge of, for example, F# is much less widespread than the knowledge of C#. However, if your business is related to gaming, fintech, artificial intelligence, or mixed reality, knowing functional languages is an absolute must.

Even though the businesses that specialize in software that relies on complex calculations are nowhere near as widespread as the ones that build relatively simple software, they are not exceptionally rare either. Also, as the skills pool that such companies can draw from is relatively small, while the demand for the cutting-edge technologies that they build is high, functional programmers tend to earn well above average in such companies.

So, if you assume that learning a functional programming language will not give you any advantage in your current company, you are probably correct. However, if you choose to become one of the few functional programming gurus, it can open some opportunities for you that are both really exciting and financially rewarding.

Wrapping up

The main advantage of functional programming languages over conventional general-purpose object-oriented ones is that the former will allow you to write software that is capable of performing very complex calculations much quicker. The majority of software houses build relatively simple software, so they don’t tend to have any specific demand for functional programming languages, especially as the knowledge of such languages is not very widespread among developers.

However, there are enough companies that produce cutting-edge software that can only be effectively built with the aid of functional languages. Those are the business that will reward you better than the rest. So, getting familiar with F# or Scala will open some exciting opportunities for you.

P.S. If you want me to help you improve your software development skills, you can check out my courses and my books. You can also book me for one-on-one mentorship.