- Functional Programming Vs OOP
- What is Functional Programming?
- What are the Benefits of Functional Programming?
- Limitations of Functional Programming
- Let’s Wrap Up:
- Some Good Reads:
Hey everyone, I am Ryan Roy. In this blog, I’m going to explain what functional programming is and promise you after watching this blog that it’s going to be absolutely simple and absolutely breeze to understand what functional programming is.
Functional Programming Vs OOP
In functional programming, you rarely write methods that mutate existing objects. If you want to change an object, you create a new one that’s in your desired state and point to it instead. This can be incredibly helpful when working with complex data sets or large amounts of information. Compare what you might do in OOP (Object-Oriented Programming) versus FP (Functional Programming). In OOP, if your app needs to search through a bunch of things and make changes based on user input, you’ll probably have an array or list of objects.
Let’s understand this using an informative infographic:
Also Read: Top Software Development Trends for 2021
What is Functional Programming?
Functional programming (FP) has been a mainstay of academia for decades, but a growing number of programmers are beginning to use it in industry. According to developer advocacy firm Stack Overflow’s 2017 survey, 19 percent of respondents said they were using FP in their day jobs. That’s up from just 9 percent in 2016.
Functional programming has its origins in the lambda calculus, which was developed by Alonzo Church in 1929 and later popularized by Haskell Curry. What, you may ask, is calculus? In mathematics, calculus can be thought of as an algebraic system that follows certain rules. The word algebra comes from Arabic.
It means something like putting together or reconciling differences. You don’t have to worry about those meanings, but it helps to know where they come from! Historically speaking functional programming languages were created using concepts found in lambda calculus. This was before computers even existed, however.
So there are four important core things that you absolutely need to know about functional programming:
The first & important one is that functional programming is more of writing “style of code” so it’s majorly independent of programming language. But there are some programming languages which is supporting it at its best some are not.
So there are a couple of concepts that you need to understand let me expand this. This is simply is code and I’m not gonna be running any code I’m gonna just explain this syntax using the help of code. So let’s just say we got a score and you are playing on to some great level you got a score of 456.
Now on that let’s just say you have a function again this is not really a core function that just the gist of how function usually looks like. So we’re going to simply say that you have done something at this exact stage so that we want to add a bonus appear. What you can do up here you can simply call this up here simply say score is gonna be equals to score + and you just add some number like 45 and finally you just go ahead and return score as mentioned in the picture:
Please don’t yell at me by saying hey you should say this dot score is more over about understanding the concept not exactly pinpointing the stuff. So this is all okay but this is not really much of an acceptable thing in functional programming.
In functional programming you’re gonna notice that these things don’t happen. Moreover a functional if I write this into a functional programming way I would write it in a bit different manner.
I would write the same function like this and I’m going to simply say that hey this is my function again add a bonus and this is going to take a parameter score. We’re going to do now here we are going to simply just hit return and we’re going to simply say whatever the bonus or whatever the score we have got we just want to add, let’s just say 45 on to add.
Now there’s a huge difference between both the above piece of code. Now it’s not much shorter you can also make it a shorter like this but that’s not the point.
In functional programming, we keep our functions and data totally separate so that’s step number one. Or the kind of a unspoken rule in the functional way so that keep your data and keep your functional totally separate. While debugging this piece of code is much easier to debug because it does not refer to any global variable that might be causing the issue.
This is moreover like self intake function which just runs on its own. It’s also not much very dependent on what kind of data we are passing whether it’s a score or maybe it’s life whatever it is, it’s just gonna add 45 to it. So that’s the basic gist of it. So that’s the one thing keep your data and your functions totally separate.
Now again one another thing is actually state change which is a very interesting thing and it’s a bit difficult to understand for beginners. But I’m gonna try my best these examples will just simplify like ridiculous level. So it says that you should not change the state much often as much as you can avoid it just do it. For example: A person Hitesh say “hey” a lot and then later on he realized that this variable should not say “hey” it should say something like “hey there” and then he realize the same variable should say something like this “hey there” and then “everyone” So, there is nothing wrong in this style of code we are just having a variable and this variable is getting updated all the time.
Now keeping the track of this state is actually a bit tricky to understand this one. So in functional programming style we actually don’t reuse this variable much often we don’t change it much often. In fact what we do is let’s just say “hey” page says “hey” and then we want to update the somehow so we’re gonna say that Hitesh on wednesday says “hey there” And, Hitesh on a Saturday says “hey there” again & “hey there everyone”.
So this is more like a functional style you don’t update the same variable. It is okay to really clear or kind of a newly declared again available with some more modification so that it’s much clearer and okay for us. So this thing is pretty common: don’t change your state and mutability & immutability, which is common in function programming. Again, the rule of thumb is to make sure you don’t really update the same variable repeatedly. You instead declare more of them.
I am now coming up with the last part which is very funny and pretty impressive as well. So in this what they say is something like this function or functions are treated as first class. This is a bit difficult statement to understand for beginners. But I’m gonna make it absolutely ridiculously simple.
So let’s just say we have a “Const with H” and it is storing 45. And, we also have got a variable which is named in your language however you define it that’s okay. If I just say this variable is “Hitesh” that’s okay!
But what functional programming says is that the way you are treating your numbers and string and throwing them around anywhere like you can declare them in a variable, you can pass them in a function. Also, you can return them in a function. The same kind of treatment should be given to the functions as well. So we can simply say “Const addscore” something like that score and that can also be a simple function whatever you want to do inside that it should be the case.
So this is how the function should look like in a functional programming. It can be it should be able to store functions into any variable and not only that if let’s just say you have a method which is “say hello” and this is our method in here also you should be able to accept function as a parameter. So I can simply say function and then simply say like this whatever you want to say or maybe a reference would be a better example but this actually makes things simple and easy.
So this is it we can store the function into available we can pass it as a parameter, we can return it as whatever the data type it is. So this is the gist of a functional programming, so these are the three important thing.
What are the Benefits of Functional Programming?
FP makes it easy to think about and reason about your code. It’s simple: if you want something done, give it to a function! Functions are predictable: If you call a function with certain arguments, it’ll return certain values every time. Plus, functions in FP tend to be smaller than their counterparts in OOP. That’s good for performance as well as maintainability. Have a look at some benefits in a detailed manner:
1) Pure functions are better than impure functions
In functional programming, pure functions are preferred because they’re easier to test and maintain. Take a function like strcpy() in C. When given two valid strings, it’ll copy one to another without changing anything else in memory.
2) Pure functions are easier to test
One of my favorite aspects of functional programming is that pure functions make testing and debugging so much easier. With a little setup, you can pass in expected arguments to test a function as though it were an API endpoint, making sure your logic behaves exactly as expected. There’s also less setup needed for writing actual tests—you don’t need to create a server environment or mock out dependencies.
3) Functional programming leads to fewer bugs
While programming in a functional style does not eliminate bugs, it certainly makes them easier to find and reduces their impact. One way to accomplish both of these goals is through referential transparency (let it be computed once), a cornerstone of functional programming. This term refers to functions that always return identical results when called with identical arguments; such functions are guaranteed not to have side effects.
4) Function signatures are more trusted
Functional languages tend to enforce and trust functions more than object-oriented languages. An object can do things that its methods can’t, which makes for a complex relationship between what I am and what I can do. By contrast, functional programming functions are objects that have one single well-defined purpose; if it says it implements an equals() method then you know that its only job in life is to compare two things for equality.
5) Concurrency is more easily kept safe
Concurrency lets you process many actions at once—but only if you’re careful. Even in single-threaded applications, it’s important to use proper synchronization tools like locks or mutexes.
6) Recursion is simpler
One of functional programming’s most attractive features is recursion. Recursion in functional languages means calling a function that calls itself, such as in mathematical functions like factorials and Fibonacci sequences.
Limitations of Functional Programming
Generally, functional programming languages tend to have fewer limitations than object-oriented ones. For example, in functional programs, you can pass a variable number of arguments to a function or write functions that don’t return anything (which is impossible in many popular OOP languages like Java).
In FP, data structures like arrays can also be passed around without having to wrap them in classes. Using FP techniques, you could even create self-modifying code. There are plenty of other benefits for writing code functionally as well—but those are just some examples of what it might look like on a technical level.
Let’s Wrap Up:
I hope this blog help you understand what is functional programming in an easy way. If you have an query or want to add anything, please write in the comment box. Let’s discuss bit more about functional programming in comment box.
Some Good Reads:
10 Top App Development Companies in India for 2021 (Updated)
10 Top Software Outsourcing Companies in India
10 Top Web Development Companies in India for 2021
20 Top Software Development Companies in India
10 Top Angular Development Companies in India
10 Top Java Web Development Companies in India
11 Top Content Writing Agencies for 2021 (Updated)