If you are a software developer and develop in object oriented programming languages, such as Java or C# and want to start developing in a functional programming language like Scala, Python, Clojure, then this blog is for you. Functional programming is a programming paradigm based on mathematical functions. It avoids change of state data. It is also recommended to implement immutable values. Its popularity has increased significantly over the past few years.
To help introduce the topic, let me share some simple examples where we can see the difference between both programming paradigms:
The above example shows how to achieve the same goal with both programming languages. It demonstrates how with functional programming languages, you need fewer lines of code.
I’ve been programming in Java for more than 5 years, even though I wanted to learn a new language and a new way to program. The reason I decided to start learning to code in Scala, is that it is similar to Java in some ways, but quite different in others. After struggling for some weeks to try to make new and different code, than that I used to make with Java, I decided to start from the beginning, as if I had never coded in another language. These are my top pieces of advice that I can give you if you decide to migrate from Object Oriented Programming to Functional Programming.
First of all, and most importantly, you have to realize you are going to learn a new language, a new way of thinking, and a new approach to facing problems, and therefore their solutions. This will require effort and time.
Thinking about functional programming is a different world, compared to imperative programming. Functional programming is based on: function composition, immutability, declarative programming and recursion. On the other hand, you have object oriented paradigm which is based on: mutability, changing state, objects. As you can see, they are two totally different ways of thinking and the best way to migrate from one to another is to begin thinking functionally, learning from zero, and that means forgetting about objects and polymorphism! Some functional programming languages can be used as object oriented programming languages as well, i.e. Scala and F#, and in these cases, try not to think or use concepts used in Java and C#. By avoiding object oriented paradigm concepts, you will make your code pure functional.
It is probably sufficient if you read two good books about functional programming in the programming language you are learning. But it would be better if you first get an additional book about functional programming itself, i.e. Functional Thinking from Neal Ford. In case you are learning Scala, as I did, I can highly recommend Functional Programming in Scala and Scala in Action, as there are plenty of interesting things you can learn with these two books.
Thinking functionally may be hard the first time, but with practice it will become normal and you will get used to it. Doing exercises and having someone who can guide you through the learning process will definitely help you grow faster. Solutions to problems can be written in many ways, but having the mentorship of someone who is really good at what you are doing, brings you the benefit of learning how to do it the best way, even if it’s really difficult to get there at first.
These four pieces of advice helped me learn to code in a totally new way, getting rid of that object oriented code I first wrote in Scala. I hope you take advantage of them and they work for you.
February 13 / 2020
December 17 / 2019
Digital technology enables organizations to boost productivity and better engage with customers. However, many technology initiatives continue to see cost overrun, damaging the bottom line and undermining the value...Read post