Microsoft Small Basic for .NET

Microsoft Small Basic is intended to be fun to use. It is that, and more besides. It has a great potential as a way of flinging together quick and cheerful applications, just like those happy days of childhood. Tetris anyone?

It is a brave Microsoft employee that can stand up and say ‘I have a design for a new computer language. I think I shall call it BASIC’.  This seems to be just about what a software developer in the Oslo team, Vijaye Raji, did. Having had the idea, and some encouragement, he created the first prototype of Small Basic as a ‘side project’ in his spare time.  Thankfully for us, he was supported and assisted by many Microsoft people who understood what he was aiming for. The result is a language that is extremely compelling; What is more, it is fun to use. Already, there is a strong community of users who exchange programs and give advice to newcomers, and Vijaye is active on its forums.

So what is Small Basic?

Small Basic  is a development environment, and compiler, for the rest of us. Unlike PowerShell, it is immediately accessible, and just begs you to try programming in it. Unlike PowerShell, you cannot yet use it for many serious applications. It has some novel ideas, not least of them being able to publish, rate, or access Small Basic programs on the web with a couple of clicks. There is also a Silverlight player for Small Basic programs that enables you to run Small Basic programs on your browser without having to install the IDE, so you can even embed programs or games on a blog or website! As a result of the simple way that you can publish programs, there is a growing collection of sample code, and by looking at it and maybe tinkering you can pick the language up in a matter of hours. When you feel that you’ve outgrown the language, you can just hit the ‘graduate’ button, and your program is converted to Visual Basic.

Why Another BASIC dialect?

Small Basic started out with the observation that youngsters no longer programmed for fun. Unlike the generation who bought Apples and TRS 80s, BBC Micros and a host of other cheap computers with little on them but a basic interpreter, today’s kids now prefer to play with games. Who wouldn’t? The downside to this is that there is now a generation in the West that is leaving school without the programming skills that the previous generation gained tinkering with primitive microcomputers.

Vijaye, and some others in the Oslo team such as Chris Anderson, felt that there was nothing in the .NET framework that was geared towards children and beginners. There certainly wasn’t much that normal humanity would think of as fun in the framework. The sheer complexity of Visual Basic and Visual Studio was too intimidating for beginners, and also for people who just wanted to get something done, but weren’t professional programmers. The introduction of F# and PowerShell confirmed the deeply cerebral thinking of Microsoft. There is nothing as exciting for .NET as Java’s OpenProcessing; nothing that one could just use for making strange patterns or wild animations.  Vijaye’s idea was for a simple compiled language that had the same simplicity and appeal of the old Quick basic of MSDOS, but with objects, a simplified grammar, and a sandboxed functionality. Vijaye wanted to escape from the trend for continual abstraction in languages that beginners find confusing, leaving just an operational abstraction that allows you to call methods on, for example a ‘Flickr’ object. Vijaye put together a prototype in his own spare time that took its inspiration from the old QBasic, where the BASIC manual was little more than one-page. This time, however, there had to be enough graphics support to allow games to be written, and even Turtle Graphics. The result is similar in its theme and intention to the brilliant Logo interpreter put together by Gary Kildall for CP/M around 20 years ago: a language that is ideal for the beginner and fun to use. One can certainly achieve something cool with 25 lines of code.

What makes Small Basic so attractive is that the application is ultimately Vijaye’s vision and idea. He makes the ultimate decision on what is included and what is kept out. The result is the antithesis of the design-by-committee of much of Microsoft’s output. We can be thankful that Vijaye’s vision is sound, and fits in with what Niklaus Wirth, Charles ‘Chuck’ Moore and other great language designers have been saying. ‘Keep it simple, get things up and running quickly’. The dialect is called Basic, but it is Basic without the oddities.

Small Basic is typeless. It is imperative and doesn’t use or expose beginners to concepts like scopes, types or object orientation.  It consists of just fifteen keywords, including GoTo. Vijaye wrote a simple IDE for it with intellisense and help. It can consume .NET objects.  As it is provided, it has just a few simple built-in objects that are chosen to provide various interesting effects and to show the possibilities, but any number of static objects can be created in C# to add to it. All variables are global, and scoping is avoided entirely.  There are subroutines, but no other types of routine. Small Basic cannot pass arguments to subroutines and cannot return values from subroutines (no functions). Subroutines can be triggered by events: You can create a subroutine and assign it to an event. This will wire it up to the event. Keywords are case-insensitive. It is a protected sandbox of the .Net libraries. You can, of course, write extensions for particular purposes. The standard libraries provide static “Objects” that group operations, properties and events. New libraries can be created using other .Net Languages and plugged in to extend the possibilities of what you can do from within Small Basic. Some third-party plug-ins already exist.

As it stands, it is powerful enough to allow you to write a space-invaders, Tetris, Sudoku  or Pong game in around 25 lines. A little application to display photographs from flickr takes three lines; an application to change your desktop screensaver can be done in two. You could argue that there are better ways of doing it but the fact remains that this is a quick and pleasurable way. Sure, you learn bad habits, such as the GoTo keyword, but when you see better ways of doing things, the experience will allow you to understand why they’re better.

The Proof of the Pudding

So, how long should it take to write a simple working pocket-calculator simulator in fewer than 200 lines for someone entirely unfamiliar with Small Basic? The answer is about twelve hours if you’re a bit slow on the uptake like me. The first stage is to download the software. It is free, by the way. You’ll find the ‘Getting Started’ guide useful too

The IDE looks somewhat like this…

958-SmallBasic1.jpg

958-SimpleBasicIntellisense.jpg

You’ll see that it is pretty simple to use. Key in your code, and hit F5. Right-click the code for a context menu that allows you to format code.

Line numbers are added so that you can see easily where the compiler hit a problem (you can of course, click on the message to go straight there). 

There is intellisense too that pops up when you start typing. Because variables are all global, it will serve these up too along with the keywords and objects.

It would be nice to say that one can use it without looking at the API documentation which is available from Microsoft, but I’m old enough to find this handy to refer to.

958-SimpleBasicHelp.jpg

958-AC3.JPG

So, with a bit of click-clacking on the keyboard, and several false starts, I had something…

…and I don’t really have to show you the code because I’ve put it on the internet for you to download. You can, of course, run the program and look at the source code at http://smallbasic.com/program/?SNG664 but you can also import it into your IDE

958-AC4.JPG

You can see the result of pressing the ‘import’ icon. Just type in SNG664, and the program can be downloaded ready for you to run and maybe change and improve. I’ve also put it here, in case you just want to see the code.

It is really simple to get results. In seven minutes, I typed this program that gets a definition of a word from an online source.

And here is the result:

958-SmallBasicCommandLine.bmp

You’ll see that you’ve actually created an EXE file, an assembly. It is just 3K in size. As long as you have the SmallBasicLibrary.DLL in the GAC, or a copy in the directory you store your application, you can run the exe file from anywhere without needing the IDE. You have created a very simple way of automating routine processes.

There is a pretty reasonable file-handling  object with enough clout to do useful tasks such as arranging backups by date and retiring aged backups. You can download, and save, files from the internet. Because you can get web pages by URL, it is possible to get or send (in the query string) information to or from the internet.

Adding functionality to Small Basic

If the functionality you need isn’t there, you can plug in an external library to extend Small Basic, using any .Net based language that can compile to a .Net assembly.  For a type to be identified as a Small Basic Object, it should be declared static and have a SmallBasicTypeAttribute. Its properties should be Microsoft.SmallBasic.Library.Primitive and the input and output parameters for Methods should be of type Microsoft.SmallBasic.Library.Primitive. All events should be be of type Microsoft.SmallBasic.Library.SmallBasicCallback

Your assembly dll should be in Small Basic’s ‘lib’ directory in a folder named “lib” in the Small Basic’s install location.  You can optionally provide XMLDocs that will be picked up by Small Basic’s intellisense. Vijaye provides a sample extension to get you started. Using .NET Reflector, you can also see how vijaye implements the built-in objects. If you have Reflector Pro, you could even debug his code to see how it works!

958-SmallBasicReflector.jpg

OK. So what is stopping you integrating quite complex libraries into Small Basic, in order to use it to automate processes? Hmm. The first thing I’d want to integrate would be a good Regex search, using the excellent .NET libraries. Then I could compile up all my common search/replace tasks.

So What’s wrong with Small Basic?

Well, considering that a single person wrote it, it is superb. However, it still has a few rough edges. It isn’t entirely stable yet, it doesn’t auto-save, and the IDE seems to be missing some basics such as a way to close edit windows from the menu or move between them. There is no ‘Replace’ function and Intellisense stops short of prompting you for parameters to methods. Small Basic itself seems to have absolutely no error handling. As the dialect is incapable of rendering numeric text in a bounded box properly since there being no left, right or center alignment, there are clearly some essentials missing.

I have a few niggles, that aren’t really huge problems. I’d like a complete solution for serializing arrays for import, export or initialization. It is there in a primitive form, but only for simple arrays with just one dimension (attribute=value;attribute=value; attribute=value; etc…)  whereas something closer to XML or JSON would be nice.

Conclusion

If I say that this brings the fun back to programming, is this a valid reason to recommend it? Yes, because it was designed and intended to be fun. I think Small Basic needs a small amount of tidying up before it is fully released as V1. However, it has great potential as a first language for anyone, whatever age, particularly where they want to exchange applications easily. As it is so extensible, it could also quickly be brought to bear on tasks where a certain amount of automation has to be provided, but where PowerShell is just too much like Klingon. It has a completely different use as a way of embedding simple graphical applications into internet sites. I can see it being used, with extensions, as a tool for automating and monitoring processes. You can always graduate to Visual Basic and the overweight Visual Studio if your brain is just too big for Vijaye’s cute IDE.

Why would one enjoy programming in Small Basic? I think that part of the reason for this is the work that has gone into making it easy to exchange programs, and share things with an active community, and the challenge of working with a restricted set of features. It is also a great way of adding features to a website that are difficult to achieve any other way.

Resources

There are some good resources out there…

  • Shapes’, ‘Eval’, ‘DotNet Interop’, … : An extension for Small Basic
    This is a very useful extension to Small Basic by Francois Remy, with a range of useful for .NET integration. For example, it allows you to use all .Net Types, Subs and Functions in Small Basic, as well as events. It add events on controls such as buttons or textboxes, and on shapes such as ellipses and polygons. It allows you to consume XML documents, and provides a number of other useful features.
  • Small Basic API Reference
     This is essential in the early stages of learning the language. The intellisense is good but nothing beats seeing the entire list of what is available.
  • Small Basic FAQ
    This gives a brief answer to some of the questions that get asked ,such as ‘Isn’t there another application called SmallBasic?’
  • Small Basic Forum
    This is probably the best place to pick up tips. It is very well supported by Vijaye, George Birbilis, Grzegorz Tworek and Francois Remy and a host of others. There is even a MySQL Database Access system in there!
  • Small Basic Blog
    The place to start, as Vijaye does all the product announcements here, and filters out the best of the forum entries and presents them nicely. Gems include the ‘ Small Basic Programs in 25 lines competition.
  • Small Basic Wiki
    More useful resources in the Wiki that aren’t available anywhere else.
  • LitDev’s Speech and Music Extension for Small Basic
    A Smal Basic extension to play musical notes and speak text