Object-Oriented Programming
Object-oriented programming means a lot of things to a lot of different people. A lot of people debate or argue about "what object-oriented programming means".
The way I see it, there are two things that make programming object-oriented.
Part The First
Suppose I'm writing a system that manages documents. I can access the length of a document by document.length
! If I want to capitalize the document, I call document.capitalize()
. If I want the title of the document, I simply inspect document.title
. This is the essence of object-oriented programming. The alternative to this involves horrible things like manually keeping track of lists of all the attributes of the documents, so you would have titles
, body_texts
, lengths
all as separate arrays, and if you want the title of the first document you ask for titles[0]
or something and ANYWAY THE POINT IS: this would be stupid. So that's the essence of objects.
It's the essence of object-oriented programming! But it's not.
Part The Second
Because really, what makes object-oriented programming awesome is that an object knows itself. When I call document.capitalize()
, then the code that gets executed knows which document to capitalize. That's the important thing!
Part The Unrelated
I kind of hate C/C++ and their legacy for overburdening OOP with this confusing question. When the above was suggested to me, I kind of had this moment when I asked
What? Why is that a big deal? Of coursedocument.capitalize()
knows which document it's talking about - you're asking for thecapitalize
function of that specific document. It shouldn't know that other documents exist!
Asking this question was a sign of how I had learned OOP - because in the Olden Times, you were always confronted with the fact that the function document.capitalize()
doesn't belong to the specific instance that you called it on, but rather it belongs to the class called "Document
" or whatever. And I think, ultimately, that that's a very confusing way for things to work!
Fortunately, it seems like most modern languages (I'm looking at you, javascript and python! Save the day!) take reasonably large strides towards hiding the actual owner of any particular method on an object.