Functional VS Object-Oriented Programming
Kurt Bauer
Posted on August 26, 2019
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 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!
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
- Data
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!
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
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")
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!
kuma.change_name("Grapefruit") kuma.name # "Grapefruit" kuma.change_mood("post-meal-happy") kuma.mood # "post-meal-happy"
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
def doesOneThing(number){ return number * number end
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 | 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 |
But which one?
Well, as per this sick stackoverflow 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!
Posted on August 26, 2019
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.