Functional Programming vs. Object-Oriented Programming Part 2
Part 2: What is Object-Oriented Programming?
What Is Object-Oriented Programming?
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
- Allows for a simpler interface, reduces the impact of change, and gives us the ability to hide properties and methods within our one object.
- allows us to eliminate redundant code
- 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.
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.
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 ).