This article includes following tips about how to debug program written in Small Basic language. And also this article shows you real samples for debugging with published programs.
When you find something wrong in you program, how do you find the cause? Small Basic IDE (integrated development environment) doesn't have ability such like variable watching. But in many cases, some variables have unexpected values in them. To find that,
let's add TextWindow.Write() or TextWindow.WriteLine() to look in variables for debugging.
To show an array "arry", you can simply use TextWindow.WriteLine(arry) or write code like the following code:
num = Array.GetItemCount(arry)
index = Array.GetAllIndices(arry)
+ index[i] +
For text base program debugging, changing foreground color may help you to distinguish program original outputs and debugging messages.
For short value to display for debugging, you can use TextWindow.Title or GraphicsWindow.Title also.
If you wrote a subroutine to show such like an array for debug, that code will be useful also in the future. But, these code will be needed only when you debug. So it is a way to use debug flag to on and off your debug routines.
Maze 0.4 (PNC833-0) is a sample which has debug flag. Following line sets debug flag off. And debug = "True" will turn on the flag.
The following lines call a subroutine to show array "cell" of created maze as text.
The following lines slow down creating maze to look and show variables and pause the program using TextWindow.Read().
2. Add the neighboring walls of the cell to the wall list.
And after debugging is completed, these lines are easily found (with [Ctrl]+F in Small Basic IDE) and removed. Actually these routines are removed in Maze 0.5 (not published).
To write generic subroutines (for general-purpose) will help your productivity. In my case, some subroutines for colors, maths, mouse, and etc. are reused in many programs. But generic subroutines should be well tested and have no bugs. No (or less) bugs
will be a prior condition for productivity.
To test your subroutines, it is a reasonable way to write a test program for them. Good test program will find bugs from your subroutines and make easy to check regression after debugging.
A program which calculate combination nCr for big n (CPQ608) has a test routine TestDiv() for a generic subroutine Div().
Following parameters were found with TestDiv() and caused problem in Div().
'a = "434399216531770650390143258708"
'b = "6752306690329"
'a = "397896921587794748049229269710"
'b = "8642083658481"
The following list was written when Shapes 1.1 (TLW744) was in debugging. When you write lots of code, you may happen to meet many strange behaviors in your program. If so, it will help you that you write a list that includes each phenomenon. Because
there may be many different causes of the phenomena. And after debugging, this list will be a good test set for your program.
There are two types of error messages given by Small Basic. One is compile errors. Another one is runtime errors. These messages help your development.
Compile errors appear under source code just after "Run" button is clicked if your program has any syntax errors. The following picture shows errors of a program "Fifty" (BRQ733) :
Sorry, we found some errors...
11,28: The variable 'files' is used, but its value is not assigned. Are you sure you have spelled it correctly?
29,24: The variable 'buf' is used, but its value is not assigned. Are you sure you have spelled it correctly?
Numbers mean lines and columns of source code, and you can jump into the point in source program by double clicking the error message. These kind of compile error messages show you what happened and some advice to fix them. In this case, errors happened
because lines with File.GetFiles(path) and File.ReadContents(filename) have been automatically commented out by Small Basic server.
On the other hand, a runtime error appears while a program runs but happens to meet trouble which make the program stop. Following picture is a sample of divsion by 0 error message.
Value was either too large or too small for a Decimal.
at System.Decimal..ctor(Double value)
at System.Decimal.op_Explicit(Double value)
at Microsoft.SmallBasic.Library.Math.Remainder(Primitive dividend, Primitive divisor)
The list shown in the text box is called stack trace. This list shows you relations which subroutine calls which subroutine at the error. In this case, the stack trace shows that main part of the program calls Math.Remainder() and occurs error.
Despite writing many debug routines, some bugs may be too complicated to find their causes. As a last option, it will work for you to use "Graduate" button to convert Small Basic program to Visual Basic program. This allows you to use strong Visual Studio
debugger to find causes of bugs originally in your Small Basic program.
Step 1: Install Visual Basic 2010 Express if you don't install yet.
Step 2: Push "Graduate" button and input folder name for converted Visual Basic program.
Step 3: Push [Next] or [Finish] buttons in Visual Studio conversion wizard. Program XXX.sb (or XXX.smallbasic) will be converted to XXXModule.vb.
Step 4: Select [PROJECT] [Add Reference] [Browse] SmallBasicLibrary.dll [Add] [OK].
Step 5: Rewrite from 'For i = 1 To n' to 'For XXXModule.i = 1 To n' to avoid scope error.
Step 6: Double click a source line to make the line as break point if needed.
Step 7: Push "Debug Start" button or [F5] key to start program.
Step 8: If program stops at break point, push "Step In (F8)" or "Step Over (Shift+F8)" to continue.
Step 9: See "Auto Variable", "Local" or "Watch" tab and confirm the values of variables.
Step 10: If you find a bug, push "Stop Debugging" button and rewrite your program with comment.
Step 11: Repeat from step 6 again.
If you confirmed a bug fix, go back to Small Basic IDE and fix program just as you did in Visual Studio.
There are some differences between Small Basic and Visual Basic syntax as listed below.
These tips are outputs from programming Small Basic within the last couple of years but not everything. It is important to be creative with programming and debugging. If you find good way, please add the way into this article. Small Basic is very compact
and a good language to learn programming with. It's easy and powerful.