Make yourself useful, do-while

Here’s a tedious loop pattern I find myself typing all the time:

    control-code
    while (condition)
      body
      control-code
    endWhile

Read the rest of this entry »

Things aren’t looking good for repeat..until

I’m probably going to cut “repeat..until” from gen2 Slag.

  • I used it literally 4 times in the 10,000 lines of the Slag standard library.
  • It can easily be replaced with a while() loop.
  • I always forget it’s there until I try and use “repeat” as a variable name.
  • Likewise, I’d kind of like to use “repeat” as a variable name again.

I guess it’s just one of those things that sounded cool but never proved itself.

Murphy mentioned the other day that every feature proposal in C# starts out with negative 100 points.  People then try to come up useful cases for the feature, each rated with some number of points to offset the initial score.  Only features that end up with a net positive score are actually implemented.

It’s a neat idea, although thankfully since Slag is just a little fish (but growing!) I can afford to just put things in & rip ‘em out later.  Anyways, “repeat..until” is currently at (-99), so I’m gonna ditch it.

Oh wait – let’s look at the MadStone source code!  Here we go.  Hmm…. zero uses.  Yep.

Fast call dispatch for aspects and interfaces

Up until now, the call-related speed issue I’ve been most worried about in the Slag VM has been aspect call dispatch (e.g. interface call dispatch).  Ironically, while thinking about multimethods (previous post) I just figured out a way to turn aspect calls into standard dynamic calls!

Read the rest of this entry »

Pondering double dispatch

This semester I gave my nooblet CS 110 students a tile-based game framework and had them add some additional actor classes and behaviors.  It worked out pretty well, but the big thing I noticed was that the majority of my students were trying to override methods in a way that was intuitively but not technically correct for Java.

For example, the bump( TileActor actor, int dir ) method is called whenever you bump into someone while moving in a certain direction.  To have Snake actors kill any other “Being” actors you’d give them this bump method:

    public void bump( TileActor actor, int dir )
    {
      if (actor instanceOf Being) actor.die();
    }

However, most of my students were defining this method instead:

    public void bump( Being actor, int dir )
    {
      actor.die();
    }

Of course in Java you have to override a method using the exact same parameter types as the original definition.  But their code would have worked in a language that uses double dispatch.

Read the rest of this entry »

Ergonomic programming and the multi-dimensional ‘which’

In classic fantasy stories, knowing the true name of a demon or a faerie gives you power over it.  Taken as a metaphor, I think this leads to a profound truth: you do not truly understand a thing until you can accurately describe it.

I’ve seen this in practice many times.  Some examples:

  • Once I started teaching I became a significantly better programmer because I had to explain things to my students.  I could no longer just avoid using or discussing the commands I was uncertain of – students have a knack for asking you about the one thing you wish they wouldn’t!
  • One of the best techniques for debugging something is to explain exactly what you’re doing to someone else.  Usually, half-way through the explanation the light-bulb will click on.  That process of trying to distill your abstract problems into coherent sentences is usually the nudge you need to spot the problem.
  • Likewise, John Placer, a CS professor who was at NAU for many years, once said (and I paraphrase): “Write a comment for a method before you write its body.  If you can’t explain what that method does, you need to redesign it before you continue coding.”  So true! (Not that I actually write comments first, but I agree that you should be able to!)

Until recently, Slag’s motto was “The Language of Games”.  I’ll admit to taking a cue from Matlab’s “The Language of Technical Computing.”  But as time wore on I grew increasingly dissatisfied: what makes Slag the language of games?  How do I really sum up the language in a short phrase to someone who’s never heard of it?  In short, I had picked an arbitrary description that did not really fit.  I did not really understand the nature of my own language.

Read the rest of this entry »