Typography to describe iteration

July 3, 2012 at 4:32 am 9 comments

One of the complaints about Python is the use of indentation to define blocks.  A quote from Don Knuth is often used to defend that use:

We will perhaps eventually be writing only small modules which are identified by name as they are used to build larger ones, so that devices like indentation, rather than delimiters, might become feasible for expressing local structure in the source language.

Is it natural to use typography to indicate local structure?  Is it more accessible than curly brace ({}) notation, or BEGIN/END blocks?  Do we understand it easily?

I thought about this Sunday morning when I was at services, using a songbook I’d never seen before. The verses and refrain were indicated like this:

You sing the refrain (the “How great thou art” part) twice, indicated by “(2)” — which is pretty obvious, since it’s clear you wouldn’t sing “(2).”  What’s the “scope” of the repeat?  Well, the refrain is italicized, and so is the “(2).”  So you sing twice the part in italics.

I thought it was interesting to use typography to convey to the general population how to “iterate.” If we could count on the general population to know musical notation, and if the music was provided, there’s a different notation for indicating “iteration.”  The use of typography for defining iteration is meant to be more understandable, more generally accessible.

About these ads

Entry filed under: Uncategorized. Tags: , , , .

Period of transition: Stanford computer science rethinks core curriculum CSU Alumnus First to Earn new Computer Science Teaching Endorsement

9 Comments Add your own

  • 1. Justin  |  July 3, 2012 at 8:32 am

    The main criticism I’ve heard about Python’s use of indentation is not that it is used per se, but that it is not robust. It is easy for text to get mangled by different editors, e-mail services, character encodings, etc. It seems dangerous to allow the possibility that e-mailing your code to someone could change its meaning.

    Suppose you OCR’ed your hymn and copied and pasted it into an e-mail. Would the recipient still be able to sing it properly? It depends highly on how much of the formatting remains intact by factors we have little control over. It seems to me that any system that depends on typography to convey information must take special care to ensure that the typography can survive in all conditions.

    • 2. Mark Guzdial  |  July 4, 2012 at 5:17 am

      To Justin, Rob, and Nathan: Sure, we can get it wrong, but how to communicate typography is a solved problem. How old is TeX? Postscript? PDF? RTF? That typography is messed up by email or printers is a flaw of email or printers. If typography really made things better for the learner or even expert programmer, then we should explore its use. This discussion reminds me of Kent Beck’s comment about Java code, in comparison to having code in the powerful database that is the Smalltalk image: “Source code in files. How quaint.”

  • 3. Rob St. Amant  |  July 3, 2012 at 9:04 am

    I think indentation offers good cues to structure; programmers wouldn’t argue about different conventions if it didn’t provide useful information. And most people seem to be able to follow the structure of an outline, partly based on how points are indented.

    Though as Justin points out, it can easily be munged. I’ve had to think about this issue lately, in working with a typesetter who had no experience with code listings. The line length limit was shorter than I’d expected, and so my Python functions were munged: end-of-line comments that overflowed to the next line; a mixture of spaces and tab stops to replace my spaces; some lines fully justified, with expanding spaces; longer identifiers that broke across two lines, with an underscore acting as a hyphen; lines that should have been equally indented but were just a little bit off. It made me realize how much source code is not like ordinary printed text, and that what an experience programmer sees as “obvious” structure may not be obvious at all to a non-programmer.

  • 4. Nathan  |  July 3, 2012 at 9:05 am

    Yeah, I think indentation is a very good, readable way to define blocks. I also think python’s detractors are wrong, but I can’t deny that the indentation can be an issue sometimes. Where it fails is the same place the use of typography as notation fails. We just don’t have a good, inherently portable way to express formatting yet.

  • 5. Darrin Thompson  |  July 3, 2012 at 3:53 pm

    I don’t know about this statement: “The use of typography for defining iteration is meant to be more understandable, more generally accessible.” I think the use of typography is as much about fitting in with a particular aesthetic and production workflow for print.

    I’ve seen a counter example in “The Book of Psalms for Singing.” http://www.crownandcovenant.com/Psalms_for_Singing_p/cm100.htm They include a few examples of chant. They use simple typography for the chants and it’s charitable to say it works poorly. It’s not singable, even if you have the “tune” memorized.

    It’s admittedly not a great counterexample but I think Western rhyme and meter just happen to work well with simple typography.

    • 6. Mark Guzdial  |  July 4, 2012 at 5:13 am

      Absolutely, one could use typography badly. The suggestion is that it could be better than plaintext.

  • 7. Bri Morrison  |  July 4, 2012 at 11:20 am

    Perhaps where this argument falls short is the fact that most hymns do not also use numeric values or parentheses in their text where as code most definitely does. I can easily see people becoming confused when viewing the random (2) or (100) after a mathematical statement in a loop.

    • 8. Mark Guzdial  |  July 4, 2012 at 11:40 am

      Sure, point is fair. I guess I didn’t convey my point well. It wasn’t that we would use this notation, just that typography can be useful to indicate blocking, to delimit scope, and that it can be fairly clear to the reader — maybe even clearer to the reader than {} or BEGIN/END.

  • 9. Barry Brown  |  July 10, 2012 at 3:31 am

    A little late to the conversation…

    I think it’s a disgrace that 60+ years into the modern computer age, we’re still using text files as the pinnacle of expression. I like the ideas of using other cues to impart structure and meaning: fonts, boxes, colors, sounds, lines, and so forth. The BlueJ editor uses boxes and colors to show scope and it’s incredibly helpful for the students. The coloring also has nice side effects: it doesn’t take them long to learn that if nothing is colored, they forgot to declare the class.

    But back to the typography: a finite number of typographic variations limits how much structure can be created. In your song, only one level of nesting is possible. For a song, that’s fine. For a language, programmers might find it severely limiting. Rather, experienced programmers might not like it.

    Would there be a pedagogical benefit to allow only singly-nested control structures for novices? I think about all the horribly nested for/if/do/while structures that some of my students write; I wonder that if they were required to make methods out of inner structures, would their programs be better designed?


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed

Recent Posts

July 2012
« Jun   Aug »


Blog Stats

  • 940,792 hits

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 3,013 other followers

CS Teaching Tips


Get every new post delivered to your Inbox.

Join 3,013 other followers

%d bloggers like this: