From OOP to Functional programming, how hard is it to act by need

functional programmingFor those who have been programming in an imperative style for a while, functional programming may look scary at first. Indeed, it requires a different way of thinking. However, for those who has the motive to learn, it has never been “a question of how”. The real question in my opinion is “why functional programming”. First let’s discuss the shift towards functional programming in the last couple of years and why its happening now:

 

 

The shift is real but is it functional?


Many programming languages have already adopted some functional features. We haven’t yet reached a point where using functional programming “exclusively” is cool, but it’s not as weird as it used to be. I think the shift is real, but it’s not as some imagine. I don’t believe that a complete transaction will happen soon to adopt FP over OOP. The shift as I see it is toward integrating functional features in Object Oriented languages which is somehow beneficial, and not as scary as adopting a whole new style.

Why functional programming?


  • The promise of Less bugs and time:

 

  1. Easy to reason about:

    Object oriented programming doesn’t deal with shared mutability very well. Its already hard enough to manage  mutable state in a sequential code. well, imagine the pain that it will cause in a multi-threading environment. As we all know functional programming doesn’t allow mutability which makes it easier to program with. The more mutability you have in your code the more bugs are likely to pop-up.

  2. Concurrency:

    Imperative style makes it really hard to make use of multi-core processors. I once read in a java book that “managing complex thread interactions is quite difficult for even the most skilled developers; therefore a set of reusable features was introduced in the new Concurrency API to solve that” sounds cool right! No not really. As even with all of the attempts to simplify dealing with threads and manage their interactions, it still is a difficult job to do.

    Imagine that one day you came early to work, wrote a beautiful piece of code that works as expected and does the job. And you went on to have a cup of coffee and enjoy the rest of the day. But then my friend comes the manager asking you to use multi-threading. You start changing the code you got threads everywhere, but threads never come alone. They come with executors, locks, concurrent collections, atomic variables and so on. A week later you are thinking about leaving the job, I think you got the point.

  3. The ability to use more:

    In OOP when you need to reuse an object for example you must use all of the things that it depends on. Unlike FP where usability is increased, simply because you don’t have to worry about dependencies “somehow”.

 

 

Important concepts in functional programming:


After learning why, let’s discuss some of the cool concepts that functional programming relies on:

  1. Higher-order functions: Higher-order function is simply a function that accepts another function, In my opinion it is the most important concept in FP.
  2. Polymorphism: Believe it or not, polymorphism is actually one of the core values for functional programmers. It comes in a different flavor though, parametric polymorphism is a form of polymorphism where function types are parametric.
  3. Immutability: As we discussed in why FP section. When the state cannot be modified, objects management becomes easier and smoother.
  4. Lambda calculus: Expressing computation by way of variable binding and substitution.

 

 

wrapping up


It took OOP more than 25 years to be fully adopted, it may take FP more or less. but eventually if you can’t adopt with new technologies you are all doomed my friend. Coming from an imperative style of programming, a good language to start with would be Scala. I wouldn’t say that Scala or Java 8 do show the beauty of FP but its always good to find a starting point and begin.

Share

Comments

comments

Leave a Reply

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