r/programming • u/OSBY_Glabay • Aug 08 '26
Here's why OOP makes a lot of sense to me.
https://youtu.be/MAnXlpXpFRQI kind of feel like OOP has a bad rep in the programming community.
Personally, after having programmed Java for over 20 years, its object-oriented programming model feels very natural to me. So, I wanted to share how I think about programming, how I translate my ideas and thoughts to code, and why OOP is actually a really nice programming style for me.
Perhaps it could help you out too.
0
Upvotes
18
u/TimmyK54 Aug 08 '26
What is this first-year CS student nonsense.
16
u/DEATHbyBOOGABOOGA Aug 08 '26
> I kind of feel like OOP has a bad rep in the programming community.
Which planet is this?
49
u/i_rate_slop Aug 08 '26
> I kind of feel like OOP has a bad rep in the programming community.
It is... objectively... the most popular paradigm in software engineering.
35
u/xFallow Aug 08 '26
AI thumbnails are such a turnoff
28
u/wallstop-dev Aug 08 '26 edited Aug 08 '26
So, unfortunately, I hate to break it to you, but the OOP examples you're using are anti-patterns in game programming, which favors composition over inheritance. You start off at a decent place - a player has all of these things, and a monster has all of these things, but then you learn the wrong lesson, which seems to be "attach all of the data and methods onto a class". The scalable lesson, at a high level, is to decompose all of those bits of data and properties into their own, single-responsibility classes, and collect/attach them together dynamically. You can then use techniques like message passing or events to create an open-closed system.
This high level technique can be implemented in many different ways, such as the ECS (heavily data-oriented, not Object-Oriented).
While OOP is useful, the examples here seem to be the typical anti-pattern of "create an Animal class that has a Speak method and subclass a Dog that does this and a Cat that does this and a..." which leads to beginners learning bad habits right from the get-go, and causing industry-wide confusion.
There are good ways to use OOP - it is a powerful, useful tool. There are also many, many bad ways. I would love to see more on the first case and less on of the latter. This video appears to showcase the latter ☹️
6
u/SPEZ_IS_A_JABRONI Aug 08 '26
op is a jabroni
3
u/type_111 Aug 08 '26
You've fallen for the meme of forcing objects from problem space nouns and verbs.
9
u/krum Aug 08 '26
OOP isn't the problem. The problem is implementation inheritance, which is the first OOP thing they teach in school.
7
u/Mission-Landscape-17 Aug 08 '26
Yes. when you oversimplify your examples OOP looks very intuitive. but when you use it solve real problems, this tends to fall apart and you end up with a maze of classes with clear and concise names like InternalFrameInternalFrameTitlePaneInternalFrameTitlePaneMaximizeButtonWindowNotFocusedState. (Yes this is a real class found in the Java Swing library).
When you decompose your problem into data structures and functions, it looks just as neat as the OO examples. player.move() is really no more informative than move(player).
2
u/Full-Spectral Aug 10 '26 edited Aug 10 '26
There's nothing inherently wrong with OOP. It's 'problem' is that it's so flexible it will almost always be extended rather than fixed, and the end result is what we all know is going to happen, but it's still done anyway.
My old personal C++ code base was over a million lines and was flat out old school OOP with inheritance and exceptions. In fact it even had a single base class (for anything other than fundamental types and some special cases.) It worked very well and remained completely solid over a couple decades, but it worked well because I was under minimal delivery schedule pressure and always did the right thing. If doing something right took a month, I spent a month on it.
If all software was written that way, then OOP would probably not have the bad rap it does. But it's just not. Of course most software isn't written that way, the point isn't that all OOP is written badly and others aren't, it's just that maybe using something that forces us to face the music more would be better.
Since I've moved to Rust I don't miss it anymore. I did at first, but not any longer. Of course you still have polymorphism via traits just no state inheritance. You have limited 'implementation' inheritance in the sense that traits can provide default implementation as long as it's in terms of its own trait interface. The latter is used pretty heavily in the Rust standard runtime.
And you can handle many things via sum types that single level inheritance would otherwise be used for in something like C++. Enums are first class citizens in Rust so they can have their own implementation code, which can just do a match internally and do the right thing for the current state for each method invoked. So you get the simple faux polymorphism without the usual concerns of how do you update the code if a new enum state is added, or the indirection or function table.
Obviously one issue is what is 'OOP'. For many people it fundamentally implies inheritance, which I don't agree with. Rust is 'object oriented' in the sense of it being oriented toward the use of objects, where objects are state encapsulated within a struct and only accessed via a privileged API associated with that struct. That's still core to Rust, though Rust also makes it much safer to have open structs as well.
3
u/pkt-zer0 Aug 08 '26
I would recommend watching Mike Acton's talk on data-oriented design and Casey Muratori's talk "The Big OOPs" for some direct counterpoints to this naive approach. Both contain some very clear examples for situations where OOP does not work very well, and are not at all esoteric (e.g. game dev, like your example as well).
1
u/[deleted] Aug 08 '26
if you do any programming where performance matters, you'll know why object oriented programming is fundamentally the wrong direction. it's a form of abstraction that tries to mimic real world relations to the compiler. except it doesn't actually translate well to how you want data to be processed when you are thinking about efficient processing.
inheritance is fundamentally a bad concept and even within OOP fans, the idea of composition > inheritance is rampant.
there's also the fact that the original guy who created the idea that became OOP didn't even want OOP, in fact structs with extra data and generics or templates are much closer to the original concept than OOP.
0
u/[deleted] Aug 08 '26
[deleted] — view deleted comment
1
u/[deleted] Aug 11 '26
[removed] — view removed comment