Mimsy Were the Borogoves

Hacks: Articles about programming in Python, Perl, PHP, and whatever else I happen to feel like hacking at.

Learning to program without BASIC

Jerry Stratton, June 19, 2007

Happy Birthday

“Come on, you can type harder than that!”

While browsing around the net today I ran across the Wikipedia article on BASIC, which led me to David Brin’s September 2006 article blaming lack of a BASIC-like language for why Johnny can’t code:

BASIC used to be on every computer a child touched—but today there’s no easy way for kids to get hooked on programming.

I have a nostalgia for BASIC too, but Brin is way off base here. BASIC was never the reason for us getting hooked on programming, it was the act of programming itself and the uses to which we put it. We learned programming on BASIC in spite of BASIC, not because of it. BASIC was popular for three reasons, and none of them had anything directly to do with BASIC itself.

It was the only choice.
To paraphrase Henry Ford, home computer hobbyists could program in any language we wanted, as long as it was BASIC. If you wanted to write software for your computer, you had two choices: learn to program in BASIC, or learn to program in machine code. And I’m talking machine code here, not assembly language. If we wanted fast code, we literally had to look up the opcodes and POKE them into memory. Using BASIC, of course. Later, people would write assemblers in BASIC for some computers, and you could also buy expensive assemblers or compilers for languages like FORTRAN, but BASIC was the only high level language available to most people. We will never return to that level of universality, nor would we want to.
We had to.
Computers didn’t come with software that did anything useful other than let you write software yourself to make it do something useful. When we wrote programs in BASIC, we didn’t do it because we wanted to write programs in BASIC. We did it because we wanted our computers to do something useful that they otherwise would not be able to do. That’s why we stuck with it. Did you want to balance your checkbook? You wrote a program for it. Needed a calculator or address book? There was no widget or desk accessory to handle the job. You wanted it, you wrote it yourself. Later, you could buy it from someone else who wrote it, but that someone else was probably only a few months ahead of you, and their software was in BASIC too.
Other people used it.
Because it was the only choice, and because we had to, a community built up around BASIC. Local computer group gurus all knew it. It was the only language that they could assume people had available to them. Magazines printed software written in it, so you learned it as you typed in other people’s computer programs. Computer magazines at the time were often little more than page after page of BASIC code to type in. I owned a TRS-80 Model I. That meant I subscribed to Wayne Green’s 80-Microcomputing. In its heyday, it ran over five hundred pages an issue, twelve issues a year.

That was what made BASIC useful to us. We programmed using it, and textbooks used it for samples, because it was the only option at the time. When other options became available, we stopped using BASIC. BASIC didn’t die out because manufacturers stopped including it with computers. Manufacturers stopped including it with computers because nobody—not even kids—wanted to use it once they had better options.

Even if manufacturers were to start including BASIC with every Macintosh, Windows, and Linux computer, nobody would use it. It’s no longer the only choice and it is no longer necessary (or even useful) for creating useful software on our computers. BASIC would still disappear from textbooks as a waste of space.

Brin goes on to complain that scripting languages like Perl are too high-level compared to BASIC, that they “hide the algorithm”:

The “scripting” languages that serve as entry-level tools for today’s aspiring programmers—like Perl and Python—don’t make this experience accessible to students in the same way. BASIC was close enough to the algorithm that you could actually follow the reasoning of the machine as it made choices and followed logical pathways. Repeating this point for emphasis: You could even do it all yourself, following along on paper, for a few iterations, verifying that the dot on the screen was moving by the sheer power of mathematics, alone. Wow! (Indeed, I would love to sit with my son and write “Pong” from scratch. The rule set—the math—is so simple. And he would never see the world the same, no matter how many higher-level languages he then moves on to.)

I don’t even get what he’s saying here. Perl has the same structural commands that BASIC does. You can even use GOTO if you want to. Further, if he’s writing Pong in BASIC, it’s no longer universal. Even that simplistic level of graphics required different code for each machine. And not only can you still follow along on paper “for a few iterations”, some of us still recommend it when we’re teaching PHP or Perl or any of the other modern tools.

But further, BASIC was not easy to follow. It was easier to use than the alternative: machine code or (at best) assembly. Looking at BASIC programs for this article, I was struck by how much it resembles FORTRAN and by how much it resembled alphabet soup. And by how hard it was to determine what a given program actually did. Yes, I cut my teeth on BASIC, and I can still remember DELETE and GOTO. But these programs are not easy to read. I took this short example from the Wikipedia page:

  • 10 INPUT "What is your name: "; U$
  • 20 PRINT "Hello "; U$
  • 30 REM
  • 40 INPUT "How many stars do you want: "; N
  • 50 S$ = ""
  • 60 FOR I = 1 TO N
  • 70 S$ = S$ + "*"
  • 80 NEXT I
  • 90 PRINT S$
  • 100 REM
  • 110 INPUT "Do you want more stars? "; A$
  • 120 IF LEN(A$) = 0 THEN GOTO 110
  • 130 A$ = LEFT$(A$, 1)
  • 140 IF (A$ = "Y") OR (A$ = "y") THEN GOTO 40
  • 150 PRINT "Goodbye ";
  • 160 FOR I = 1 TO 200
  • 170 PRINT U$; " ";
  • 180 NEXT I
  • 190 PRINT

Is that really easier to follow than this example in Python?

[toggle code]

  • #ask them how many stars they want to see
  • #then show them how many stars they asked for
  • def showstars():
    • starcount = raw_input("How many stars to you want? ")
    • starcount = int(starcount)
    • stars = ""
    • for counter in range(starcount):
      • stars = stars + "*"
    • print stars
  • #ask them if they want to see more stars
  • def morestars():
    • wantmore = ""
    • while not wantmore:
      • wantmore = raw_input("Do you want more stars? ")
    • if wantmore.lower()[:1] == "y":
      • return True
    • else:
      • return False
  • name = raw_input("What is your name? ")
  • print "Hello", name
  • showstars()
  • while morestars():
    • showstars()
  • print "Goodbye",
  • for counter in range(200):
    • print name,
  • print

I’m pretty sure I got everything right, but not completely sure, because I couldn’t get the BASIC version to run in BWBASIC. It kept dying on line 140. Yeah, BASIC came on every computer, but it was never universal. Each computer had its own flavor (even when the flavors were all written by Microsoft). Even simple BASIC programs weren’t guaranteed portability. That’s part of why I subscribed to a TRS-80 computer magazine and not some universal computer magazine.

What is today’s BASIC?

Today there are a whole lot of languages that can take the place of BASIC’s functionality without inheriting its drawbacks. We no longer have to program in BASIC. Mac OS X comes with AppleScript, Java, JavaScript, Perl, PHP, Python, and Ruby built in. If you want to install the free developer’s package, you can also get C, C++, Objective C, AppleScript Studio, and more Java. If you want to download something else, the sky is pretty much the limit for Mac, Linux, and Windows. Every computer with a web browser has JavaScript built in, and all of the major scripting languages are available for download if they aren’t already installed.

Muddy Pig Simulator

Find the algorithm in this PEEK and POKE hell if you can. A muddy pig is the perfect metaphor for all BASIC programs that did anything useful.

And these choices are better and easier to use than BASIC was. Recursion and subroutines no longer drain CPU time and RAM: we have both to spare. Just about every language in the list even supports objects, something that CPU killers in the eighties like PL/I never even dreamed of. Some of the scripting languages can even be used to create cross-platform scripts.

Since there are a lot of choices, you can look for languages that help you do something useful and that have a community behind them. Finding a community isn’t difficult. With the Internet, even the crappiest scripting language or programming language has a community larger than anything we could have tapped into back in 1982. So the important criteria is, does this language do something useful for you? Does it do something that will make you want to stick with it after you start learning it?

If you don’t find the language interesting, and if it doesn’t do something for you, then you won’t likely be able to learn programming with it. We learned programming in BASIC because we could produce useful (at the time) software using BASIC. Whatever language you choose, it should be one that lets you produce something you find useful and interesting. Pointless exercises don’t create a love of programming.

It will also help if the language can provide instant feedback.

Persistence of Vision
To my mind, one of the best scripting languages for learning programming today is the Persistence of Vision 3D raytracer. If you think you would enjoy creating surreal or photorealistic images, but can’t draw like Dali or the Hildebrandts, you should check this out. I’ve written a basic tutorial, and have other tutorials on-line, but its claim to fame is a simple scripting language that lets you program 3D imagery. If that appeals to you, this is a great way to learn programming.
If there are repetitive tasks you would like your Macintosh to do, AppleScript is a great choice for learning programming. It uses an English-like syntax that has you typing commands such as “tell application "Firefox" to Get URL "http://www.hoboes.com/"”. If you want to make the Finder do something, you can create a script automatically by telling Script Editor to record your actions as you do them. It will record your actions as script steps, which you can then modify as needed. AppleScript is a great way to tie multiple applications together on Mac OS X, or to automate some repetitive task in an application. I use it on GraphicConverter, for instance, to automatically resize album covers. And I use it in iTunes to play a song backwards. Feel like going further? Use AppleScript Studio to write a custom web browser with your own controls and backgrounds. What kid wouldn’t love that?
Just remember that your AppleScript scripts, unlike the other choices here, will only run on the Macintosh.
If you are already making web pages, but need to add some dynamic content to them, PHP can be a great choice to learn programming. With PHP, you take an already-existing web page and insert PHP code in the location on the page where you want things to change automatically. You can use PHP with most hosting services that charge you money, and you can also use it directly on your computer.
If you’re making web pages and want to add some dynamic display to them, JavaScript can be a great choice to learn programming. With JavaScript, the script runs in your browser, or in the browser of the person downloading your web pages, which means that any web page on any host can have JavaScript built into it. Besides building functionality into web browsers, you can also, on Mac OS X, use JavaScript to create Dashboard widgets. Widgets are little more than HTML, CSS, and JavaScript—and you can leave out the CSS and JavaScript while you’re learning the basics. Or, you can (on Mac OS X) download JavaScript OSA and write JavaScript in Script Editor.
Python: Django or Snakelets
If you’re the kind of person who can ignore the stuff you don’t understand until such time as you understand it (and if you’re not, BASIC was never useful for you), then you might find Django useful. At least on Mac OS X it is easy to install, and you can create some very useful web models without really understanding Python. As you learn more Python, you can create ever more detailed web sites using it. If you want to dive into Python immediately, Snakelets can get you going with a simple web application server. You can easily use Python, then, to create a simple blog or other web application that you can use and improve. If you’re interested, look at O’Reilly’s Learning Python. It’s a great tutorial.
Second Life
For all Brin’s complaints about “dressing up avatars”, programming avatars can be a whole lot of fun. I used to do it with MOO, but that was text-only. Today’s budding programmers will find the scripting languages built in to on-line worlds such as Second Life more interesting and more useful—and more likely to keep them programming than pointless exercises.
If you desperately need something BASIC-like, Perl will do you well. You can program in a very BASIC-like manner if you wish; you can also conceptualize your programs in different ways: line-by-line, as objects, or as a text sieve. Take a look at my own Perls Before Swine for a simple tutorial. However, while I find Perl very useful, I don’t necessarily recommend it for beginners. It can be as hard to understand and follow as BASIC was.

What isn’t today’s BASIC?

  • A language designed only for learning is not today’s BASIC.
  • A language that pretends to emulate how the computer works by jettisoning basic programming concepts is not today’s BASIC.
  • A language that artificially holds back its users by requiring them to program in arbitrarily tiny steps for no apparent reason is not today’s BASIC.
  • A language that ties itself to what textbook writers thought was current years after it wasn’t, isn’t today’s BASIC.

We’ll never be able to go back to the world where everyone used BASIC—or any single programming language—and we wouldn’t want to. That doesn’t mean we don’t have universally-available languages. JavaScript is available on any computer with a web browser; and Perl and Python are available pretty much on any computer either through a download or pre-installed. We don’t need a language designed solely for learning, and we never did.

If we create a language that we don’t expect people to use for real software, then it isn’t today’s BASIC. When BASIC was installed on most computers, our expectations were lower: our computers didn’t come with any pre-installed software. The software we could write with BASIC was software that filled a purpose on our computers. If we want a language to “replace” BASIC today, it needs to be a language that people want to use to make useful software.

If programming snippets disappear from textbooks, it isn’t going to be because of the lack of a universal language. It will be because of a fear of letting kids know that they can run their own software; programming requires free-time play. More and more, that is being discouraged. Give kids free time, and an unrestricted computer to use it on, and some of them will use it to play around with programming.

  • When I was seventeen
  • It was a very good year.
  • It was a very good year for GOSUB routines
  • and POKE and PEEK soup.
  • Our GOTOs would loop
  • and CLS cleared the screen
  • When I was seventeen.
  • When I was twenty-one
  • It was a very good year.
  • It was a very good year for HyperCard stacks
  • that kept my addresses stored.
  • ’til XCMDs corrupted my fork,
  • and events came undone
  • When I was twenty-one.
  • When I was thirty-five
  • It was a very good year.
  • It was a very good year for HTML
  • and Perl CGIs.
  • Thought I’d print ’til I died.
  • Then PHP arrived.
  • When I was thirty-five.
  • But now my DEFs grow short.
  • I’m in the Python of my year
  • and now I write my scripts as packages
  • in a file-based scope
  • from local to import
  • It traces sweet and clear.
  • It is a very good year.
  1. <- Three-fold PDF
  2. Cleaning iTunes ->