A Dip in the Water with Understanding Functional vs. Object-Oriented Programming
Tags: functional, objectoriented, paradigm, design
Published: 2019-06-18 07:49:06

Functional Programming (FP) and Object-Oriented Programming (OOP).

What are they, what is the difference, is one better than the other? How do they work? When is each one appropriate to use? I've had so many questions about this since this seemed like such a loaded question around programming. Let's start with what each one is.

Functional Programming:
"..a style of building the structure and elements of computer programs—that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data." - [Functional Programming](https://en.wikipedia.org/wiki/Functional_programming)

Object-Oriented Programming:
"..a programming paradigm based on the concept of "objects", which can contain data, in the form of fields (often known as attributes), and code, in the form of procedures (often known as methods) A feature of objects is an object's procedures that can access and often modify the data fields of the object with which they are associated (objects have a notion of "this" or "self")." - [Object-Oriented Programming](https://en.wikipedia.org/wiki/Object-oriented_programming)

Just based on these Wikipedia definitions, I'll determine that some keywords to point out here for some differences:

FP - avoid changing-state and mutable data (Immutable data)
OOP - access and modify data fields of objects (mutable data)

So what you are saying is if I did something like this.
```ruby
#Functional Style
class Character
    attr_reader :name, :weapon

    def initialize(name, weapon= "none")
        @name = name
        @weapon = weapon
    end

    def equip(new_weapon)
        Character.new(@name, new_weapon)
    end

    def view_info
        puts "Name: #{self.name}"
        puts "Weapon: #{self.weapon}"
    end
end
puts "Functional Style"
puts ""
puts "-----Creating Object-----"
hero = Character.new("Que")
hero.view_info

hero_with_weapon = hero.equip("Sword")
hero_with_another_weapon = hero.equip("Axe")

puts "---Hero with weapon---"
hero_with_weapon.view_info
puts "---Hero with another weapon---"
hero_with_another_weapon.view_info
puts "---Hero without weapon---"
hero.view_info
```

Here is the output of this code:
```
Functional Style

-----Creating Object-----
Name: Que
Weapon: none
---Hero with weapon---
Name: Que
Weapon: Sword
---Hero with another weapon---
Name: Que
Weapon: Axe
---Hero without weapon---
Name: Que
Weapon: none
```

So we instantiated a hero "Que" without a weapon, which outputs their weapon to default "none." Then we created a copy of "Que" but with a weapon "Sword," instantiating a new Character Object with the Equip method. However, we are assigning that method to a variable 'hero_with_weapon.' We created another copy with an 'Axe' weapon, also assigning the method to another variable. Then calling just the 'hero' object at the end, we see that the weapon is still none from the first 'hero' object.'

```
[1] pry(main)> hero
=> #<Character:0x0000000003c765e8 @name="Que", @weapon="none">
[2] pry(main)> hero_with_weapon
=> #<Character:0x0000000003c76458 @name="Que", @weapon="Sword">
[3] pry(main)> hero_with_another_weapon
=> #<Character:0x0000000003c76408 @name="Que", @weapon="Axe">
```
We can see here in pry that calling each variable, we did not alter the hero object, but instead created copies of the hero object. The objective of Functional programming is to create immutable data so we don't alter data we don't want. This is just a brief example of my understanding of Functional Programming because there is so much more to this topic that I did not expect when I was diving more into this topic.

![](https://thepracticaldev.s3.amazonaws.com/i/lohwsqxyve79uxs0zxb2.jpg)

With Object-Oriented Programming, the data is altered when we use methods.

```ruby
#OOP Style
class Person
    attr_accessor :name, :weapon
    def initialize(name, weapon="none")
        @name = name
        @weapon = weapon
    end

    def equip(new_weapon)
        @weapon = new_weapon
    end

    def view_info
        puts "Name: #{self.name}"
        puts "Weapon: #{self.weapon}"
    end

end

hero2 = Person.new("Que")
puts "Hero 2 without weapon"
hero2.view_info
hero2.equip("Spear")
puts "Hero 2 with weapon"
hero2.view_info
hero2.equip("Bow and Arrows")
puts "Hero 2 with weapon"
hero2.view_info
```
We instantiate a 'hero2' Person object. Calling the equip method for the 'hero2' object assigns the "new_weapon" argument into the @weapon property. So every time hero2.equip is called with a new weapon, it changes the weapon property for hero2.
```
Hero 2 without weapon
Name: Que
Weapon: none
Hero 2 with weapon
Name: Que
Weapon: Spear
Hero 2 with weapon
Name: Que
Weapon: Bow and Arrows

[1] pry(main)> hero2
=> #<Person:0x0000000003b05998 @name="Que", @weapon="Bow and Arrows">
```

When we call hero2 at the last line, we can we see 'hero2' forgot about not having a weapon and having a spear. This shows that we would constantly be changing the data for the objects.

![Alt text of image](https://media.giphy.com/media/rkXNems5uxExG/giphy.gif)

Functional is to Declarative as Object-Oriented is to Imperative.

Declarative: "In a declarative programming style you describe the results that you want, but not how to get there." - [tobyink](https://www.perlmonks.org/?node_id=983655)

Imperative: "The word "imperative" comes from the Latin "impero" meaning "I command". It's the same place we get "emperor" from, and that's quite apt. You're the emperor. You give the computer little orders to do and it does them one at a time and reports back." - [tobyink](https://www.perlmonks.org/?node_id=983655)

FP & OOP has mainly been categorized as these 2 approaches to programming. Declarative expresses the logic without describing the control flow.
Imperative expresses through all the commands necessary through the program on how it works.

Pros and Cons

Pros of using Functional Programming:
- Able to preserve original data, work with Immutable data
- Working with many copies for different purposes (Ex. iterations)
- Working with Pure Functions (Function where return value is based on input value)

Cons:
- Having many duplicates based on original data
- Working with pure functions become an obstacle when dealing with I/O operations

Pros of using Object-Oriented Programming:
- Practically everything can be objects, so applying real-world ideas to OOP is easy
- Classes are reusable around the program
- Parallel Development: when working with teams, it is much easier to work with independent code throughout development

Cons:
- Program needs proper planning from the beginning when designing the application
- It can get really scalable, sometimes unnecessary code gets loaded in too

There are still many more pros and cons that I didn't include because I am even unfamiliar with that territory just going deeper, like Lambdas, Recursion, Memoization, etc. I did not expect that this rabbit hole was even deeper than I could have possibly ever imagine. Although, I do feel like I understand a general concept between Functional and Object-Oriented Programming now since this has bothered me throughout my years of self-teaching myself coding to even currently studying at a coding bootcamp. 

When I looked into many articles, blogs, and stack overflow readings about the debate with Functional Programming and Object-Oriented Programming, I found that that notion of thinking has become an obsolete mindset of programming because programs already implement both sides in a program during situational circumstances. There is still so much more discussions with the Functional Programming & Object-Oriented Programming debate.

A quote that was used from a blog from CodeNewbie gives a nice summary about when to use Functional Programming and Object-Oriented Programming:

'Michael Fogus, author of “Functional JavaScript”, suggests in his blog post “FP vs OO, from the trenches” (http://blog.fogus.me/2013/07/22/fp-vs-oo-from-the-trenches/) that when he deals with data about people, FP works well, but when he tries to simulate people, OOP works well.' - [CodeNewbie - Object Oriented Programming vs Functional Programming](https://www.codenewbie.org/blogs/object-oriented-programming-vs-functional-programming)


![](https://thepracticaldev.s3.amazonaws.com/i/bryqu84a8mqrlf3bbvta.jpg)

If you want to learn more beginning concepts of Functional Programming, check out this link:

https://www.freecodecamp.org/news/an-introduction-to-the-basic-principles-of-functional-programming-a2c2a15c84/

Here is also beginning concepts of Object-Oriented Programming:

https://www.freecodecamp.org/news/how-to-explain-object-oriented-programming-concepts-to-a-6-year-old-21bb035f7260/

Citations:
https://www.sitepoint.com/functional-programming-pure-functions/

https://dev.to/nijeesh4all/functional-programming-vs-oops--explain-x-like-im-five-2fnc

https://www.educba.com/functional-programming-vs-oop/

https://www.csetutor.com/advantages-and-disadvantages-of-object-oriented-programming-language/

https://greengarageblog.org/6-pros-and-cons-of-object-oriented-programming

https://codeburst.io/declarative-vs-imperative-programming-a8a7c93d9ad2