Skip to main content

Section 13.2 A change of perspective

Throughout the earlier chapters, we wrote functions and called them using a syntax such as getGrade(score). This suggests that the function is the active agent. It says something like, “Hey, getGrade! Here’s a float value for you to use.”
In object-oriented programming, the objects are considered the active agents. For example, in our chapter on strings, we used an object-oriented style. We said str.split('/'), which asks the string to split itself into a list wherever there is a slash. This is like saying “Hey, str: please use your split method.”
This change in perspective is sometimes considered to be a more “polite” way to write programming instructions. However, it may not initially be obvious that it is useful. It turns out that often times shifting responsibility from the functions onto the objects makes it possible to write more versatile functions and makes it easier to maintain and reuse code.
The most important advantage of the object-oriented style is that it fits our mental chunking and real-life experience more accurately. In real life our cook method is part of our microwave oven — we don’t have a cook function sitting in the corner of the kitchen, into which we pass the microwave! Similarly, we use the cellphone’s own methods to send an text message or to change its state to silent. The functionality of real-world objects tends to be tightly bound up inside the objects themselves. OOP allows us to accurately mirror this when we organize our programs.