Programming in Smalltalk for Spanish readers

Squeak, a World to LearnDiego Gómez Deck is finishing a new book entitled Programando con Smalltalk (Smalltalk Programming), which guides readers new to Smalltalk (but not to programming) into the use of the language and the Squeak environment. The book will be published under a CC license, and its drafts are available online. I just skimmed over the latest draft, but seems a very interesting reading (if you read Spanish, that is; translating Diego’s book to English would be an interesting project). Diego has already coauthored a previous Spanish Squeak book, which was published last year: just click the image of its nice cover for more info.

This book joins the uprising of Squeak’s popularity in Spain, driven by its being used by the local government of Extremadura as the platform for some of its projects. The last one consists of an interactive educational and reference tool that will let users of the Extremadura’s Regional Net to know more about a bunch of topics on physics, chemistry, biology, ecology or society, to name a few. Actually, the tender is still open, in case you feel like trying. Cees’ Blog provides the necessary info (this time in English), including some reasons to not really wasting the time applying.

Tags: ,

Programmers love writing (and mocking) tests

Aggressive unit testing (also known, by those willing to earn some easy money, by the buzzword test-driven development) is about the only practice from XP that i embraced happily in my days as a dot com employee (it was around 2000, and i was working for one of those companies that got lots of funding from avid but hopelessly candid investors to do… well, to do basically nothing).

Those were my long gone Java days, and i got test infected all the way down. That writing tests is a good practice is hardly news for any decent programmer, but what i specially liked about putting the stress on writing lots of tests was discovering how conductive the practice is for continuous code rewriting (count that as the second, and last, extreme practice i like). I had yet to discover the real pleasures of bottom-up development in REPL-enabled languages, but even in Java my modus operandi consisted basically in starting with concrete code (sometimes even a mere implementation detail) and make the application grow up from there. Of course, some brainstorming and off-the-envelop diagramming was involved, but the real design, in my experience, only appears after fighting for a while with real code. The first shot is never the right one, nor the second, for that matter. The correct design surfaces gradually, and i know i’ve got it right when unexpected extensions to the initially planned functionality just fit in smoothly, as if they had been foresighted (as an aside, i feel the same about good maths: everything finds its place in a natural way). When you work like that, you’re always rewriting code, and having unit tests at hand provides a reassuring feeling of not being throwing the baby with the bath during your rewriting frenzies.

Of course, there’s also a buzzword-compliant name for such rewritings (refactoring), and you can expend a few bucks to read some trivialities about all that. (Mind you, the two books i just despised have been widely acclaimed, even by people whose opinion i respect, so maybe i’m being unfair here—in my defense, i must say i’ve read (and paid) both of them, so, at least, my opinion has cost me money.)

Sunit in SqueakAnyway, books or not, the idea behind the JUnit movement is quite simple: write tests for every bit of working code you have, or, if you’re to stand by the TDD tenets (which i tend not to do), for every bit of code you plan to write. As is often the case, the Java guys where not inventing something really new: their libraries are a rip-off of the framework proposed by Kent Beck for Smalltalk. Under the SUnit moniker, you’ll find it in every single Smalltalk implementation these days. A key ingredient to these frameworks’ success is, from my point of view, their simplicity: you have a base test class from which to inherit basic functionality, and extend it to provide testing methods. Languages with a minimum of reflection power will discover and invoke those methods for you. Add some form of test runner, and childish talk about an always green bar, and you’ve got it. The screenshot on the left shows the new SUnit Test Runner in one of my Squeak 3.9 images, but you’ll get a better glimpse of how writing unit tests in Squeak feels like by seeing this, or this, or even this video from Stéphane Ducasse‘s collection.

Of course, you don’t need to use an object-oriented language to have a unit testing framework. Functional languages like Lisp provide even simpler alternatives: you get rid of base classes, exchanging them by a set of testing procedures. The key feature is not a graphical test runner (which, as any graphical tool, gets in the way of unattended execution: think of running your tests suites as part of your daily build), but a simple, as in as minimal as possible, library providing the excuse to write your tests. Test frameworks are not rocket science, and you can buy one as cheap as it gets: for instance, in C, i’m fond of MinUnit, a mere three-liner:

/* file: minunit.h */
#define mu_assert(message, test) do { if (!(test)) return message;  \
                                    } while (0)

#define mu_run_test(test) do { char *message = test(); tests_run++; \
                               if (message) return message; } while (0)

extern int tests_run;

(Let me mention in passing, for all of you non-minimalistic C aficionados, the latest and greatest (?) in C unit testing: libtap) Add to this a couple of Emacs skeletons and an appropriate script and you’re well in your way towards automated unit testing. From here, you can get fancier and add support for test suites, reporting in a variety of formats, and so on; but, in my experience, these facilities are, at best, trivial to implement and, at worst, of dubious utility. It’s the quality and exhaustiveness of the tests you write what matters.

Lisp languages have many frameworks available. The nice guys of the CL Gardeners project have compiled a commented list of unit testing libraries for Common Lisp. In Scheme you get (of course) as many testing frameworks as implementations. Peter Keller has written an R5RS compliant library that you can steal from Chicken. Noel Welsh’s SchemeUnit comes embedded into PLT, and the Emacs templates are already written for you (or, if you mileage varies and you’re fond of DrScheme, you can have a stylized version of the green bar too). Personally, i don’t use PLT, and find Peter’s implementation a bit cumbersome (meaning: too many features that i don’t use and clutter the interface). Thus, my recommendation goes to Neil van Dyke of quack‘s fame’s Testeez. Testeez is an R5RS (i.e., portable), lightweight framework that is as simple as possible. Actually, it’s simpler than possible, at least in my book. In my opinion, when a test succeeds it should write nothing to the standard (or error) output. Just like the old good unix tools do. I only want verbosity when things go awry; otherwise, i have better things to read (this kind of behaviour also makes writing automation and reporting scripts easier). So, as a matter of fact, I use a hacked version of Testeez which has customizable verbosity levels. It’s the same version that we use in Spells, and you can get it here. Also of interest are Per Bothner’s SRFI-64, A Scheme API for test suites and Sebastian Egner’s SRFI-78, Lightweight testing (both including reference implementations).

Lisp testing frameworks abound for a reason: they’re extremely useful, yet easy to implement. As a consequence, they’re good candidates as non-trivial learning projects. A nice example can be found in Peter Seibel’s Practical Common Lisp (your next book if you’re interested in Common Lisp), which introduces macro programming by implementing a decent testing library. In the Scheme camp, Noel discusses the ups and downs of DSL creation in an article describing, among other things, the SchemeUnit implementation. A worth reading, even for non-beginners.

Once you settle on a test framework and start writing unit tests, it’s only a question of (short) time before you’re faced with an interesting problem, namely, to really write unit tests. That is, you’re interested in testing your functions or classes in isolation, without relying on the correctness of other modules you’ve written. But of course, your code under test will use other modules, and you’ll have to write stubs: fake implementations of those external procedures that return pre-cooked results. In Lisp languages, which allow easy procedure re-definition, it’s usually easy to be done with that. People get fancier, though, specially in object-oriented, dynamic languages, by using mock objects. The subject has spawn its own literature and, although i tend to think they’re unduly complicating a simple problem, reading a bit about mockology may serve you to discover the kind of things that can be done when one has a reflective run-time available. Smalltalk is, again, a case in point, as Sean Mallory shows in his stunningly simple implementation of Mock Objects. Tim Mackinnon gets fancier with his SMock library, and has coauthored a very interesting article entitled Mock Roles, Not Objects, where mock objects are defined and refined:

a technique for identifying types in a system based on the roles that objects play. In [9] we introduced the concept of Mock Objects as a technique to support Test-Driven Development. We stated that it encouraged better structured tests and, more importantly, improved domain code by preserving encapsulation, reducing dependencies and clarifying the interactions between classes. [...] we have refined and adjusted the technique based on our experience since then. In particular, we now understand that the most important benefit of Mock Objects is what we originally called interface discovery [...]

An accompanying flash demo shows SMock in action inside Dolphin Smalltalk. The demo is very well done and i really recommend taking a look at it, not only to learn to use Mock Objects, but also as a new example of the kind of magic tricks allowed by Smalltalk environments. Albeit not as fanciful, Objective-C offers good reflective features, which are nicely exploited in OCMock, a library which, besides taking advantage of Objective-C’s dynamic nature, makes use of the trampoline pattern (close to the heart of every compiler implementer) “so that you can define expectations and stubs using the same syntax that you use to call methods”. Again, a good chance to learn new, powerful dynamic programming techniques.

As you can see, writing tests can be, a bit unexpectedly, actually fun.

Tags: , , , , , ,

NASA is looking for Squeakers

Want to work at NASA and program in Squeak? Then you may be interested in this
job offer:

A team at NASA Ames Research Center is looking for a system architect to help us design, develop, and deploy a component-based framework for building NASA mission control systems. This project is at a point where they need someone with serious chops to help translate the preliminary design and prototypes into a state-of-the-art yet practical and deployable system. The architect would be joining an up- and-running, 10+person team of engineers and designers. The design includes elements of user-level composition, model-driven user interfaces, dynamic assembly of components according to ontology- specified roles, distributed components, etc. etc. We are currently building a pilot in Java / Eclipse RCP, and a parallel testbed for user-experience exploration in Squeak (Smalltalk) (really). The resulting framework will be used by the various NASA centers (e.g. Ames, JPL, Johnson Space Center, Kennedy Space Center, etc) to build distributed, multi-mission systems for planning and executing a variety of NASA missions, including robotic (e.g. Mars rovers & deep- space probes) and manned (e.g. the new Moon/Mars exploration effort, including the Crew Exploration Vehicle currently being designed).Position is located in Mountain View, California, at NASA/Ames, a NASA research center. We can work with individuals on residency issues. Interested parties should send a resume to tshab at mail.arc.nasa.gov

Thanks!

Any taker?
Update: Cees de Groot has some additional info in his blog.

Tags: ,

Recovering Lost Code Screencast (and Squeak porn)

As promised, James has just posted his Recovering Lost Code Screencast, which clearly demonstrates how powerful Smalltalk development environments are, and will give you a chance of seeing how VisualWorks looks like.

SqueakI must confess Smalltalk is deeply impressing me: i want one of those environments for Scheme! In the meantime, i content myself playing with Squeak, which has nothing to envy to VW, by the way. I used to complain about its looking childish. Besides silly, the complaint is baseless: see how my Squeak environment looks after installing the Skylark Theme

Tags: ,

Orthogonal Smalltalk

Yesterday, I was longing for a perfect, orthogonally persistent world. As it turns out, this world is here right now. James Robertson, over at Smalltalk Tidbits, Industry Rants, has posted a comment on my entry:

Well, in Smalltalk – certainly in VisualWorks and Squeak – even a power outage isn’t going to end up losing you any work (unless your HD dies simultaneously, of course). As it happens, each change you make to your image is saved off in a transaction log called the change file. When you restart the image, you can load the change file into a tool, and replay all (or a set of specific) actions so as to restore your state.

He goes on to promise suggest the possibility of a screencast on it by next week, so stay tuned! It the meantime, you can take a look at his screencast collection to see, if only second-hand, how developing in a truly dynamic environment feels.

Also of note is his next entry, Tools and Power, where James comments on how easy, well no, trivial incremental development is in Smalltalk, and points readers to a video to see it in action. The movie shows a debugging session in Seaside, a framework for developing web applications in Smalltalk. The developer is browsing a web page served by the system, and modifies the code generating the HTML with a new, unimplemented method. A debug trace ensues. Alt-Tab and he’s again into Squeak, with a debugger window ready. From there, it’s just a matter of two clicks to add the missing code. One more to proceed. Alt-Tab and the new page is there, in his browser. You really must see it.

SqueakI must really give Smalltalk a serious try. If you feel like me, Squeak is the obvious, open source alternative, but it’s not the only one: Cincom’s VisualWorks is free-as-in-beer for personal use, and the list goes on

Tags: ,

Persistent Joy

In the comments section of The Joy of REPL, a reader is posing an interesting question: how do i make my joy persistent? Or, in her words,

Dumb question – you are happily programming in the environment, and the lights go out. Have you lost your state?
How do you save “source” code? I’m interested from the angle of irb, as I like ruby. I still think in the mode of writing the source in an editor, checking it in, etc.
I can’t seem to imagine this environment in terms of day to day work, esp with a development group.

Managing persistence depends largely on your development environment. But of course, the primary method is the traditional one: you write files. You don’t need to literally type your code at the interpreter’s prompt. Any decent editor will let you send to the interpreter expressions written (and, eventually, saved) in any editing buffer. Emacs excels in this aspect, specially if you’re on Lisp and use Slime (or its cousin slime48, which works on scheme48). You can see it in action in Marco Baringer’s excellent tutorial video (bittorrent available here). The important thing to keep in mind is that you need the ability to evaluate individual expressions (as opposed to loading files as a whole), and this is made possible by the joint work of your language’s runtime support and your editor. I’m not a Ruby user, but i bet Emacs or vim, among others, give you similar facilities. That said, i would be surprised if they are as impressive as Slime’s. Because Slime is cheating: it interacts with a programming system (namely, Common Lisps’) that does its very best to allow an incremental, organic development style. How so?

Well, as soon as you go beyond writing little toy snippets and into serious (as in bigger) programs, you’ll need some kind of module system, in the sense of a way of partitioning your namespace to avoid name collisions. Every language out there provides such a mechanism in one way or the other (and Scheme famously provides as many ways as there are implementations; more on this below). Therefore, to keep enjoying our interactive way of life, we need that the interpreter and the editor cooperate to evaluate our code in the correct namespace. Common Lisp’s module system is based on packages. Each symbol known to the system belongs to one of them, and it is customary to begin your files with a form that informs whoever is interested to what package the following code belongs into… and the editor/interpreter team are definitely interested: expressions sent from a buffer to the REPL are evaluated in the correct context. Again, i don’t know whether Ruby or Python offer this synergistic collaboration, but i know that you definitely need it to attain the Joy of REPL.

Common Lisp is not unique in this regard. In the Scheme world, scheme48′s module system was also designed with interactive, incremental development in mind, and taking advantage of it in Emacs required an, in a sense, almost straightforward (but, by all means, worthy) effort (thanks Taylor and Jorgen). As an aside, this is what makes s48 my preferred scheme and keeps me away from otherwise remarkable systems like PLT. (And this is why the current R6RS standard module system proposal is a show-stopper: if you happen to have a friend in the committee, please write him and include a link to Taylor Campbell’s alternative proposal and its accompanying rationale.)

Thus, when lights come back, you recover your previous environment by reloading your files. Good module systems provide means to streamline this operation, typically (but not always) by storing the package definitions in separate files. But this is still a nuisance, isn’t it? I must wait to all my files being reloaded and maybe byte-compiled… Don’t despair, there are better ways. Most CL implementations and several Schemes (MIT/GNU Scheme and, again, scheme48 come to mind) allow you to save your complete state, at any time, in what is called and image file. This image contains a binary snapshot of the interpreter’s state, and you can reload it at any later time. Being a binary representation, it will come to life blazingly fast. Besides Lisp, Smalltalk is the paradigmatic (and possibly pioneer, but i’m not 100% sure on this) image-based language: for instance, in Squeak, the only way to launch the environment is loading a previously saved image, which contains detailed information of your previous state (including the graphical environment). In this sense (and many others), Smalltalk is a dynamic programmer’s dream come true.

Image files make things even better, but not perfect: you still need to save your state every now and then. In an ideal world, persistence should be automatic, managed behind the scenes by the system, even by the operating system. Just like the garbage collector we have come to know and love in our dynamic environments manages memory for us. This nirvana is called Orthogonal Persistence, but unfortunately, we’re not there yet. I first heard of OP from the guys of the Tunes project, where François-René Bân Rideau and friends have envisioned what i view as the ideal computing environment. Unfortunately, up to this day it remains in the Platonic realm of the ideals (but this doesn’t prevent their having one of the best online knowledge bases on computer science). Another interesting project in this regard, with actually running code that may interest the pythonistas among you, is Unununium, an operating system built around the idea of orthogonal persistence. Finally, in this context it is also worth mentioning again Alan Kay’s brainchild Squeak, which provides an environment that, without being an entire OS, in many ways isolates you into a wonderland of its own.

Tags: , , , , , ,

Dynamic OS X Programming

I am very fond of Mac OS X’s graphical interface. As a GNU/Linux or FreeBSD user, i despise GUIs and usually work in a mostly text-based ambient powered by Emacs running inside a urxvt terminal. Things change dramatically when i enter OS X. There i discovered that it is not that i dislike GUIs, but that i dislike ugly GUIs (although the jury is still out on where i’m more productive).

Thus, OS X is the only environment where i feel like programming a GUI. From a programmer’s viewpoint things also start out better: one can go native and avoid C and C++. Cocoa’s native language is Objective-C, which happens to be a dynamic language. That is, one has a runtime where possibly dynamic objects live, good (but not great) reflective capabilities and a meta layer that let’s you, for instance, access class and metaclass objects. And all that at runtime. That allows (besides the fun of programming) funny tricks, like the ability to interact with running applications from the outside, in what constitutes an incredibly flexible plug-in system.

Probably the description above rang a bell: Smalltalk. Objective-C is a scaled down Smalltalk in many ways. The good news is that you can have the real thing: F-script is a Smalltalk dialect specifically designed to script Cocoa applications. It let’s you inspect any running Cocoa application, and manipulate the objects it contains in real time, so to speak. F-Script Object browser As any decent Smalltalk, it comes with a powerful object browser, which you can admire in all its beauty below. Moreover, F-script can be embedded in your Cocoa application, acting as an excellent extension language, way more cool than Apple’s default scripting language. A beautiful application taking full advantage of these abilities is ObjectiveCLIPS, a powerful rule-based development environment for Cocoa.

But still, to my knowledge, you cannot program a whole application in F-Script and, besides, Smalltalk is all very well, but it’s not Lisp. Support for Lisp in the Cocoa world is not as good, but shows promise. First, there’s OpenMCL, an excellent Common Lisp implementation for the Mac and Unix systems that comes with a Cocoa bridge. It includes a demo Cocoa IDE to get you started, and people have gone as far as creating games using it.

Until recently, poor Schemers were left in the cold. Enter Chicken and its Objective-C egg. Support is still rudimentary, but there’s already a nice tutorial on writing a currency converter, the default Cocoa Hello World app.

Probably, it’s still too hard to write a well-integrated Cocoa application using Lisp, but i think it’s just a matter of time. Happy hacking!

Tags: , , , ,

Smalltalk Metaverse

I’ve just been watching a very interesting presentation (by Julian Lombardi and Preston Austin) on the Croquet Project. From their website,

Croquet is a combination of open source computer software and network architecture that supports deep collaboration and resource sharing among large numbers of users. Such collaboration is carried out within the context of a large-scale distributed information system. The software and architecture define a framework for delivering a scalable, persistent, and extensible interface to network delivered resources.

Have you read Neal Stephenson‘s Snowcrash? If you have, well, Croquet aims to be the Metaverse implemented in Smalltalk (if you haven’t, please do). More concretely, Croquet runs inside Squeak, a, mmm, peculiar Smalltalk implementation. I’ve tried a couple of times to get it, but to no avail. Maybe it’s because i find the environment somewhat childish. But then, i remember i had a hard time at first getting The Little/Seasoned Schemer books for exactly the same reason, and nowadays i’m recommending them to everyone and her brother.

Come to think of it, Squeak (and Smalltalk) is a programmer’s dream come true: it puts under your fingertips a live, integrated environment where you can access and modify in real time the source code running the system. Isn’t it that the reason why we admire Lisp Machines? Or the one that makes living into emacs so pleasant?

On top of that, Croquet builds what could be the future of our interaction with computers. Add to the integrated and life Squeak environment 3D worlds shared via P2P by any number of users, including all kinds of multimedia bells and whistles, without centralised servers. The presentation includes a demo of, ahem, two interacting avatars. It completely escapes me how on Earth the system will scale, but the claim is that the underlying network infrastructure is ready to live up the challenge… using protocols and technologies described in David P. Reeds dissertation, dated (and this is not a typo) October 1978. If that’s true, i wonder what have we been doing all these years.

Precisely that stagnation is one of the main drives of the Croquet people: we’re using the same desktop/document metaphor that the Xerox guys invented in the 70s and Apple (which, by the way, seems to be interested in Croquet) made mainstream during the 80s. They think we need to go beyond that. The problem is, of course, finding the right direction. But know what? The mind behind Squeak and Croquet is Alan Kay (who, as you probably know, besides many other things, invented the overlapping-windows interface and object oriented programming), so maybe you should stop reading my ramblings and go learn some Smalltalk. Afterwards, you can take advantage of this recently published step by step Squeak tutorial, followed by Introduction to Basic Croquet Programming and Basic 3D Programming in Croquet.

I may give it a try a third time one of these days, during my copious free time.

Tags: , ,

Berkeley SICP videos

I’m sure i don’t need to point you to the original SICP video lectures (or do i?). As any good university, Berkeley’s a course on computer programming based on Abelson and Sussman’s book. The nice bit is that they have recorded the lectures and made them into webcast lectures.

I have yet to watch most of them, but i won’t be surprised if they’re good. For starters, look for the Alan Kay videos in there: they’re from the mid 80s (when Alan was at Apple), and he reviews the history of user interfaces. He shows really amazing videos of the late 60s and early 70s, with rarities like, for instance, a lecture by Douglas Engelbart featuring his futuristic (and working!) NLS system (the full demo is also available elsewhere), or the first SmallTalk system.

I am deeply surprised by the amount of sophistication these people had reached thirty years ago: several of the things they were able to do look pretty advanced even by today standards. One cannot help but wondering what have we been doing during the later decades!

Tags: , , , ,

Follow

Get every new post delivered to your Inbox.

Join 29 other followers