top home email rss

evanmeagher

Object-Oriented Sandwich Making

The phrase “object-oriented” is thrown around a lot. It’s a very interesting concept and paradigm, but it can be a hard one for non-programmers to understand.

Imagine that you want to write a recipe for making peanut butter and jelly sandwiches. Imagine that the person you’re writing the recipe for has no idea what a peanut butter and jelly sandwich is, so you have to write explicitly what to do, step by step.

The resulting recipe would be very long and contain a lot of seemingly-trivial tasks that any idiot should take for granted like “find a jar in cupboard labeled ‘Crunchy Peanut Butter’” and “transfer a glob of peanut butter from the jar onto bread using a butter knife.”

To relate this example to programming, the person you’re writing the recipe for is a computer (compiler), the sandwich is a program, and the recipe itself is an algorithm. All a program is is a set of directions telling a computer what to do, step by step.

So, back to object-oriented programming… imagine now that you’re writing the same recipe for a person who knows a bit about the culinary arts. You won’t have to explicitly tell them every step. You’re recipe will be much shorter:

  1. Get 2 pieces of bread, 1 jar of peanut butter, and 1 jar of raspberry jam
  2. Spread peanut butter and jelly onto corresponding slices of bread.
  3. Put two slices together.
  4. Eat! (optional)

This is object-oriented recipe-making. You assume that the person knows what they’re doing and thus you can give higher-level instructions. In object-oriented programming, the program that you write takes advantage of already-written packages of code (called object classes), and allows you to tell the computer what to do without having to go into the nitty gritty details of every complex task.

In the sandwich example, the more complex steps of the second recipe can be abstracted as objects to be used in the sandwich-making algorithm.

Who said algorithms can’t be tasty?

SH @ Enhance Life said,

February 8, 2008 @ 12:38 am

Nice application of object oriented concepts to real life! Any ideas for inheritance and encapsulation ! lol

Evan said,

February 8, 2008 @ 12:50 am

Currently working my way through intro CS courses at the University of Washington, so we’ll see!

Naomi Dunford said,

February 10, 2008 @ 3:22 pm

That’s too funny — I’d never really thought about it that way, but reading your description, I realized how difficult it would be to actually teach the sandwich-making to someone who knew nothing about it. Like, how much PB? How much J? How hard to put the pieces of bread together when you’re done? How gentle is gentle?

I have a little bit more respect for programmers now. :)

RSS feed for comments on this post · TrackBack URI

Leave a Comment

*
To prove that you're not a bot, enter this code
Anti-Spam Image