This website uses cookies on its adverts and sponsored links. By clicking the "Accept" button you are consenting to their use.

Read more

Accept


Mobile Tech Tracker
≡ sections
Home

Tech Advice

Tech Thoughts

Apps

Tech News

About

Welcome to Mobile Tech Tracker. Our mission is to help technically-minded people to become better versions of themselves and to help ordinary people to use modern smart technologies to their own advantage. If you want to support us, please consider visiting the pages of our advertisers.


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:

C# Example

The equivalent in F# would look like this:

F# Example

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.


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.



Written by

Posted on 17 May 2018

Fiodar Sazanavets is a full stack software developer with several years of experience working in various industries. In his professional career, he has mainly worked with a number of different Microsoft stack technologies, both back-end and front-end, and Java for Android. Fiodar has an Honours degree in Environmental Biology and a Masters degree in Environmental Informatics.

Comments (1)

Author's Name *

Email *

1 + 7 *

Comment

*



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)

=> Enumerable.Range(1,n).Select(x=>Square(x)).Sum();

or shorter

public static int SumOfSquares(int n)

=> Enumerable.Range(1,n).Sum(x=>Square(x));

or even shorter.

public static int SumOfSquares(int n)

=> Enumerable.Range(1,n).Sum(Square);

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.

Reply

Author's Name *

Email *

1 + 9 *

Comment

*


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!"


More from Tech Advice


Building .NET Core audio application - part 1

Building .NET Core audio application - part 1


Building .NET Core desktop application

Building .NET Core desktop application


How to play sound on .NET Core

How to play sound on .NET Core


How to protect your website from spammers

How to protect your website from spammers


Proven way to make programming fun

Proven way to make programming fun


What the heck is WebAssembly

What the heck is WebAssembly


Desktop apps are not dead. Here is why

Desktop apps are not dead. Here is why


Popular misconceptions about Node.js

Popular misconceptions about Node.js


Becoming a software developer is easier than you think

Becoming a software developer is easier than you think


Why every web developer should know Node.js

Why every web developer should know Node.js


Share this:

Facebook Google LinkedIn Twitter Become a Patron!


More from Tech Advice











Privacy Policy

© Mobile Tech Tracker. All rights reserved. Unauthorised copying of any of this website's content is prohibited under international law.

For any queries, comments or suggestions, please write to info@mobiletechtracker.co.uk.