No-one disputes that Edsger W. Dijkstra was a genius, and a legend of programming. (Well, no-one except Rob Nagler.) It seems incredible to me that his idea of structured programming was once considered a foolish affectation, or even that there was ever any dispute about it at all: but I remember all too well reading arguments about it in the early-eighties issues of Practical Computing and Personal Computer World. Maybe it was an idea waiting to happen, or at least waiting to be popularised, but it seems we have Dijkstra to thank that the languages we do our work in are largely expressed in block-structured loops and conditionals rather than a maze of twisty little GOTO statements, all alike.

Dijkstra’s Wikiquote page is full of gems, like this one (which accords very nicely with Kernighan and Plauger’s key quote):

“The competent programmer is fully aware of the limited size of his own skull. He therefore approaches his task with full humility, and avoids clever tricks like the plague.”

I am right on board with that. I’m not so sure about his claim that “Object-oriented programming is an exceptionally bad idea which could only have originated in California”, but I find it amusing so I am willing to give him a pass on that one.

But here is where I think he badly misread a common situation:

“It is practically impossible to teach good programming to students that have had a prior exposure to BASIC: as potential programmers they are mentally mutilated beyond hope of regeneration”

O RLY?

First, to be fair, I should note that this quote is taken from a tongue-in-cheek short paper, How do we tell truths that might hurt? (1975) — the same paper that contains the much-misquoted observation “The use of COBOL cripples the mind; its teaching should, therefore, be regarded as a criminal offence”, which is usually given as being also about BASIC. The whole paper is only 670 words long and well worth the couple of minutes it takes to read. It’s pretty clear from context that Dijkstra was exaggerating for comic effect; but it’s also clear that the thing he was exaggerating was something he really felt: that learning BASIC is of net negative value towards becoming a programmer.

But that conflicts with what we actually observe, and however beautiful a theory is it really ought to be checked against reality every now and then. I know plenty of good programmers who cut their teeth on Microsoft BASIC on late-1970s and early-1980s micros. (That’s how I started out myself.) So why haven’t we been mentally mutilated? Dijskstra would probably say that we have been, and that had we escaped early exposure to BASIC, we’d be better then we are. I suppose that’s possible, and it’s not something that can ever be resolved by experiment. But I think the excellent ex-BASIC programmers that I know are excellent because of their exposure to that language.

I’m not arguing here that BASIC was better than it’s been given credit for. It wasn’t. It was horrible. The only available looping construct was the arithmetic FOR I=1 TO 10 loop; the only conditional contstruct was an emasculated single-line IF…THEN that couldn’t control a block; all variables were global and their names were restricted to two significant characters. If we wanted a WHILE loop, we had to build it using IF…THEN GOTO. Subroutines were sort of supported, through the GOSUB statement, but RETURN couldn’t yield an argument, so subroutine protocol had to be via changes in global state. In the snow. Uphill both ways.

And, kids, if you’ve used Visual Basic .NET or some other not-really-BASIC-at-all, please realise that we are not talking about:

Public Function PercentageOf(ByVal value1 As Integer, ByVal value2 As Integer) As Integer Dim result As Integer result = (value1 / value2) * 100 Return result End Function

We’re talking about:

0 sys866:goto10 1 c$=c$+" ":k=0:d$=n$ 2 k=k+1:a$=left$(c$,k):ifright$(a$,1)<>" "then2 3 b$=mid$(c$,k):ifleft$(b$,1)=" "thenk=k+1:goto3 4 ifb$=""thenn$="***":goto6 5 b$=left$(b$,len(b$)-1)

The ancient by-hand while-loop and “maximum-significant-length-two-characters” jobs bear as much relation to Visual Basic .NET as a packet of mixed nuts does to the entire west wing of the Sirian State Mental Hospital.

But I’m arguing that the horribleness of BASIC was its virtue. It forced us to think around corners. It made us think through what the control structures really were, and how they were implemented. Most of all, it made us hold a huge amount in our minds at once. Programming in a language with no local variables means that you need to have a clear mental model of what every single variable is doing at every point in execution, for every possible code path. I am not saying that’s a good way to program. It’s not a good way to program. It’s a horrible way to program, and I am delighted that I don’t have to do it any more. But it’s a fantastic exercise. It develops mental muscles that you’re going to need all through your career and which you’re not likely to develop while doing any of the more productive activities you spend time on.

I think of it like tennis players weight-training. When a top player lifts weights, he’s applying more force than he will ever need to apply in a match. The point is not to make him able to lift, I don’t know, 16 tons or whatever it is that these guys can lift: the point is that when they have to apply a much lesser force in the course of a match, they can do it effortlessly. They have that side of things covered, so their concentration can go into higher-level matters like shot selection and anticipation.

Or think of a classical pianist. Someone playing Chopin’s Etudes will be going through technical contortions that no normal piece would demand; but because he has mastered those etudes, he’s better able to deal with the demands that performance pieces throw at him. (Admittedly this analogy is a bit spoiled by the fact that Chopin was a superhuman genius and his Etudes are glorious pieces of music in their own right as well as being technical studies.)

Anyway: like a weight-training tennis player, or like a pianist who has mastered the Etudes, so if our minds are stretched by dealing with programs stitched together with global variables and GOTOs, we’ll be better equipped to cope with the lots-of-stuff-at-once complexity that our real programming demands we deal with.

Not only that, but I think programming in BASIC stretches the mind in a specific direction that other exercises don’t, and that is a particularly relevant direction in modern programming environments. What I mean is that while the exercise of, say, writing a correct quicksort is valuable for developing the ability to focus right down on a single complex problem, it doesn’t really touch on the ability to think about a hundred things at once. And I feel like I have to do that all the time, thanks to the innumerable libraries and languages that have to come together to make a modern web application. The other day, I found myself working on a hunk of Perl embedded in HTML (by the Mason module), where the role of the Perl was to dynamically generate a fragment of a JavaScript function which was to arrange for specific elements of the HTML form to be set to various values, which were themselves taken by Perl from a database. I don’t think that kind of thing is unusual. But to do it right, you need to think about a lot of quoting levels at once — HTML encoding, Perl’s string encoding, JavaScript quotes. I don’t think any amount of structured programming helps with that kind of frankly messy problem. It’s hard to get right. (As anecdotal evidence in support of that assertion, I offer the information that I got it wrong twice myself before getting it right the third time.)

“But Mike, what about all the bad habits that you learn from a language like BASIC?” Yes, they are a problem. Those bad habits do need to be unlearned, and the trick is of course to dump those spaghetti-code techniques while still retaining the mental muscle-memory that you built up while using them. I guess most of us didn’t find that too hard: programming with WHILE loops comes pretty naturally.

So join me, please, as we raise a glass to the horrible BASIC dialects we grew up with; we curse the bad habits they left us (which we have hopefully long ago left behind) but thank them for having broadened our mental capacity.

And finally …

I leave you with John McCarthy, the creator of Lisp. (From here.)

Update (10 March 2010)

A couple of discussions of this article elsewhere on the web:

Slashdot — some excellent observations here.

Reddit

Not much happening at Hacker News.