Space sensitive programming
I recently started reading “Introduction to Algorithms”, a classic text on the subject of computer algorithms. Algorithms are described using a home-brewed pseudocode, about which the book says:
We use the following conventions in our pseudocode.
- Indentation indicates block structure. … Using indentation instead of conventional indicators of block structure, such as begin and end statements, greatly reduces clutter while preserving, or even enhancing clarity. 2
Sounds good. But before we get too excited, the referenced footnote says:
2In real programming languages, it is generally not advisable to use indentation alone to indicate block structure, since levels of indentation are hard to determine when code is split across pages.
I’ll stick with Python’s conventions on this one. I can take the page-split hit — which I’d never noticed, to be honest. Reduced clutter. Enhanced clarity. Win win.
Feedback
-
The indentation footnote is odd isn't it? Do 'begin' and 'end' help you determine block structure when code is split across pages? No they don't.
Things that would help:
- printing inner blocks in a progressively different colour (eg, more grey or more red). I've seen this done in an IDE.
- print the indentation level in the margin.
- not splitting code blocks across pages (which is surely totally feasible for a book, but maybe not for code that's just printed out).
-
To be honest, I think the authors found themselves trying to explain away why most programming languages don't use indentation alone to indicate block structure. I think Python has proved it's not only workable, but also a good idea.
It's a while since I've actually printed out code -- something which used to be commonplace. It's a while since I've seen a form-feed character in source files -- again, something which used to be more commonplace.
My personal code-style avoids too many levels of indentation. I go for short functions, container operations (e.g. use of map, filter, list comprehensions, STL algorithms).
One thing I do understand is that adding sophisticated page-breaking logic to printed documents can be tough. I guess programming authors who really care have to hack this themselves, sometimes even invent their own type-setting systems.