fbpx

Object-oriented programming, or OOP, is a powerful way of designing software by creating your own types that encapsulate behavior and data. Your types can make use of other types through relationships. When I’m teaching programming, I don’t start out with this topic right away but I do recommend that you learn it as soon as possible because you’ll need to get used to a new way of thinking about your software designs.

Using the WordGuess game as an example, the core essence of OOP allows you to bundle behavior such as the scrambleWord method with data such as the word to be scrambled.

And along with this bundling comes encapsulation which allows you to scope methods and properties so they can have simpler names and to hide some aspects that are really only for methods in the new type.

Listen to the full episode or read the full transcript below.

Transcript

In the WordGuess game that I showed you how to make in the 5-day email course and in episodes 13, 14, and 15 of this podcast, there were just 3 methods, main, playGame, and scrambleWord. There’s not much we can do about main. It’s a method that has to exist. And playGame is a simple method that takes no arguments and returns void. But look at scrambleWord for a moment. It needs a word to be scrambled and returns a scrambled form of that word.

Why do we need to pass the word to be scrambled to the scrambleWord method? Because it’s just a method that has no relation to anything else in the program. One option to avoid passing a word would be to put the word to be scrambled in a global variable. That would allow the scrambleWord method to read its word from the global variable. But it would also make that word available to other parts of the program. For a simple game like WordGuess, there’s not much else to the program to worry about. But for a larger application, this practice will lead to problems. You see, you just don’t have control over what other code might decide to change the value. What if you started playing with the word tree but before you could scramble the word tree, some other part of your code changed the global variable to cow? The player would have a tough time winning that game!

If you’re going to try avoiding passing information to methods, then the information needs to be available somewhere else, that’s for sure. You just don’t want it sitting in global variables.

If only there was a way that we could bundle behavior such as scrambleWord with data such as the word to be scrambled. That’s just one ability that object-oriented programming provides. And this is the core essence of OOP.

What we need is an object to represent the game. This object can have two behaviors for now. One will play the game. And the other will scramble the word.

Here’s another benefit of OOP.

In the original WordGuess game, we had two methods to worry about other than main. Imagine those other two methods were written by another programmer and your job was just to write main. When you want to write the code in main to play the game, you’ll see the two methods, playGame and scrambleWord, and for a moment, you might wonder if you need to call scrambleWord or not. Now there are ways to get around this, but I’m just using this as an example. With OOP, your game object can hide the scrambleWord method itself. This makes sense because main only really needs to call the playGame method. So OOP helps you and other programmers using your code to better understand which methods should be called.

Let’s call our game object Game. Seems reasonable. The playGame method will be a member method of the Game object. That seems a bit redundant, doesn’t it? Why not just call the method play since it’s already part of the Game object?

You can. And that’s another benefit of OOP. You get an extra scope when you create an object like this.

Before we get too far, though, let me clarify some terms. When you create a game object, what I’ve been talking about so far is creating the object type. Or really, just defining the object type. You do this by writing in your code a class.

This is a really important benefit that I cannot stress enough. By creating a class, you just created a new type. Where before, you just had some basic types available to program with such as ints, chars, bools, etc. you can now extend this with your own types. In fact, we’ve been using the string type already and it’s just a class. You have the full power to create your own types just as useful as the string class.

Classes define the structure of your object. What methods will it have? Which methods are available to be called? And what data will it have?

So what we need then is a class called Game that has a public method called play, a private method that we can still call scrambleWord for now, and a private string to hold the word that the player is trying to guess. It’s all wrapped up in a nice bundle that only exposes the single method called play. That’s a lot more understandable.

Well, that’s all for now. There’s just too many concepts in OOP to explain in one episode. Now that you know why OOP is important because of the main benefits it provides, future episodes will explain more specific concepts.