The best explanation of why OOP is useful

The best explanation of why OOP is useful
0.0 0

#1

TL;DR version: Click here for a great explanation of how OOP can make certain programs much easier and better

I normally consider myself not-so-stupid, but OOP made me feel downright moronic. I went through the FCC stuff. No problem. But following those simple steps didn’t tell me what OOP really was or why I should use it or when. I’ve actually been looking for what I would have imagined would be a simple answer to a very simple question for over a month!

Book after book, site after site, I kept running across the exact same thing. “This is how you make an object. This is how to make a constructor function. Variables in objects are called properties. Functions are called methods. Encapsulation. Inheritance. Polymorphism.” It’s like telling you how to build a carburetor without having any clue as to what it does or how it interacts with the engine or where it goes.

Nothing was helping me to understand why it’s better than what I was doing before. Making my Pomodoro timer was simple enough. User clicks and a function is called, which after a certain time calls another function, which after a certain time, calls the first function again, etc. Click on something else and you stop the timer. Click again and it starts again. Works beautifully. I could say this about all the projects I’ve done so far. Try as I might, I couldn’t understand why OOP was needed or how to implement it in a useful way. Why would I need to use objects for this? How would it make it better or easier to code?

And then, two days ago, I finally found someone that explained OOP in a way that makes sense! Joy filled me to the brim. LOL

I don’t know if this will help anyone else, but here is a clear breakdown with a concrete example of how trying to code something without OOP can be unwieldy and how OOP can come to the rescue. This is the link.

Going back to Pomodoro, I still don’t see how using OOP would be any better though. However, I still feel like I’ve unlocked a level here because I CAN see how objects would be immensely useful for certain games and other applications that would require you to keep track of things by using several arrays, etc.

If anyone knows of any other, similar sites/tuts that explain not only how to create objects, but instead shows how to use them effectively and when, please post a link here. I would love to see more like this to really wrap my head around it.


#2

That is a great article thanks for the link.

I’ve only been coding for 3 days so take what I say with a grain of salt but I found this article useful in explaining why you should implement oop. It uses a java program called animals.

Essentially inheritance and polymorphism can be used to speed up your code implementation time and to isolate bugs, which makes your code maintenance tasks easier.


#3

A very good explanation, imo, is in HeadFirst books. It’s very simple and detailed and with easy to understand examples. I myself have read their book on Java and really liked OOC explanation there, but probably they also have a book on Javascript and I would expect it’s beginner-friendly too.


#4

Regarding the Pomodoro timer: I must be really dumb or I’m missing something because it does not seem to be simple to build at all.


#5

Haha. Don’t feel bad. When I said “simple enough”, I didn’t mean easy to program. Just that I didn’t see any real need for OOP. After reading this article, at least one thing was clear about OOP which wasn’t before. For certain types of programs, it can make things a lot easier than making a bunch of arrays and trying to keep track of them all.

But yeah, programming can be very tricky and it has a way of sometimes making us feel dumb, doesn’t it? In every program, I have at least a few of those moments. Incrementing a variable in the wrong place, using an equal sign instead of a double equal, having a colon instead of semi-colon, etc., and I constantly have to look things up because I can’t memorize the syntax commands even though I have used it at least 20 times before. LOL. But we aren’t dumb. These are things that seasoned vets do all the time too. They’re just quicker at catching them. I’ve seen plenty of tutorials on YouTube by experts and I’ll often catch mistakes like that and sure enough, 2 minutes later, they realize it and go back to fix it.


#6

Just remember that Javscript has no classical inheritance. It has no ‘class’… And with closures & arrow functions now it is more of a functional programming with prototypal inheritance.

No classes, no constructors. Don’t learn the ‘bad’ way.
Where you think you should be using ‘class’ because someone told you so - use factory function, read up on Design Patterns in JS.

(class keyword that came in ES6 is a wrapper with prototype delegation anyways so - no point polluting your code with it. Also class coupling is a pain with bigger apps, makes your code brittle).


#7

In JS everything is an object so you are actually using it all the time without even knowing. That said, using many instances of an object is not always needed. In the pomodoro clock example you need two counters - one for the sessions and one for the breaks. If you use instances you can write the code once and then make the specific counters that you need.

Edit: The problems with OOP begin to appear when you start abusing inheritance. No matter how good your taxonomy is, you’ll end up with cases where things just don’t fit very well in the created structure. You’ll also find debugging really difficult since you’ll have to check the entire inheritance chain for the error.


#8

tl;dr avoid using objects unless you know why they are needed.

Hi,
That example is highly misleading. Structuring data is not OOP at all. It is a common organizational tactic used by any sound minded individual that is storing related data.

What is OOP about or why would you use an object?
And object is a lot like a program with one responsibility. It hold the data and methods/functions that are required to handle this responsibility.

It should not be depended on another parts of the whole programs which allows thorough testing of an object. This is useful when you want to break down a large software solution into smaller responsibilities. It is also useful when you want to make reusable blocks of code.

This is not the only way to write reusable code. Any function that is useful, can be called anywhere in the code and can also handle a responsibility.

When you want to write code that is only useful in one specific case, avoid an object. When you want to handle a responsibility that is not persistent, you can likely handle it with a function. Only when you need to accumulate data over time, hold mutated state and respond accordingly, will an object be useful.

Generally objects are misused because of people who misunderstand them or are payed to advocate overuse of objects. The link you shared is an example of object misuse. The same program would work just fine if the object were a simple data structure.


#9

Still Arthur White just on another computer. :slight_smile:

tl;dr, Writing oop is akin to writing several distinct programs. It requires much more work and effort and more code to make the same program. It’s benefits will only appear when you write a very large and complicated program and you write it smart (imho with several persistent reusable systems that communicate).

Object are to be dealt with, they are hard work when compared to procedural code and they slow down your progress. Oh, and using inheritance is known to be a terrible idea, most would agree (look it up).

Here are some good and somewhat fair slides offering a cleared picture:

https://www.cs.drexel.edu/~introcs/Fa15/notes/06.1_OOP/Disadvantages.html?CurrentSlide=2

You can click next slide and previous slide at the bottom…