Archive for the 'Programming Languages' Category

Scratch

Saturday, September 29th, 2007

I don’t expect my kids to become programmers like their dad. I wouldn’t wish it for them unless it is what they want for themselves. Still, I think there is value in getting exposure to programming for a number of reasons - thinking logically, planning, designing, applying math, understanding how things work and fostering creativity to name a few.

The question is when and how to introduce kids to programming. Scratch may be the answer to how and right about now the answer to when. Scratch is a new language from the Lifelong Kindergarten group at the MIT Media Lab designed to make it fun and easy for kids 8 and up to create programs. My kids and I found Scratch instantly compelling. Everything about Scratch, from the friendly looking cartoon cat that starts out in each new project, to the building block style programming, to the sharing of projects on the Scratch web site has been designed for and appeals to the youngest possible audience.

My kids are at the young end of the target age range for Scratch so I don’t expect too much programming from them at this point. The important thing is that they are really excited to use the program. They create projects to collect, edit and arrange pictures. They are interested in seeing and trying the projects that other people have created. It provides a context for me to talk to them about things like the X, Y coordinate system in terms of the number line they already know and what it is used for in moving things around on the screen.

Just the other night I was sitting with my youngest and she had chosen a cartoon girl sprite and a bedroom background. Then she asked “can we make her jump up and down on the bed”. I said “sure” and in a few minutes I showed her how. A block to move up. Click, a block to move down. Put them in a repeat loop, click. How many times do you want her to Jump? Twenty, OK. Oops, too fast. Click, add a wait block. Done. Then I introduced her to variables so she could control how many times the girl jumped.

The Scratch web site provides a social network where people (I say people because it is not just kids playing with Scratch) can share their projects and ideas. Any uploaded project can be downloaded by anyone so they can open it up and learn how it works or simply just play with it. This really fosters learning and building on the ideas of others. You will see some comments complain of copying or ripping off ideas but overall the site promotes an attitude of sharing. Knowledge and ideas are not a scarce resource to be horded and protected they are abundant and should be shared. Many people share projects that demonstrate a particular technique and encourage others to use it in their own projects.

I am very impressed with the projects on the Scratch web site. There are over 32,000 projects as of this writing, which is impressive since the program only came out in May of 2007. Some projects are artistic perhaps using color cycling or other graphic effects to make their pictures come to life. Some are animated stories. Some are musical. Some interactive. There are plenty of video game style projects ranging from imitations of classics like Pac-Man or Donkey Kong to original ideas. There are projects exploring science and math and many show a great deal of sophistication.

The usability of the development environment is superb. It is well organized and intuitive. It is also visually attractive. What I find most impressive, for such a new product, is that it is rock solid. I used it a number of hours over the course of a week often leaving it running for days without a single crash, glitch or loss of data.

Programming is done by putting together statement blocks such as Scratch Move Block and control blocks such as Scratch Repeat Block. As far as I can tell it is not possible to get an error message no matter how you put the blocks together. It may not do what you want but it will not complain that you did something wrong. Feedback is immediate. Just double click a block to see what it will do. Another great feature is that you can change your program while it is running.

Scratch is implemented in squeak. When the projects are uploaded to the web site they run as a Java applet. I have noticed some differences between the behavior of my project when run in Scratch and as an applet but the differences are minor. Probably just display glitches in the Java graphics routines.

How about the Scratch language itself? Well from an experienced programmer’s point of view the language is extremely limited. For example, variables can only hold numbers, there are no string manipulation operations, and there are no arrays or collection data types. Not surprisingly the programs are event driven. Groups of statement or control blocks are run when the start flag is pressed, a key is pressed, when a sprite is clicked or when a message is received. Messages are broadcast and all handlers act on the message. You can define as many messages as you like. Messages cannot have arguments which is one of the most limiting things. The message handlers are the closest thing to a subroutine but because there is no argument passing all information flows through global variables. I would say that it is a strongly typed language in the sense that the round edges of a numeric value will not fit where the sharp edges of a Boolean expression are expected. Variables must be declared.

Simple things like prompting for a name and then outputting "Hello " + name is very difficult due to lack of input/output and string manipulation primitives. However things that would be complex in other environments like hit tests between two moving sprites are trivial. I had to remind myself that I am not their target audience and everything left out was probably left out for a good reason - usability.

I am not a big fan of graphical programming languages. I quickly got tired of the Lego Mindstorms block programming language and switched to NQC. I found the Mindstorms programming environment to be slow, noisy, and inefficient. All of the graphical programming languages I have ever tried I found to be far inferior to text. Scratch is no exception but it is the best interface I have see yet. I also think it is just the right thing for young kids. Having visual blocks to move around really maps well to the physical world they are used to manipulating.

I do hope the language gets extended. I would like to see advanced users be able to explore programming concepts such as recursion.

Like I said, its not just my kids that find Scratch compelling. I wanted to play with it as well. I rationalized that by learning it I would be better able to help explain it to my kids but I like creating things no matter what the medium.

My first project is Addition Tutor. It is an interactive program to learn and practice addition of up to 3 digit numbers. I decided on this project because of the challenge of inputing and displaying numbers in Scratch and because it is something that I’m teaching my kids now.

Scratch Project

Scratch is not the first programming language with kids in mind. Way back when there was Logo and more recently KPL. I took a quick look at KPL and it looks like visual basic to me. Unlike Scratch there was nothing enticing me to try it out. The first thing you may ask about a language is what can I do with it. What better way to answer that question than with a web site with thousands of examples all of which you can download and try.

I Like Python

Sunday, April 22nd, 2007

I recently started learning Python and to my surprise find it to be a very nice language.

I had heard good things about it years ago but never gave it a look. I noticed that Joel Spolsky gave Python half credit as a safe choice of language to develop enterprise applications in. Still, I was (and still am) happy working in Java.

What prompted me to look at Python is my new Job. I now work at BEA. The BEA WebLogic Server has a tool for configuring and managing domains called WebLogic Scripting Tool (WLST). WLST is based on Jython (Jython is Python implemented in Java).

I started reading a Python tutorial. The first thing I noticed is that code blocks are identified by indenting. This seemed very strange and uncomfortable after years of using curly brackets. Now it makes sense to me. I was going to indent my code anyway so why shouldn’t the language make use of that fact. A side benefit is no more arguing over if the curly brackets belong on the same line or a new line. If arguing about code style floats your boat be comforted that you can still argue over how many spaces to indent.

So far I have created two small utility programs in Python. One gathers some information about files and outputs an XML file. The other processes an XML file using SAX and outputs a new XML file. (I didn’t use XSLT because I needed to write files, run a program and include it’s output as new element content.) Even with the learning curve I was able to write these programs faster then I could have in Java. Also the code is much smaller than their Java counter parts would be.

Some things I like about Python:

  • Simple but powerful syntax is quick and easy to learn. It seems to me to be minimally complete. In other words it doesn’t have lots of unnecessary junk.
  • Very good documentation including tutorials.
  • The interactive interpreter makes it easy to try things out. This facilitates learning.
  • Like JavaScript, Python has a syntax for creating rich literal data structures. It is so much easier to do this:
    fruit = { "apples": [ "Cortland", "Braeburn"], 'pears': [ "Bartlett", "Bosc" ] }

    Than it is to do this:

    HashMap fruit = new HashMap();
    ArrayList apples = new ArrayList();
    apples.add("Cortland");
    apples.add("Braeburn");
    ArrayList pears = new ArrayList();
    pears.add("Bartlett");
    pears.add("Bosc");
    fruit.put("apples", apples);
    fruit.put("pears", pears);

  • List comprehension - a concise syntax to make new lists from existing lists without the usual for loop. Example: [ x**2 for x in range(5) ] produces this list: [0, 1, 4, 9, 16]
  • An extensive standard library of functions and classes.
  • The map() and reduce() functions.

Python will replace Perl as my scripting language of choice.

Subtext

Wednesday, January 31st, 2007

I just finished reading “Subtext: Uncovering the Simplicity of Programming”. As I read, my mind kept going back and forth between this makes a lot of sense and this is crazy. This is common with radical new ideas. Continue reading…

Why I like working with Java more than .NET

Friday, December 1st, 2006

For many years I enjoyed working on the Windows platform writing applications in C++, Win32, MFC and ATL/WTL. I was not an early Java adopter. I had taken a Java course but continued to work in C++. More and more of my friends had made the switch to Java and told me how much they liked it.

In 2002 I started programming in Java. In 2005 I took a course on C# and .NET as well as doing my own learning. I only got a little hands on experience with it because I was doing to much management.

There are many things about C# that I like more than Java:

  • The verbatim strings: @”string that can span lines”
  • Unsigned integers
  • Structs for defining your own value types
  • How types are unified so that value types can be used as objects (Boxing and Unboxing). Yes, Java has this now but the C# way seems more unified.
  • The namespace concept is a little better than packages.
  • Properties
  • Delegates and events

I can take or leave the C# preprocessor. I’m not too fond of attributes or annotations. They have their uses (and abuses) I just haven’t had a strong need for them. I didn’t work with the .NET library enough to say if I like it better than Java. I don’t like ASP.NET because it doesn’t seem as flexible as the Java layering of Servlets and JSP. I could be wrong about that.

The reason I like Java better, and it out weights all of C#’s advantages, is the perceived attitude of the Java community. When I look around at what is going on in the Java community I see many people working on all kinds of interesting things. There is an attitude of “Hey that would be cool, I’ll build it”. When I look around at the .NET community I get the sense that people are waiting around for Microsoft to provide all the infrastructure.

I know this is a gross generalization. I’m sure there are people doing interesting things with .NET and more importantly people who are happy to be working with .NET. This is just how I perceive things. As a data point there are about 5,000 C# projects on SourceForge and 23,000 Java projects. This could be because Java has been around longer or perhaps SourceForge is not the place to look for .NET projects.