SICP 4: Objects, Functions, and Change

Reading Time: 7 minutes

Screen Shot 2019-11-05 at 10.35.27 AM

I recently took Dave Beazley’s week-long course on The Structure and Interpretation of Computer ProgramsIn this series, I’ll share memorable insights from the course (here’s where you can see all the posts so far).

In this post, we’ll look at some more data representation questions from the second chapter of the book. This set of questions happens to dovetail nicely with some of the material from Crafting Interpreters, so I’ll include some of that here, too.

Let’s talk about programming paradigms and how they affect the way our code bases can change.

Suppose we have two different implementations of a box class.

Bob’s box implementation relies on storing the lower left point, width, and height.

Alice’s box implementation relies on storing the locations of opposite corners of the box.

What happens when we want to allow both of these data representations to work within the same program?

Option 1: Add a type tag to each box

We can attach a specific label to each type of box as a tag, then create methods that switch between the implementations of each type of box after checking that tag. Like this:

Each time we add a new type of box to our program, we would need to add a case to each method that does this type check.

Option 2: Encapsulate tagged operations within each box

In this case, we have each box, upon creation, call a procedure that accepts a type argument, with type now referring to the operation to be run rather than the type of the box.

As Dave Beazley observes, this produces a syntax similar to the object dot method syntax:

Each time we add a new type of operation to our program, we would need to add a case to each box type to execute a message of that name.

See how in both options, we have to go back and change something in the existing code? This has a name: the expression problem.

For this, I appreciate Bob Nystrom’s explanation apropos of a discussion about representing expression types in an object-oriented language. I’m shamelessly pasting the relevant part here for your convenience (sorry, Bob):

For each pair of type and operation, we need a specific implementation. Picture a table:

A table where rows are labeled with expression classes and columns are function names.

Rows are types, and columns are operations. Each cell represents the implementation of that operation for that type.

An object-oriented language like Java assumes that all of the code in one row naturally hangs together. It figures all the things you do with a type are likely related to each other, and the language makes it easy to define them together as methods inside the same class.

This makes it easy to extend the table by adding new rows. Simply define a new class. No existing code has to be touched.

The table split into rows for each class.

But imagine if you want to add a new operationa new column. In Java, that means cracking open each of those existing classes and adding a method to it.

Functional paradigm languages in the ML family flip that around. There, you don’t have classes with methods. Types and functions are totally distinct. To implement an operation for a number of different types, you define a single function. In the body of that you use pattern matchingsort of a type-based switch on steroidsto implement the operation for each type all in one place.

This makes it trivial to add new operationssimply define another function that pattern matches on all of the types.

The table split into columns for each function.

But, conversely, adding a new type is hard. You have to go back and add a new case to all of the pattern matches in all of the existing functions.

Each style has a certain “grain” to it. That’s what the paradigm literally meansan object-oriented language wants you to orient your code along the rows of types. A functional language instead encourages you to lump each column’s worth of code together into functions.

This “grain” thing is interesting because it offers us a context-specific attribute with which to judge whether a particular problem might benefit most from an object-oriented solution or a functionally-oriented solution. But what if we need to be able to add rows and columns? Can we make that easier within either of the paradigms?

We can.

Option 3: Dispatch functions through a table, or registry.

This time we’re going to tag each of our box types the way we did in Option 1, and we’ll then register the type-specific method calls in a table whose keys are lists containing the general operation names (like width) plus the tag for the type of box. These keys point to a reference to the type-specific operation to be performed on a box of that type when the general operation name is called.2

The width and height functions now look up a key containing their name, plus the type tag of the object they’ve been called on, in the hash. So we don’t have to add new cases to those general functions each time we add a new type of box.

This pattern makes it a little easier for us to add new types of objects in functionally-oriented code.

But what if we have object-oriented code?

Option 4: The Visitor Pattern

We didn’t implement this in class. I started to implement this in Scheme at home to show you in a consistent language, but the visitor pattern in Scheme ends up looking enough like Option 1 that it could be confusing as a demonstration. The visitor pattern benefits heavily from the presence of interfaces, which Scheme doesn’t have. So instead I’m going to show this one in Java.

Here’s the setup:

We have an interface that requires implementers to have a method for each of our two class types. We then create a method on each box that accepts a class implementing that interface and calls its the implementer’s dedicated method for this type of class on itself.

Then, all of the behaviors live together in a class that implements the interface:

We still have to provide an implementation of the method for each type, but we can keep the behavior together rather than scatter it across all of those types’ classes.


None of these strategies produces a code base for which it is equally convenient to add both classes and behaviors (though Options 3 and 4 aim to make it easier). Understanding the tradeoffs for each of these strategies allows us to choose a paradigm based on our expectations for how the code base is likely to change in the future.

In the next post, we’ll move into Chapter 3 of SICP: Modularity, objects, and state.

2So we have keys that are lists of the general operation name and the specific box type tag. We can do that because we are representing all this in code…for now. Were we doing this with a SQL database, we’re not allowed to use a list as a key. This exemplifies how some of that key concatenation we talked about in the last post might come in handy.

If you liked this piece, you might also like:

The Crafting Interpreters series

The Listening series

The Guest Lecturing series


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.