Functional VS Object-Oriented Programming
Tags: learning, beginners, oop, explainlikeimfive
Published: 2019-08-26 19:41:28

---
title: Functional VS Object-Oriented Programming
published: true
description: Programming Paradigms and their uses
tags: learning, beginners, oop, explainlikeimfive
cover_image: https://thepracticaldev.s3.amazonaws.com/i/hfhzaoyj3g3ymqndtb1u.jpg
---

# The Gist

Different programming languages have different features, or _paradigms_, that can be used to classify them.

Today we'll take a look at two of the most talked about, functional and object-oriented.

Edit #1: And as [Adam](https://dev.to/adam_cyclones) pointed out, the two are not pitted against each other, they're just different! These are only two of a wide array of varying approaches that could all server(typos are the best puns) you better, so never stop doing your research!

![](https://media.giphy.com/media/KKbXQxrDFLLQQ/giphy.gif)

## The What

Firstly, let's take a look at some of the commonalities

* Things either approach will have to deal with
  * Data
      * what your program wants to KNOW and USE
  * Behavior
      * what your program is looking to DO and HOW

There's a reason why most coding boot camps start you off with ruby, or python, and that's because they're both very eye-friendly languages. Let's use Ruby for our walk through!

![Ruby from Steven Universe](https://media.giphy.com/media/85RTAcTGvcTxm/giphy.gif)

### Object-Oriented Programming(_OOP_)
* Classes often used to generate object instances
* Class defines the attributes with which we want to imbue our object.
* We will give our class "_instance methods_", which will exist within our object.
* These instance methods can be called on the object itself.
* "initialize", is not an instance method, but instead tells the class what attributes it will have at the moment of it's creation. 
* Every new instance of our object will contain preset __data__ and __behavior__
* As noted above, data will be provided to our instance upon creation
* We then use methods on our instance object to manipulate it's data
* All of the important information our objects contain is stored safely within their classes. If you can imagine being an engineer at a fairly large company, with a lot of pre-written code, you can also see where this would come in handy.
* __Core Concepts__
 * Abstraction
 * Inheritance
 * Polymorphism
 * Encapsulation

![meow,meow,meow](https://media.giphy.com/media/3oz8xSqoR27S07xXUs/giphy.gif)

<pre>
class Cat
  def initialize(name, mood)
    @name = name
    @mood = mood
  end

  def change_name(name)
    @name=name
  end

  def change_mood(mood)
    @mood = mood
  end
end 

kuma = Cat.new("Kuma", "mischievous")
</pre>

### What happened above?
* We created a Cat class, or blue-print, for what we want to be able to do with our Cat instances
* We initialized our Cat with a _name_ and _mood_. If you've ever hung out with cats, you'll know their mood is the second thing you remember about them after their name. 

Now let's change their name and mood!

<pre>
kuma.change_name("Grapefruit")
kuma.name
# "Grapefruit"

kuma.change_mood("post-meal-happy")
kuma.mood
# "post-meal-happy" 
</pre>

### What happened above?
* Using the _change_name()_ method allowed us to change the name of our Cat class instance object
* Using the _change_mood()_ method allowed us to change the mood of our Cat class instance object
* Our initialize method will take the name and mood we passed to our Cat object and store that information, so that we can easily access it later on
* '_@_' symbols are used for instance variables, these exist in an object(_instance_) and is used without being passed in


### Functional Programming(_FP_)
* Uses a series of small methods that each do their own specific job.
* Implements _composition_ or building of large tasks with smaller ones, which is also used in OOP languages, but it's pivotal to FP ones.
* Objects are immutable, can't be changed after being created
* Best fit for data science work
* A function is reusable
* __Core Concepts__
  * Higher Order Functions
  * Pure Functions
  * Recursion
  * Strict & Non-Strict Evaluation
  * Type Systems
  * Referential Transparency
 
![ants](https://media.giphy.com/media/yUko4cwc6nkHu/giphy.gif)
<figcaption>Here we see small functions in their natural environment</figcaption>
<pre>
def doesOneThing(number){
 return number * number
end
</pre>

###What happened above?
* Above we have an example of a pure function
* The same value will always be returned, as long as the same input is given
* There is no other operation occurring within the function that could alter our result
* An instant benefit is fewer lines of code!
* In FP, we will be viewing every thing we do as transforming data by applying some sort of operation on it, and then returning a new data set
* We also often times default to using a __map__ method, instead of __each__, which creates a new copy of the data and stores it in an array. The original array is unscathed as in FP, immutability of our data is key
* Immutability allows us to keep track of our data's value


## Comparison Table

| Topic        | FP           | OOP  |
| ------------- |:-------------:| -----:|
| DEFINITION    | emphasizes evaluation of functions | based on concept of objects |
| DATA     | immutable      |   mutable |
| MODEL | [declarative programming](https://en.wikipedia.org/wiki/Declarative_programming)      | [imperative programming](https://en.wikipedia.org/wiki/Imperative_programming) |
|SUPPORT| parallel programming supported | No Support |
| EXECUTION | statements can be executed in any order | Need an order|
| ITERATION | Recursion | Loops |
| BASIC ELEMENTS| Functions & Variables | Objects & Methods |
| USES | Few things with need for more operations | Many things with few operations |  

[Thanks to EDUCBA!](https://www.educba.com/functional-programming-vs-oop/)


_But which one?_

Well, as per this sick [stackoverflow](http://stackoverflow.com/questions/2078978/ddg#2079678) post, here are some key concepts to keep in mind when considering these two very popular approaches.

OOP
* Helpful when dealing with a fixed set of __operations on things__
* As your code evolves, you _add new things_
  * this means that you add new classes, which make use of existing methods
  * existing classes are left alone

Danger Zone
* Adding a new operation may require editing many class definitions to add a __new method__

FP
* Helpful when you have a __fixed set of things__
* As your code evolved, you __add new operations__ _on_ things.
  * this means you add new functions which compute with _existing data types_
  * existing functions are left alone

Danger Zone
* Add a new thing may require editing many function definitions to add a __new case__

###TLDR;

Object-Oriented Programming uses Classes, objects and methods to achieve it's lofty goals. Functional Programming on the other hand makes use of an army of  pure functions and variables to to build a larger whole. Languages dedicated to on or the other each come with their own gotchas (looking at you JavaScript 😭). Make sure to research the communities supporting whatever language or framework you're looking at, and you'll be steered in the somewhat right direction. That is, until you hit a huge bug and try to turn in the other direction...this is ill advised but not impossible. Still though, like adding Redux to your React app, why would you give yourself that extra work? Plan, plan, plannnn!

![Yoda](https://media.giphy.com/media/d2W7eZX5z62ziqdi/giphy.gif)