O.O.P: Pleasant by Nature

How can I explain it? I’ll take you frame by frame

Object-oriented programming (OOP), is a term loosely used by young devs constantly as they start learning the basics of software development. Java, C++, C#, Python, PHP, JavaScript, Ruby and Perl, to name a few, all carry OOP qualities in their structure. The C languages, Python and Ruby are generally considered to be true OOP languages, and there remains debate in Java. In my opinion, JavaScript is the most functional of the bunch while still, supporting some OOP guidelines. The opposite of developing and adhering to the OOP paradigm would be functional or procedural programming techniques. As a young developer, it’s important to understand both concepts.

Why OOP?

Procedural Programming Illustrated — a/k/a spagetti code

OOP was introduced to solve this problem. It combines like features and classes into units, or objects. Variables are referred to as properties and functions as methods.

Example: A car has properties such as a make, model and color, with methods available such as start(), stop(), and move().

The 4 Pillars of O.O.P

Encapsulation

Example: If a cat is hungry we can feed a cat with the feed()method, but can’t directly change how hungry the cat is with decreasing the hunger property directly on the cat.

Abstraction

Example: Your iPhone has a home button, which you know how to use all the time. Not critical to understand what the home button does under the hood. A software update wouldn’t change how you use the home button.

Inheritance

Example: If we need to build a program that manages a school, can create a teacher class, and then two teacher classes (regular vs. subs), while also maintaining a separate student class, which all derive their logic from “Person”.

Polymorphism

Example: A triangle, circle and rectangle are children of “Figure”, which has access to calculateSurface()and calculatePerimeter().

OOP vs. Functional Programming

Say we wanted to create a program which would analyze the score of a basketball game.

In OOP, we would:

  1. Create a Game class would would be initialized with a team and players as its children, with the players having properties of points scored, rebounds, assists, steals, etc.
  2. We would create instances of the players
  3. Invoke a method of pointsScored() to assign points to a player.

In a Functional Program, we would:

  1. Create a players array, which is an array of a hash of their team and game played.
  2. Create a pointsScored() method which returns the player name and the amount of points they scored in that game.
  3. To add points to the player if they scored a basket, we could create a new method, called addPoints(), which maps through the players array and assigns the points to that player.
  4. We could invoke both methods, pointScored() and addPoints() to return a new dataset: final_game.

OOP proponents would argue its paradigm does a better job making code re-useable through inheritance, and encapsulations helps managing and manipulating the dataset. Function programming developers would argue that keeping methods and datasets separated leave less room for bugs and errors. Each of the paradigms have their own use cases, but ultimately, it’s up to the dev team which structure they prefer to develop their application in.

Conclusion

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