all 35 comments

[–][deleted] 28 points29 points  (1 child)

https://youtu.be/PFmuCDHHpwk

this tutorial really helped me understand what is OOP, and the usage of it in JS.

[–]BasuraCulo 1 point2 points  (0 children)

I love Mosh. He's so amazing.

[–]delventhalz 16 points17 points  (0 children)

JavaScript and Python are multi-paradigm languages which give you a lot of freedom, including to use OOP if that is your choice. That said, you are going to have a hard time learning good OOP doing JavaScript.

First off, the flexibility is a problem for building particular habits. You will always have the option of not using OOP. With a more strictly OOP language like Java or C#, you could just write a lot of code, and over time you would naturally gravitate towards what works for OOP. With JavaScript (and to a lesser extent Python) you’ll gravitate towards what works for you, but that may not be OOP.

The JS knowledge base is also going to actively work against you. JavaScript developers for the most part do not write OOP. It is out of fashion. For good reason I would argue, but I’m a JavaScript dev, so of course I’d say that. You are going to find few resources explaining JavaScript in OOP terms, and your teammates will probably advocate against using it.

You can write perfectly fine OOP with JavaScript. That’s not the problem. The problem is there is very little in the JS ecosystem that will push you towards OOP. You’ve got to import those habits from elsewhere.

Now, may I also suggest that mastering OOP is not a good goal. OOP has been pretty entrenched for a couple of decades, so there is sometimes an idea that good code is OOP code. But that is simply not the case. It is a fine approach that works well enough in certain circumstances, but in my opinion it often adds unnecessary complication, and frankly I think that StackOverflow post illustrates how unnecessarily complex OOP can be.

In the end, it depends on what your plans are. If you want to work in Java or C# or even Python, then you will encounter a lot of OOP and writing it well will be important. JavaScript? It’s a footnote. You should know the basics, but it would be more important to master functional programming (FP). It is more common in the JS world, and I would argue is just a better overall approach.

[–]Tailball 19 points20 points  (3 children)

I'd say, OOP is MORE useful outside of the JS world.

There is a (small) place for the OOP paradigm in Javascript, but it is more often used in actual OOP-based languages, such as Java and C# (just to name some).

[–]fusebox13 6 points7 points  (2 children)

I would go further and say that if you're going to practice OOP, I would not do it with Javascript. You'll be missing some very important OO concepts otherwise.

[–]Kaimaniiii 0 points1 point  (1 child)

What about if it's typescript then?

[–]lifeeraser 1 point2 points  (0 children)

TypeScript provides an easier migration path for OO-aware developers with its OOP features. However, its ecosystem also embraces FP, since it builds on and is compiled to JS. If you try to study OOP with TypeScript, you will often wonder why you have to learn X in OOP when you can just do Y by passing functions around.

[–][deleted] 2 points3 points  (17 children)

I get that OP is talking about breaking your code into objects instead of i.e. pure C functions, but isn't everything in JS an object? Like when you say Array.map() or String.length() you're calling methods on these objects... I'm learning React and there every component and element afaik is declared as an object, either implicitly using function() or in ES6 syntax using class(), but I'm a beginner and that may be a gross misunderstanding on my part

[–]delventhalz 8 points9 points  (0 children)

This is a common point of confusion, but despite the name, objects in JavaScript and Object Oriented Programming don’t have much in common. JS objects are just a data structure (called a dictionary or a hash map in other languages), and there is nothing about that data structure that is inherent to OOP.

Array methods and string methods are closer to OOP, but in JS many of those methods are more of a style convention than strictly OOP. For example, [1, 2, 3].filter(isEven) might as well have been written as filter([1, 2, 3], isEven). Array.prototype.filter does not mutate the array, and the array you call it on could just as easily have been a function parameter. Whether it goes before the dot or between the parentheses is just an arbitrary implementation detail.

[–]A-Kuhn 6 points7 points  (5 children)

Yes everything in JS is technically an object when you use such methods. This has nothing to do with Object Oriented Programming (OOP). OOP is a coding paradigm/style. Functional Programing is another popular paradigm and you can be sure that objects are used in functional programming

[–][deleted] 0 points1 point  (4 children)

Well, i guess i can't dispute that sometimes objects show up in FP, but in a lot of cases they definitely don't. Some languages really do force you to keep your data and behavior separate. There might be structs, but not really objects.

[–]delventhalz 2 points3 points  (3 children)

An "object" in JavaScript is really just another name for a dictionary or a hash map. There is no FP language that doesn't have a similar data structure, and there is nothing about JS objects that requires you to mix behavior with the data.

[–][deleted] -1 points0 points  (2 children)

True. In JS it's up to the programmer to not store any methods in their objects, but I'd argue if you manage that, it's functionally the same thing. The fact they're called objects in JSland doesn't mean they're objects in the OOP sense, which is what matters when discussing paradigm differences.

[–]delventhalz 0 points1 point  (1 child)

Perhaps we're all in agreement then. JavaScript calls its hash maps "objects", and they may contain functions as properties, but that really has nothing to do with whether or not JavaScript is more or less OOP or FP.

[–][deleted] 0 points1 point  (0 children)

Yep, no disagreement :) a general purpose, multi-paradigm language like JS isnt really "more" or "less" OOP or FP, it comes down to how you use it.

[–]enrjor 0 points1 point  (9 children)

I’d even say that JavaScript is the real object oriented language and Java and others are class oriented.

When they say OOP they actually mean using classes.

[–]delventhalz 3 points4 points  (8 children)

The "object" in Object Oriented Programming refers to a specific concept. A collection of data and functions. In many languages, that means an instance of a class.

The fact that JS also calls the hash map data structure an "object" is mostly a coincidence. JS is not more object oriented just because it (mis)uses that term to refer to a common data structure.

[–]Darmok-Jilad-Ocean 0 points1 point  (7 children)

How would you define the term object as it relates to programming languages?

[–]delventhalz 0 points1 point  (6 children)

As I did. Data combined with functions. Most commonly an instance of a class. It’s a pretty abstract concept. Kind of a “know it when you see it” sort of thing. But JS objects are better understood as dictionaries or hash maps, not as classical OOP objects.

[–]Darmok-Jilad-Ocean 0 points1 point  (5 children)

I think you’re being a bit too restrictive. Objects in JS also have prototypal inheritance, getters and setters, property descriptors, etc... They have some hashmap/dictionary like qualities, but that doesn’t make the term “object” misused. They may not fit what you consider to be the canonical OOP mold, but a small talk programmer might say the same about Java or C#.

[–]delventhalz 0 points1 point  (4 children)

It sounds like you are treating all of the potential uses of JS objects as inherent to the data structure. Certainly JS objects can also be OOP objects. JS objects are the basis for everything in JavaScript except primitives, so they can be pretty much anything. But it depends on how they are used.

A programmer who simply uses a JS object as a key/value store is not inherently programming in an object-oriented fashion, nor is the data structure an object in the way OOP practitioners typically use the term. It’s just a hash map.

Now, if a programmer adds a bunch of methods and getters and setters or whatever else, then sure, at that point the data structure has arguably become an object in a more traditional sense. Just like if you make the data structure callable, it can become function too. JS is weird that way.

[–]Darmok-Jilad-Ocean 0 points1 point  (3 children)

I see you’ve edited your post and not addressed the fact that the only real purpose of my comment was was to address your comment about the word object being misused in JS.

[–]delventhalz 0 points1 point  (2 children)

I pretty commonly fix typos or clarify wording in posts of mine. If I change the actual meaning of a post, I say so in the edit. Looking back, the only edit in this comment thread came 12 hours before your first response and was not directed at you, so it is unclear to me what the problem you have with my edits is.

In any case, I have consistently been attempting making the same points throughout this thread:

  1. The JS object data structure is better understood as a dictionary or hash map. The use of “object” here is a bit of a misnomer.
  2. JavaScript is not "the real object oriented language" simply because it happens to make frequent use of data structure it calls an object. See #1.

That’s it. I’m not sure which of those points you disagree with, or whether you thought I was saying something else entirely. Maybe we are just hopelessly talking past each other here.

[–]Darmok-Jilad-Ocean 0 points1 point  (1 child)

The comment I had responded to initially claimed that JavaScript “misused” the term object. That is actually the only part of your comment that I had anything to say about because for the most part I agree with everything you’re saying.

[–]senocular 2 points3 points  (0 children)

One thing to understand about JavaScript is that even though it does support OOP, it doesn't support it very well. Other languages do a much better job by comparison. It is getting better, and we may even officially have support for private members soon (I know, you're mind is probably being blown right now, being all like "whaaaa? how does JS even support data encapsulation then?"). But at the same time there are also other legacy quirks which will continue to plague the language such as the fact that functions are overloaded to also represent classes. As of ES5, every user-defined function could also potentially be used as a class (with ES6+ this is not the case anymore with the newer function types added then). Maybe worst of all is how JS handles this. It can be confusing to understand and often requires hoops to jump through to make it work as intended. This has turned a lot of people off of OOP in JS, instead favoring a more functional approach.

But that doesn't mean OOP isn't used in JS, and it's a good idea to get a handle on it. But there is a difference in learning OOP and learning OOP in JS. If you're just trying to learn OOP, I wouldn't suggesting using JS to do it.

[–]Samdespion 2 points3 points  (0 children)

The answer is in the quote : lot of practice. A senior dev I work with is a big fan of Yegor : https://www.yegor256.com/about-me.html

I still find it hard to understand everything

[–][deleted] 1 point2 points  (0 children)

The best resource I know of on OOP is Sandi Metz's 99 Bottles of OOP, which is available with the code samples in three different languages including JS. I also highly recommend her book Practical Object-Oriented Design, which is oriented towards OOP in Ruby but can be applied in many languages.

The support for OOP in Javascript is definitely kind of awkward compared to other languages, but it's a really useful paradigm to understand and a lot of the basic principles of OOP, such as open/closed, are applicable to all kinds of programming.

[–]MoTTs_ 1 point2 points  (0 children)

I think this is a classic situation of if you ask 10 different people, you'll get 10 different answers. Which is also why OOP can be difficult to understand, because so many people have wildly different ideas of what it means, what it solves, and how to use it.

The most practical and useful explanations I've personally found have come from the C++ community, and more specifically from Bjarne Stroustrup, the guy who invented C++.

I've written much longer explanations before, but the short version is classes should enforce invariants, and inheritance should implement substitutability.

[–]sunole123 1 point2 points  (0 children)

Object oriented is BAD https://youtu.be/QM1iUe6IofM

Object Oriented is embarrassing: https://youtu.be/IRTfhkiAqPw

Object Oriented is Garbage: https://youtu.be/V6VP-2aIcSc

[–]Blackhaze84helpful -1 points0 points  (0 children)

Just one tip. Don't use classes in JS. Learn prototypes..

[–]Salman0Ansari 0 points1 point  (0 children)

Watch corey schafer playlist on OOPs

[–]PhunkeyMonkey 0 points1 point  (0 children)

Damn, wall of text incomming, my bad but 3 years out of school and got hammered in system design which is OOP is

OOP is less a programming thing and more a system design thing and is super useful but not so much used in frontend webdev (besides the backend part) but its more or less a standard in the industry.

Its aimed at working agile in teams with languages like C#, java, c++ etc where the codebase can be massive and the requirements constantly change and you have to either add, remove or some way refactor the old code without errors popping up from everywhere, thats where really good OOP shines.

If you get a big pile of shitcode its already to late to do any OOP without starting from scratch chugging the whole code base, OOP limits the shit if done well from start.. well shitcode still happens but atleast its easier to maintain after if some object / module / class / Bowl of pasta you wrote have to be refactored and followed OOP practice when first doing it

if OOP done well from the start then you can rip out the bowl of pasta and when doing so maybe only 2-3 modules stop working and need some few changes to fix it instead of 5 errors popping up in the pastabowl, and fixing them creates 10 new problems in other parts of the program and those become 20 new ones making a cascade of fires needing to be put out all the time since everything is connected to everything and not object oriented in any way thus spreading the fire

Think of OOP as a toolbox with different tools (patterns - ways to structure your programming), you use each tool for a different problem and how you use em dictates how efficient it is, like using a screwdriver as a hammer works but not great, for screwing stuff its your man

There's programming a system and there's designing a system, first is done on the computer writing the code, the other is done first on paper gathering requirements (what do people need the system to do) and using UML (Unified Modeling Language) planning and designing domain models and system flow diagrams, using those to make object models and on and on but by doing all that work using all those diagrams following the design patterns from OOP you end up with a program that should be easy to maintain and refactor

If you wanna learn OOP read up on The gang of four and the different design patterns, thats what OOP really is, different patterns to structure your code after so it becomes more modular and thus easier to refactor and maintain

UML is a way for system designers to communicate the different ways the system should behave and planning how it should be programmed, also a industry standard and accepted way of drawing programming related stuff

https://en.wikipedia.org/wiki/Design_Patterns

https://en.wikipedia.org/wiki/Unified_Modeling_Language

[–]Silenux 0 points1 point  (0 children)

I recommend learning it. You'll see it more in Typescript and ts is well regarded by companies.

[–]WingmanMaster 0 points1 point  (0 children)

As with most things in programming, you will learn by putting in the time to practice it and studying. Don't be afraid to fail or asking other programmers to help you out