The lies we say about OOP
It's a fresh new day, and the bug has no time to lose. Today's topic: The principles of OOP.
When you ask someone what OOP is they might say:
OOP models real world objects in programming. That's why it was invented. So we could model the world.
But when you think about it, it's not quite true, is it. Didn't procedural programming model real objects? Didn't it take us to the moon by controlling rockets, handling communications in 69'?
The fact is that all programming paradigms model real objects and processes, not just OOP but functional and procedural programming as well.
The value of OOP is not in the fact that it can model the world. All programming languages do that.
After a few years of university someone might answer to the "What is OOP" question like this:
OOP is about:
- Encapsulation
- Inheritance
- Polymorphism
But that's not quite true either, is it?
Procedural languages have perfect encapsulation. They expose no variables at all to us. Functional languages like Haskell make a point of never using variables. So if anything OOP, by exposing public variables weakened encapsulation.
1. Encapsulation
Inheritance is another interesting topic. The GOf book says it best:
Inheritance exposes a subclass to details of its parent's implementation, it's often said that 'inheritance breaks encapsulation' link
Inheritance can be evil and couple your software in a very unpleasant manner. Subclasses have strong dependencies on superclasses. I wouldn't count this among OOP's best features. You can very well write complex software with other forms of code reuse, not using inheritance at all. Functional programming languages prove this.
2. Inheritance
Polymorphism. Now this is an interesting topic. In C there was no easy way to achieve polymorphic dispatch. You could not call the same method on multiple structures and expect each to behave in it's own way without some amount of switches, casts and pain. OO languages make it much easier to have polymorphic dispatch.
3. Polymorphism
Be careful how you define OOP. And that reminds me.
OOP could be going away. It may very well be so. Functional programming is on the rise. We switched to OOP languages by transitioning from C to C++/Java/C# in the 90's. One of these days we may wake up and notice that our much loved OOP languages are at the bottom of tiobe index and we will all be like Crusty. Crusty never got the point of OOP, he liked doing everything in C without all that object overhead because he felt it was slow. Don't be the Crusty of OOP era. Continue learning and revisit your opinions every now and then