Functional programming languages like Haskell are becoming increasingly popular in the coding world. Of course, if you’re a programmer, you’re probably already familiar with the concept of functional programming, it doesn’t matter if you have used a „pure“ functional programming language before or not. Your favorites like Java, Python, and C++ all support a functional paradigm, and chances are that you had to use functional concepts in your code before.
If you’re not quite sure what Functional Programming is, it’s basically a type of programming that relies on mathematical functions. If that doesn’t explain much, don’t worry, you’ll understand it better after you finish reading this article.
The concept of functional programming
You should look at functional programming as a way of thinking. Unlike with object-oriented programming, you’ll be dealing with functions rather than objects. Think of it as solving a mathematical problem of some sort.
The main concepts you’re going to be dealing with are pure functions, higher-order functions, and recursion. I’m not going to go much into detail when it comes to these, since there are many online resources you can use to find out more info about it. For example, I was impressed by this article by Gints Dreimanis that explains FP and its concepts in a simple, yet detailed manner. You can find many articles and video guides like this one, so there is no need for me to focus on detailed explanations of FP.
However, what I think is still important to note is that, when it comes to functional programming, the output depends mainly on the input arguments. If you, for example, have a function that calculates the sum of „a“ and „b“, and you input the same values for a and b you should always get the same results. So, you can see how it resembles mathematical functions, right? It’s pretty easy once you get used to this way of thinking.
There are many advantages of the functional approach. Even though it’s mostly famous for its front-end usage, you can still use it for back-end programming as well, if you’re skilled enough. If you want to be a full-stack developer you’ll have to master both functional and imperative programming approaches. So what are the main advantages of mastering functional programming?
Code written using this approach is very easy to read and memorize making it easier to write too. It’s easy to predict what goes into your function, and what its result will be. You’ll know exactly what your function can and can’t do. You won’t be dealing with states which makes it much easier to comprehend.
Functional code is easy to understand because pure functions will never deal with the outside world of hidden states. Most functional programming languages make it extremely easy to distinguish pure from impure functions. This makes the code very readable and clean, and that’s one of the most important things when it comes to coding.
Debugging is made easy
With pure functions, the debugging process is very simple. As previously mentioned, pure functions depend only on the user input values, so it’s really easy to trace any mistakes. You’ll only need to follow the values and you’ll know exactly where it all went wrong.
This is also super important for beginners, as it makes the development of programming logic much easier. You only have to worry about the errors in one particular function since the testing process is so easy. You’ll know what kind of output to expect, and that helps you in noticing errors more easily.
Parallel programming is a very necessary concept to master for anyone serious about programming. Still, it’s often described as „a programmer’s worst nightmare“. This is because it’s one of the hardest things to learn when it comes to programming. It’s super non-intuitive and hard to grasp, especially for a beginner. Trying to learn Java threads almost made me give up on the language!
Well, with a functional approach, parallelism is much easier to handle. This is mostly due to the characteristics of pure functions. Since you’ll be dealing with the user input almost exclusively, parallel programming becomes much easier to handle.
Functional programs only store values when you require them to do so. This means that you’ll avoid any repeated evaluation which improves efficiency.
This also means that working with infinite arrays and values is easy. Let’s say for example that you made a function that represents a list with an infinite number of elements. In functional programming, the list won’t be evaluated or calculated until you require a real value from it (for example an indexed element).
It’s a good way of making your program more efficient by avoiding unnecessary evaluations.
As many benefits as it has, functional programming comes with its own disadvantages as well.
Combining pure functions
Writing a pure function is fairly easy, especially if you’re into math and algebra. Still, combining those and „gluing“ them together to make a functional application is where it gets hard.
It requires a lot of knowledge and skill, so it may take some time to master.
The most complicated concept in functional programming is probably the concept of recursion. You won’t be able to use loops with functional languages, so recursion is the only method you have for iteration. It’s a bit complicated and it may feel a bit unnatural for some people. It takes a lot of effort to master, and it’s pretty counter-intuitive.
You’ll have to develop a completely different mindset and learn to think recursively. This is especially hard for people who are used to the OOP approach since they’ll be accustomed to loops.
Because of its fairly complex mathematical terminology, many people avoid getting into FP languages. It’s something that scares many people off from ever trying FP. It takes both time and effort to gain enough knowledge to understand all the different mathematical concepts, and many people find it to be way too complicated for them.
Still, as a programmer, mathematics should be your best friend, whether you like it or not. Programming and mathematics both have different logic to them, but they are still tightly connected.
Functional programming (or FP) is a programming paradigm that’s becoming increasingly essential to every programmer’s portfolio of skills. It has both its advantages and disadvantages, but it’s a good skill to have as it can make many coding problems much easier to solve.