Blitz2D Newbies: Modular Programming & Libraries
by Ghost Dancer

Modular Programming

Modular programming is a style that adds structure and readability to your program code. It may not make much difference on small projects, but as you start to work on something bigger it can make your code much easier read and maintain, so it's worth getting into good habits now.

Structuring your code is a simple task of splitting your program into manageable chunks so that each part is self contained. By creating these self contained modules, you can focus your programming to each part. Once you are satisfied that one bit is working, you can then move to the next module with the confidence that whatever you do elsewhere will not have a knock on affect on other modules you have already written.

Blitz Basic gives us three methods for modular programming:

  • Functions
  • Subroutines
  • Include files

Functions & Subroutines
Functions & subroutines are very similar in nature, both are a blocks of code that can be called from another part of your program, when the routine has finished, it returns to the point it was called from. This is very important in programming because it means you can reuse the same piece of code again & again.

To illustrate this, let us look at a simple example:


The textBox() function is basically an extension of the Text command but in addition to drawing some text, it also draws a box around it. You may use something like this to create a simple menu. Rather than duplicate the code for each option, we just make one simple call to the function and it does the rest.

Not only is this a much easier way of programming, but you can use this function in any program you write. This is when you start creating your own libraries (see below for details).

Functions vs Subroutines
I'm not going to go into to much detail on subroutines since functions are more appropriate to modular programming. Just for the record though:

  • Subroutines are slightly faster but, unless you are calling the routine thousands of times per second, you won't really notice much difference.
  • Functions are self contained - all variables defined within the function are local to that function and all variables defined outside the function can not be accessed from within (unless declared as Global). For more information on variables, please refer to the Blitz documentation.
  • Parameters can be parsed to functions, adding much more flexibility and usefulness.
  • Values can be returned from functions - again, more useful.
  • Functions can be used in expressions.

Include Files
When you start writing large programs, having one long page of code can make life very difficult. Include files allow us to split a program into several files. So your code will look something like this:


This is a simple example of splitting your source code where you have one include containing your generic library functions (see below), one with your game functions and one which loads your images etc. Note that you must load images AFTER the Graphics command since all current images are unloaded when this command is used.

You only need to compile your main file and any include files will be inserted during the compilation.

Organising Your Files
This may sound obvious, but make sure you organise your source files. In your Blitz source code directory, I recommend you create a new folder for each of your projects. If you are using libraries then it is best to have one directory where you keep all your libraries

Libraries

A library is a collection of generic functions that can be used by any program. Each library is a separate .bb file which can be included in your source if any of it's functions are needed. As explained above, I recommend you have one directory where you keep all your generic libraries so all your programs can reference the same files.

To get you started, my own libraries are available on my site and there are also many other libraries available on here on www.blitzcoder.com and on www.blitzbasic.com.

Creating Your Own Libraries
You don't need to specifically write your own libraries. Instead, when you are programming, just think libraries! I've built up many of my libraries whilst working on different projects. If I write some code and think it could be useful elsewhere then I make a few tweaks and I add it to one of my library files. When I next need that functionality - it's already done. A small amount of effort now can save you much time on future projects.

A few tips for writing your own libraries:

  • Avoid using global variables where possible - try and make each function self contained.
  • When writing new versions, try and keep them backward compatible - if you need to add new parameters, make them optional (see below).
  • Make the functions as flexible as possible.
  • Don't just stick all your functions in one file. Create a library file for different types of function (e.g. numeric.bb, string.bb etc.). This is important because when Blitz compiles, it will include ALL contents of an include file, which can increase the file size unnecessarily if you only use some of the functions.

Optional Parameters
There are two reasons for using optional parameters:

  1. Quite often you will write a function that will often use the same value for a parameter. Rather than pass this value as a parameter each time, you can set it as the default value within the function. Then you only need specify the parameter if you need a different value for it.
  2. If you add extra functionality to an existing function, it is important to maintain backwards compatibility so, programs calling a function using the original syntax will still work as intended. By making all new parameters optional you can maintain the original functionality.

To declare an optional parameter, you simply specify a default value for that variable as shown in the example below. Optional parameters must be declared after the required values and once you have specified one optional parameter, all parameters following this must also be optional.

Lets add some more parameters to our example:


As you can see, we have added two extra parameters to centre the text at the x position (instead of starting at the x position) and to change the gap between the text and the border.

When calling this function and you want to specify the gap, you must specify the centreText parameter too, even if you are using it's default value, since it is declared before the gap parameter.

Some Other Useful Tips
Notice in the example above that if the same calculation is being done more than once, it is assigned to a variable so we are not recalculating the same thing. This is another good practice to get into - if the function is used many times in a loop this will help to optimise the code.

Also, notice the indentation. This is used to distinguish between levels of nested code. This also helps make your program more readable since you can easily see where constructs like loops & If statements begin and end.

Well, I hope this has been helpful for you.

Happy coding...

This article can also be downloaded from my site: www.aurora-soft.co.uk


For a printable copy of this article, please click HERE.


This site is Copyright© 2000-2004, BlitzCoder. All rights reserved.