Untangling the Meaning of Object Oriented Programming

xkcd source

This post began as a response to a teacher who posted in the Unity Teach Community Facebook group that he had been advised by college video game teacher that object oriented programming (OOP) is a key skill needed for success in collegiate video game programming.

OOP was, in part, a reaction to increasingly larger code bases and increasingly more programmers on projects. Creating OOP can feel unnecessarily verbose for small programs (it often is). Using OOP can often feel like calling magic functions that do things with no understanding of what is happening or how it is happening. I’m brand new to Unity, but already seeing this problem. If the goal is to create video games, no worries, if the goal is to educate students on how OOP works, there might be some concern.

OOP can be tough to crack because the term has conflated a lot of different concepts and taken on somewhat of a religious tone in certain circles. Let me try to break down the different meanings.

OOP is sometimes and incorrectly conflated with Structured Programming. Structured Programming is ReallyGood<tm> stuff and is easier to learn with a pure (read not OOP) procedural language. It is, however, quite possible to write structured code in an OOP language where you end up with procedural Java or C# or C++. This aspect of the definition is often just assumed, though, and rarely explicitly discussed.

OOP also has the concept of data hiding sometimes referred to as “black boxes”. This is one step beyond Structured Programming in that you build black boxes that take input and produce output and the programmer using the boxes do not need to know what is going on inside. This is a wonderful thing and it allows programmers to use libraries without having the slightest clue of how they internally work. There is, however, overhead (both code performance and increased coding time) in creating libraries this way and there is a very real argument that merely teaching students to use black boxes is to short change them on the ability to create them.

Next we have inheritance which is a very abused but very powerful concept that is especially powerful in video game programming. Imagine you have a sprite. It can go up, down, left, right. It has an image associated with it. Now you want to create a tank in your game. Tanks can do everything a sprite can do and more, say shoot another sprite as a projectile. So you inherit all the things a sprite can do, don’t have to recreate that particular wheel, and add only the new things the tank is and does.

Mind blowingly powerful when used correctly in video game programming and other programming fields. Painful when twisted for unsuited uses.

Lastly, OOP is about data protection aka public and private (and protected and a few other things better left for another time). This is related to the concept of black boxes. Data protection provides the opaqueness that is needed to protect the inside of the boxes from the outside world.

It would be nice to just be able to set the rules for libraries in a procedural programming language and tell the other programmers “use this, don’t use that, assume this, don’t assume that”. Reality is programmers rarely follow those rules and history is rife with programmers taking advantage of “undocumented features”. So OOP actually allows you to enforce some set of the rules on programmers. Some things are public, fair game for any use by anyone, some things are private, that is inside the black box and, even if you can figure out how it works, you can’t directly access it.

I’ll add that there is a more pure definition OOP coming out of the SmallTalk world, primarily, where the entire goal is to pass messages from one object to another to enforce some of the above. This is largely a theoretical approach and isn’t so consistent with modern Java, C++, C# and the like. Objective C was somewhat in this vein. Honestly, Objective C just gave me a headache.

Learning or teaching OOP can be made much easier by breaking each of the above parts into separate components as one tends to be overwhelmed by all the concepts that get thrown around. With all that said, to loop around to the beginning, OOP is a tool and sometimes a very useful tool (especially in video game programming) but it is a screwdriver and doesn’t help so much driving nails.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: