What you need to know about Object Oriented Programming
Java, C++, Python, Ruby, and many other languages thrive on Object oriented programming (OOP) and understanding what that means is vital if you plan on using these languages professionally.
🤔 What is object oriented programming?
This paradigm is based around defining “Classes” which will initialize “objects”, these objects will contain the the attributes and properties you defined in the Class. Object oriented programming is a model that encourages encapsulation, abstraction, inheritance, and polymorphism.
⏸️ Pause for definitions!
Encapsulation: Ensuring that the internal state of a class is not unnecessarily exposed to people who will be adding to your code in the future. We can “hide” a classes state by making it
private, this will ensure that it’s only accessible within the object and its methods.
TLDR; reduce complexity by never exposing state in the objects interface unless you have a good reason
Abstraction: Ensuring that the methods that you do expose are relevant to other objects/classes. A toaster is a great example of abstraction, rather then provide you a timer and temperature setting it gives you a number from 1-5 or shows you how dark the toast will be, this is a Abstraction.
There is not special keyword like “private” for this and knowing how to best abstract something is one of the most valuable skills any software developer can have.
TLDR; reduce complexity by abstracting the complexity of your code into higher level concepts.
Inheritance: One of the great productivity benefits of OOP is the ability to reuse classes in many contexts, inheritance makes this even more powerful. Languages that support OOP allow you to write a class that will be used by other classes. If your writing a class for dogs, cats, hamsters, etc you can begin by writing a “Mammal” class that has things like eat, sleep, etc that all mammals do.
TLDR; reduce complexity in individual classes by collecting common functionality in a single parent class
Polymorphism: It’s quite common that you have a several classes that share common functionality but accomplish it in different ways, for example a circle, square, and triangle all have an area but the calculation for each is different; using polymorphism you can declare a parent class where any shape that uses it must provide it’s own
What does OOP have that Functional Programming doesn’t? While its true that OOP has lost some of its appeal in recent years its still one of the most wildly known and most common paradigms to find for any long standing codebase. Having a good abstraction is critical to maintainable software regardless of what paradigm you use but it just so happens that OOP makes this a key tenet.
🧠 How to make your code object oriented?
Not all languages lend themselves well to this but if you are using a language well suited for OOP then these few steps can be helpful in writing OOP code.
1. Use UML diagrams
If you dont know what these are watch some YouTube videos about them, they will make your object relations explicit and allow you to visualize how these abstractions are connected. A good UML diagram will help you find bad abstractions before you spend the time writing all the code.
2. Get as much context as possible
Even if your just adding 1 class to a codebase its important to understand how the system works, knowing this will reduce the chance you write redundant code or have unintended side effects.
3. Test Test Test
IMO debugging OOP code is harder then functional code and the best way to made debugging easier is to have robust testing.
Object oriented programming has gotten a bad wrap as a bad alternative to functional programming but it isn’t that black and white. They both have their place and it’s your job to knowwhen one may be the better choice.
If you enjoyed this article please consider “following” or subscribing to my newsletter.
👋 Have a great day!