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.
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.
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 specialise 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.
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.
Written by Fiodar Sazanavets
Posted on 17 May 2018
Daniel 19 May 2018
I know F# is awesome and i love the language, but why did everybody use this C# examples which felt 20 years old? LINQ is a few years old.
public static int SumOfSquares(int n)
public static int SumOfSquares(int n)
or even shorter.
public static int SumOfSquares(int n)
Don't get me wrong. F# is awesome, but also as a C# developer those example do not convince me at all. The discriminated union and the pipe operator does the trick.
Alan 20 May 2018
C# got these ideas from functional programming and F# in several ways lets you take this further. If you like using Linq queries in C# you will really love F#.
Daniel 20 May 2018
@Alan I already love F#. But in the business C# is more common, unfortunatly. I know, that it came from functional languages. But if a C# programmer sees this example, maybe she or he thinks, what? We don't use this constructs any more. That was my first thought, when i cam first in contact with F#. I thought: "WFT? Nobody use this any more!"