DEV Community

Young Mamba
Young Mamba

Posted on

OOP The Definitive Guide

OOP - Object Oriented Programming! ( There is a video version of this blog in YouTube, watch it here )

OOP At A Glance

OOP, is just a fancy way of saying that all instances of classes are objects.

For example, you have a list of books, and each book is a class. You have a function to read your books, etc.

See how I just gave a real-life example using coding terms?!

In OOP, you can think of everything like real-life objects.

Your code editor is your workshop, and your project is the purpose of the workshop( that's how I like to think of it)

Say that you we're building a library App( since we used that example before).
Your code editor( let's say, VSCode) is your workshop( library in this case)

You make a variable( list) where you store all your books, and this variable is a shelf in your library.

Then you make a class( or you'd probably make the class first, lol) of book where you define all the characteristics of the book, this could be how you define a book in your library.

You get the point.

OOP, is a way of designing your projects!

Think of it as a way of designing systems. Which is available for all( if not, most) coding languages!

And since it's a design mechanism( that's what I'm calling it) it has a couple of goals, and principles!


Goals

This is what you should try to achieve when designing an OOP system.

Robustness

To put it in simple English, a program that produces the right output for all the anticipated inputs in the program's
application, but that's also capable of handling unexpected inputs.

For example, you might want your email form to always have @gmail.com in the end. Maybe your users don't add @gmail.com your app should be able to handle that!

Adaptability

Your program needs to evolve over time with the new trends and market needs!

For example, now( in E-commerce websites) chatbots are trending. Maybe later, instead of chatbots a trend will be talking to AI with your voice versus texting with them! Your app should be able to evolve.

Reusability

The same code should be able to be used in different parts of the app. Don't write very specific functions! Instead, write general-purpose functions.

For example, instead of having a function where you filter one specific model( from the database) you have a function where you can filter every model, with any field you choose, you just have to pass in parameters!


Principles

These are approaches( think of them as methods) to achieve the goals mentioned above.

Modularity

Modularity refers to having different components( in your app) that all have their own functions.

In other words, keep everything organized.

Don't cram everything in one place, make a place for everything!

Abstraction

Abstraction refers to breaking down a system into its most fundamental( simplest) components!

Something like, Divide And Conquer!

Encapsulation

Different components of an APP should not reveal the internal details of their respective implementations.

Encapsulation gives you the freedom of doing component-specific coding( this contradicts, what I've said about reusability, however some times we do need to add specific functions in our components)

Top comments (0)