Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get better at OOP? [closed]

Tags:

javascript

oop

This might come as a strange question to many of you, and I don't actually know if it is correct to say OOP in this context, because OOP (object-oriented programming) is usually associated with programming languages like C++ and Java, and not lightweight programming languages, or scripting languages. My question, however, is in the category of JavaScript, which is also object oriented. I do know about objects, properties, methods, prototypes and constructors, I just can't seem to get into my mind when to use objects.

When I am writing my web-applications, I, for some reason, never use objects. This annoys me, because when I read about objects in a variety of books and online articles, objects make everything so much simpler and, just to put it out there, I HATE repeating myself, and this is why I wish I knew when to use objects.

I really want to become better at using objects and when to use objects.

Can you please mention a few situations objects would be good? It would be really nice to have written down something you know you can go back and look at when you get confused about when to use these darn objects :)

I would love simple answers explaining why and when objects are to prefer.
I would also like if you could tell me if I am to use objects when I am in some special situations generally suitable for objects i.e. every time you want to _________ then you use an object...


I really hope you understand my question and you will consider that I'm somewhat new to this site and new to JavaScript

Thanks!

like image 996
Latze Avatar asked Aug 23 '10 12:08

Latze


People also ask

How do I become an expert in OOP?

Regular 1:1 mentorship from industry veterans. Practical experience through real-life projects. Career support via a dedicated recruitment team, alumni network, etc. Aspirational peer group of 3,500+ Scaler students & alumni.

Why is unit testing harder in OOP?

Why is unit testing harder in OOP than functional programming? Objects may maintain internal state, which is not easily accessible by the tests. The quality of unit testing frameworks for functional languages is better. OOP promotes code reuse, which means that your tests have to consider more use cases.

How do you follow Open Closed Principle?

As the name suggests, this principle states that software entities should be open for extension, but closed for modification. As a result, when the business requirements change then the entity can be extended, but not modified. For the illustration below, we'll focus on how interfaces are one way to follow OCP.


2 Answers

You probably use objects without even realizing it.

If you're writing Javascript that interacts with the DOM, you're using objects.

If you're using any of the Javascript frameworks out there (jQuery, MooTools, etc.), you're using objects.

Using objects will be useful when you need to encapsulate some commonly used code so that it can be easily re-used (within a single application or across multiple applications like jQuery plugins...which are objects in and of themselves).

And to answer the question in the title of your post...the only way to really get better at OOP is to practice! Reading and studying the subject can only get you so far.

like image 103
Justin Niessner Avatar answered Sep 26 '22 12:09

Justin Niessner


First, you don't need to use objects to avoid repeating yourself. If you need to do the same thing at two points in your code, you can write a plain vanilla non-OOP function to do that and call it twice.

To summarize the advantages of OOP without writing a book here, OOP basically does three things for you:

  1. Group related data together. Non-OOP programs often have a whole bunch of variables floating around in the main program that are only loosely related. With OOP, you put related variables into an object.

  2. Associate functions with data. By putting functions in an object with the data they operate on (purists will say they are then "members" rather than "functions"), you make it clear to the reader that these go together.

Combining #1 and #2 lets you hide implementation details from other objects. You create the "public interface" for a class, the set of functions that other objects should call and that represent the logical things that this class does, and then any other functions you need can be hidden. (More explicitly in some languages than in others, but that's not the point here.)

  1. Classes can inherit and mutate. If you have two similar classes A and B that should be mostly the same but with some minor differences, you can make a superclass C with all the common stuff and then A and B inherit from that and each adds in its own unique stuff. This is what is usually advertised as the power of OOP. Frankly, yes, it's way cool, and in some situations can be very handy, but I only use its true power occasionally, and I suspect the same is true of most programmers. (OOP enthusiasts feel free to jump in with how and why you use inheritance all the time.)

When to OOP it? Any time you have several pieces of data that logically go together, it makes sense to create a class to hold them. Like X and Y coordinates; or customer name, address, and zip code; or phaser range and phaser power consumption; or whatever.

Any time you have functions that logically operate on this related data, put them in the the class with the data. Like "capitalize customer name", "compute distance of this point from the origin", etc.

How and when to use inheritance is more complicated. I'll leave that for another time.

like image 30
Jay Avatar answered Sep 26 '22 12:09

Jay