Tuesday, 17 February 2015


Dick Pountain/Idealog 240/05 August 2014 10:34

So, 20 years of this column now, and so far I haven't run out of ideas. Of course one good way to achieve that is to keep banging on about the *same* ideas, and I plead guilty to that with a broad grin. My very first Idealog column in 1994 was entitled "OOPS Upside Your Head" (that prehistoric disco reference will be lost on the youth of today) and it expressed my faith in the long-term potential of object-oriented programming, along with my disgust at the misuse of the term by marketeers and the maladroit/cynical implementations by leading vendors like Microsoft, and this 240th column will be about object-oriented programming too, largely because I recently encountered a curious little OOP product that has renewed that faith.

The product is called Scratch and is intended to teach programming to very young children. At first sight it looks like Lego, thus neatly linking the topics of two of my other recent columns. You build programs by dragging different coloured "blocks" into a window, where they will only fit together in certain ways decided by their shapes, thus removing at a stroke the possibility of making syntax errors (probably biggest source of frustration for beginners). Some of these blocks are control structures, some are data objects and some are actions, and what powerful actions they are, a complete multimedia toolkit that lets you create animations with sound (warning: requires Flash) remarkably simply.

You'll not be too surprised to learn that Scratch was developed at MIT's Media Lab - started in 2003 by a team under Mitchel Resnick - and was originally implemented in a modern dialect of Smalltalk. The latest version 2 is written in Adobe's ActionScript. It's free to download from scratch.mit.edu and you use it online via your browser (though you can store the source for your projects locally too). Scratch is most certainly not suitable for professional development as it can only handle small, visually-oriented projects, but what grabbed me forcefully are the principles it demonstrates.

Scratch is based around a little-used style of object-orientation that employs cloning instead of class-based inheritance. Everything you build is an object called a "sprite" which contains all its own code. Every sprite is executable (it doesn't have to be visual) and one of the things it can do is spawn clones of itself at run-time. Clones behave like their parent, but have their own identity and all run concurrently: they're automatically destroyed and garbage-collected once execution ends. Scratch is also event-driven and supports events like keypresses, mouse clicks, sound-level thresholds, and message-passing between different sprites and their clones.

My first impression of Scratch was so Toy Town that I almost walked away, but then the old Byte training kicked in and nagged me to write The Sieve of Eratosthenes benchmark. It took me half an hour to grasp the principles, the program stretched to all of 10 "lines", looked like a colourful Lego picture, and required a leisurely 40 seconds to find the first 10000 primes. I rapidly knocked out some other old chestnuts like Fibonacci and Factorial to convince myself Scratch could do maths, then had the brainwave of reviving an abandoned Ruby project I called Critters, an animated ecosystem in which various bacteria-like species swim around eating each other, recognising their preferred prey by colour. I'd scrapped my Ruby version when the graphics became too tedious, but Scratch got an impressive version working inside an evening, thanks to predefined blocks that detect whether one sprite or colour is touching another, and a built-in sprite editor to draw the critters. That done, my other old favourite - an animated real-time bank queue simulation - submitted equally gracefully.

Scratch has several, deliberate, limitations. It supports lists and parameterised procedures, but neither are first-class objects that you can pass as parameters, which limits the level of abstraction you can achieve. Everything is tangible, thus overcoming another steep obstacle faced by novices (at the cost of failing to teach them abstraction). The only I/O is export and import of text files into lists (surprisingly usable) and the ability to talk to Arduino and Lego WeDo controller boards. While reading up about Scratch I discovered that a group at Berkeley has created a derivative called Snap! which extends Scratch by introducing first-class lists and local procedure variables. I duly tried it and it works well, but to my own amazement I actually prefer the challenges that Scratch poses to an experienced programmer! In our programming world of 2014, every development tool from C++ through Javascript to Python employs OOP, and I no longer need to defend the technique, but Scratch looks to me like by far the most fun way to teach it.  

[Dick Pountain had rather hoped that his second childhood might no longer involve computers - dammit!]

No comments:

Post a Comment