Thursday, June 19, 2014

Programming as Storytelling

Code tells a story. When I hear someone talking about telling a story in code I tend to think of tools like Alice, Kodu and Scratch. After all those are tools with lots of pretty pictures and commands that seem to scream “use me to tell a story.” But really any program tells a story of one sort or another.
Take for example this bit of code.
          PictureBox p = (PictureBox) sender;
            if ((int)p.Tag == 0)
            {
                p.Tag = 1;
                p.Image = Image.FromFile("redot.png");
            }
            else
            {
                p.Tag = 0;
                p.Image = Image.FromFile("bluedot.png");
            }

There is a story here. The story is a simple one. Let’s make the dot blue if it is red and make it red if it is blue. It’s part of a larger story of lets manipulate some dots on a form to make it look like they are rotating around in a loop.


image


Like any good story it has characters. In this case the characters are a bunch of dots that can either be blue or red on color. The dots don’t actually move in this story but the pretend to move by changing colors in a synchronized manner.

We have a plot of sorts.It has a beginning – a button is pressed. Every fraction of a second a dot takes on the color of a neighbor dot in a pattern. There is an end to it – the button is pressed again or the program is terminated. OK it’s not much of a plot but then it is a simple program.

Larger more complicated programs have larger more intricate stories. A book piece of software even has a back story based on the types of people who will use it and what they need it to do. Some development groups layout these backstories and great detail with names and descriptions of different types of users to keep in mind during the development.
 
At any point while reading a piece of code one can ask “what happens next?” and “how did we get here?” What is this character (variable, method, data structure) and how do we expect it to act? What are its traits? What is its reason for being? Have we described it carefully enough so that it can act out its proper role in our story? Does our story hang together and make sense?

Part of teaching programming is teaching students how to tell the story in code. And to understand the stories someone else has written. This is not the normal, natural language, way of storytelling but that doesn’t mean there isn’t a story there.

No comments: