Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
August 26, 2019 07:41 pm GMT

Functional VS Object-Oriented Programming

The Gist

Different programming languages have different features, or paradigms, that can be used to classify them.
Today we'll take a look at the two main ones, functional and object-oriented.

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/use
    • Behavior
      • what your program is looking to DO and HOW

There's a reason why most coding bootcamps 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

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 about our objects 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  endend kuma = Cat.new("Kuma", "mischievous")

What happened above?

  • we create 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, because if you do own a cat you know that's the second thing you'll learn 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 for 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 them, so that we can easily access them later on to take a peak.
  • '@' symbols are used for instance variables, these exist in an object(instance) and is used without being passed in

meow,meow,meow

Functional Programming

  • Uses a series of small methods that each do their own specific job.

ants

Here we see small functions in their natural environment
  • Implements composition or building of large tasks with smaller ones, 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 * numberend

What happened above?

  • Above we have an example of 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

TopicFPOOP
DEFINITIONemphasizes evaluation of functionsbased on concept of objects
DATAimmutablemutable
MODELdeclarative programmingimperative programming
SUPPORTparallel programming supportedNo Support
EXECUTIONstatements can be executed in any orderNeed an order
ITERATIONRecursionLoops
BASIC ELEMENTSFunctions & VariablesObjects & Methods
USESFew things with need for more operationsMany things with few operations

Thanks to EDUCBA!

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!

Yoda


Original Link: https://dev.to/krtb/functional-vs-object-oriented-programming-357

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To