When I first came to OOP (at first in Java, and then C++) after years of working in C and VB, it was amazing. The idea of extending existing behaviour and operating generically over things of related types was very appealing, and I almost immidiately saw why it was that I wanted this. That is, my motivation for OOP was subtyping polymorphism and inheritance.
Fast forward to today, and I regularly get to use ad-hoc polymorphism solutions (typeclasses and duck typing) which solve the problems subtyping originally appealed to me for in very elegant ways. I have access to parametric polymorphism in most of my environments. Programming generically with these different sorts of polymorphism gives me all the extensabiltiy and re-use inheritance ever did, and more.
So, my question is: why should I still care about OOP? What uses of OOP have you found that are really great besides inheritance and subtyping?
Also, “because it’s popular and you will have to use it” does not count. I already am quite familiar with OOP, and use it in my day job because the code base uses it. For a new project, or for myself, that’s what I’m asking about.
1
Without getting bogged down into a semantic discussion about precisely defining OOP, I’d say that if you are using polymorphism then (as you note) you are using the main benefit of OOP.
Using type classes or duck typing is really just another way to achieve the same goal. You aren’t using the C++ / Java style of inherited object orientation, but you are still designing around the concept of typed objects. In my opinion this is either another style of OOP (distinct from the C++ inherited style), or is something so close to OOP that it is only nominally different.
Besides polymorphism, what’s so compelling about C++ style OOP? Just the C++ style, if you like that.
2
Besides inheritance and subtype polymorphism, here’s a list with great things about object-oriented paradigm:
- Encapsulation – keeping your data and internal structure hidden provides great flexibility in changing them
- Modularization – Most object-oriented languages are class based which makes the modularization more natural; also each modules should communicate to other modules via well defined interfaces
- Separation of concerns – Each module/class should do one thing; and is easier to identify what it does
- Ease of modeling – domain entities can naturally be modeled into objects
- Exception handling – although not really a feature of OOP, it is associated mainly with OO languages
- Reusability – OO paradigm brought frameworks which is a great way of reusing code
- Feasibility in building large projects – Basically, the object-oriented paradigm is the only feasible way in which we can build large system easily enough
The great advantage of OOP over the others is the idea of modularization; here you have many modules that communicate with each other by sending messages between them. This is a great improvement over the old way of programming were you had data and many subroutines changing the same data.
11
As someone who was never particularly enamored with OOP, I can think of a couple of reasons to use it.
One is, it’s a good match for certain kinds of problems, such as GUI toolkits. That’s not to say that you can’t use other facilities to solve the same problem, just that OOP is well-adapted to it.
Another reason is that, for all the long-standing hype, classic object-oriented languages do provide decent support for programming-in-the-large. Sure, OOP hasn’t got a monopoly on that kind of thing, and you could also look at this as a side effect of “it’s popular”. Nonetheless, it’s a well-tested way of solving that problem, as well.
I don’t know if either of these are what you would call “compelling”, but on the other hand, I’d also say that ad hoc polymorphism is almost as overhyped these days as OOP used to be. Both sets of language features are toolsets which may or may not be particularly useful in a given context. Also, they are frequently useful in combination; there is no point in acting like it’s a contest between them.
3
Besides the answers already given I would like to offer the following benefits:
- It is a well known paradigm so by using OOP appropriately you will allow many other developers to work with your code more easily
- There is a large body of knowledge built around OOP using such things as design patterns
- Most frameworks also assume a certain knowledge of OOP in order to use them
I am sure there are a lot more reasons, but you will note these reasons do not vary much from what you already said as “because its popular.”
Yes you will have to use it to some extent but what I seem to be reading is that it isn’t exciting for you and that you don’t feel compelled to work with OOP outside of your professional work or at least it isn’t a driving force. So to answer your question with a question, why don’t you find something that excites you again in programming?
I got the same way about OOP after being so excited when I started with C++ (waaay back when..) I felt kinda bored and was losing my gumption. I still use OOP daily in my professional work, but I have found great excitement in learning new things such as functional programming (Haskell) for instance. It is new for me and exciting and I get the feeling of learning things new again. Silly? Perhaps, but it has brought some enjoyment to what I do again. Maybe that is the answer you are looking for.