6

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

4 Answers 4

4

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
  • 1
    So you're saying any ad-hoc or parametric polymorphism, such as via type classes in Haskell, type abstractions in Hindley-Damas-Milner type systems, generics, C++ templates, duck typing, multi-methods, etc. is essentially OOP? Lots of people from lots of different backgrounds would beg to differ. And I don't think this is a particular useful definition (the term "OOP" would be a redundant synonym).
    – user7043
    Apr 30, 2013 at 20:35
  • Well I'm not saying OOP is polymorphism; i.e. they are not isomorphic. OOP is an approach or paradigm, and polymorphism is an attribute of the output of that approach. But I am saying that any approach focusing on objects and typing with a view to achieve polymorphism is merely another way to achieve polymorphism. And I don't believe there is much more to OOP other than it being a way to achieve polymorphism. Apr 30, 2013 at 20:45
4

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
  • 1
    +1 this is the best answer so far. Modularity is IMO the killer feature of OOP. Polymorphism is merely neat. May 1, 2013 at 5:13
  • 2
    @JohnCartwright: Modularity is great, but it isn't at all unique to OOP. IMHO, OOP doesn't even do that great a job of modularity with getters/setters and inheritance being so popular. May 1, 2013 at 8:04
  • 1
    @m3th0dman: Can you elaborate on the feasability of building large projects? I've seen this claimed a lot, but I've never seen an explanation that made sense. May 1, 2013 at 8:05
  • @MichaelShaw that's true indeed. I think the OOP model (done right) is nicer than many of the alternatives. May 2, 2013 at 4:44
  • @MichaelShaw The modularization in the way programming was performed in a manner in which there was global data and modules performing operations on the global data; you had algorithmic decomposition instead of object-oriented decomposition. Regarding the feasibility, there were (and still are) large projects built in other paradigms but this are very few; it is related to the modularization and decomposition which are far superior in an object oriented approach. See the first section of Object Oriented Analysis and Design by Booch et Al. for a better understanding.
    – Random42
    May 3, 2013 at 14:07
3

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
  • "Also, they are frequently useful in combination" How? It seems to me that subtyping and ad-hoc solve the same problem.
    – singpolyma
    Apr 30, 2013 at 21:05
  • They can be used to solve many of the same problems. So can hammers and screwdrivers. Apr 30, 2013 at 21:08
  • In particular, C++ has both subtyping OOP and ad-hoc templates with duck-typing. They are frequently useful in combination; and when they are not, it is generally clear which is most appropriate for a given problem. Apr 30, 2013 at 22:03
0

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.