Why you should care about functional programming

While releasing version 15.7 update to Visual Studio IDE, Microsoft has announced that its main functional language, F#, has been given a whole new set of cutting edge features and tooling, far better than anything available in any other of its popular languages.

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 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 career 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 takes advantage of functional programming.

However, some critics will argue that most of the popular modern languages, while not being primarily intended to be functional, still allow you to write software in 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 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 few lines of code. For example, while a function written in C# would look like this:

The equivalent in F# would look like this:

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 .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 that fact that, as functional languages are concise and much more easily unit-testable, they significantly simplify the process of writing the 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.

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 career. 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 an overkill. It will also be harder to find developers for such 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. 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 the 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.

Leave a Reply

Your email address will not be published. Required fields are marked *