What if we want to use our command to draw centred squares in various programs? We can simply copy it from our tunnel program and paste into the new program. It’s a bit cumbersome, but still much better than reinventing the algorithm every time we need it. However, there is a drawback: if we find an error in our algorithm (and that kind of thing happens all the time), we have to change the algorithm and copy the changes to every program that uses the centred_square command.

We can avoid all this contant copying and pasting, and at the same time make our programs much shorter, by storing the centred_square command into a separate module (i.e. a file containing just commands) and then loading the module into every program that requires centred squares. If an error in the algorithm is found, only the module is changed and all programs will work, because they always load the newest version of the module when they start executing.

Having one different module for each command we define is the most flexible option, so that each program only loads those commands it needs, but is a bit cumbersome. A better option is to store related commands in the same module, in order to build libraries of reusable algorithms. A program may use just a subset of the commands defined in a module. For our running example, we copy the centred_square command into a file called shapes.dbn:

// shapes library
// version 1, 30 Oct 2008, Michel Wermelinger
// centred_square command

command centred_square left
// draw a centred square, given the left coordinate
{
   set bottom left
   set centre 50
   set distance (centre - left)
   set right (centre + distance)
   set top right

   line left bottom right bottom
   line right bottom right top
   line right top left top
   line left top left bottom
}

In file tunnel.dbn we replace the centred_square command definition by  DBN’s load command.

// the tunnel
// version 4, 30 Oct 2008, Michel Wermelinger
// put command in external library

load shapes.dbn

line 0 0 100 100
line 0 100 100 0
centred_square 20
centred_square 40

To recap, this and the previous two lessons introduced three different fundamental programming concepts, all relating to abstraction:

  • variables allow us to abstract concrete values into general concepts
  • sub-programs (called commands in DBN) allow us to abstract sequences of computational steps into named algorithms
  • modules allow us to abstract related commands into reusable libraries

In the next lesson we get back to drawing: we introduce the use of grey scales to reinforce the notion of distance.

3 Comments to “DBN tutorial: Modules”

  1. Hi, I’ve been following your tutorials in conjunction with Maeda’s book… they’ve been extremely helpful so far… two questions though:
    1. I have not been able to complete your Modules Tutorial, I keep getting the “Not a valid statement” error when I try to load shapes.dbn. Is there somewhere I need to specify what folder for DBN to use as a library?
    2. How did you export the animations from DBN? I keep saving them as gifs but I only get the one frame.

    Thanks a bunch in advance

    Leon De la Rosa

  2. mw says:

    Leon,

    Thanks for your interest in the tutorial. I really must continue it some time soon.

    Regarding your first question, it should be enough to have the shapes.dbn file in the same folder as the tunnel.dbn file. As for the 2nd question, I don’t export DBN animations. The web page includes the DBN interpreter as an applet and so you’re actually running the DBN program in your computer. See the FAQ on the DBN site on how to integrate DBN into your own web pages.

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Spam protection by WP Captcha-Free