Functional Programming vs. Object-Oriented Programming Part 2

Part 2: What is Object-Oriented Programming?

What Is Object-Oriented Programming?

It is a programming paradigm centered around objects instead of functions. There are a handful of languages that support OOP (Object Oriented programming), some of these languages are Javascript, Python, Ruby, Java, & C#. Typically what OOP aims to do is encapsulate data and behavior into objects.

4 pillars of Object-Oriented Programming

Encapsulation: will allow for us to cut down on a function’s number of parameters.

In object-oriented programming, we combine a group of related variables and functions into a unit. AKA an object. Our variables are referred to as properties, and functions are referred to as methods.

In our code example below breed, color, and name are examples of variables in a dog object, and newDog, is a function and therefore a method of the dog object.

Here’s a procedural programming example of what the code from above would look like. You can see, there is a hefty amount of function parameters being passed in.

“the best functions are those with no parameters” — Robert C. Martin

Abstraction

  • Allows for a simpler interface, reduces the impact of change, and gives us the ability to hide properties and methods within our one object.

Inheritance

  • allows us to eliminate redundant code

Polymorphism

  • poly means many, morph means form: polymorphism means many forms
  • we can implement a render method on each object.
  • EX. element.render()
  • allows us to eliminate clutter reducing switch statements.

Objects.

Let's dig deeper into objects. How do we create one?

OOP vs. Functional Programming

Ultimately when it comes to OOP and Functional Programming each has its own benefits. Check out this graphic as a visual to understand the main differences between the two.

Classes

OOP programming is often built by using classes. In a class, you would see varying objects with methods and properties defined within them, classes are deemed as blueprints for objects. Javascript itself isn’t a class-based language, so that can be a bit of a caveat when using it with OOP because it’s actually a prototype-based language. This means there is actually a global prototype built into the language. You can find examples of Javascripts built-in prototypes in this blog.

What to use and when?

Object-oriented programming is most helpful to use when we have more objects, with fewer behaviors. Functional Programing is best used when we have fixed data and there are many actions to be performed, and one major benefit is that it can provide high performance in processing large data sets for applications ( think about how many scenarios this could be pertinent to ).

Software Engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store