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:
TextWindow.ForegroundColor =
"Yellow"
num = Array.GetItemCount(arry)
index = Array.GetAllIndices(arry)
For
i =
1
To
num
TextWindow.WriteLine(
"arry["
+ index[i] +
"]="
+ arry[index[i]])
EndFor
"Gray"
For text base program debugging, changing foreground color may help you to distinguish program original outputs and debugging messages.
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.
27.
debug =
"False"
The following lines call a subroutine to show array "cell" of created maze as text.
61.
If
debug
Then
62.
DumpMaze()
63.
TextWindow.WriteLine(title)
64.
EndIf
The following lines slow down creating maze to look and show variables and pause the program using TextWindow.Read().
229.
230.
Program.Delay(
20
)
231.
232.
' 2. Add the neighboring walls of the cell to the wall list.
234.
AddWallToList()
235.
236.
"iWalls="
+ iWalls)
237.
TextWindow.Write(
"nWalls="
+ nWalls)
238.
TextWindow.Read()
239.
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 available 'files' is used, but its value is not assigned. Are you sure you have spelled it correctly? 29,24: The available '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.
Decimal type value is too large or too small. Location System.Decimal..ctor(Double value) Location Microsoft.SmallBasic.Library.Math.Remainder(Primitive dividend, Primitive divisor) Location _SmallBasicProgram._Main()
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: Rewrite from 'For i = 1 To n' to 'For XXXModule.i = 1 To n' to avoid scope error. Step 5: Double click a source line to make the line as break point if needed. Step 6: Push "Debug Start" button or [F5] key to start program. Step 7: If program stops at break point, push "Step In (F8)" or "Step Over (Shift+F8)" to continue. Step 8: See "Auto Variable", "Local" or "Watch" tab and confirm the values of variables. Step 9: If you find a bug, push "Stop Debugging" button and rewrite your program with comment. Step 10: 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.
Nonki Takahashi edited Revision 29. Comment: changed the way to avoid scope error
Nonki Takahashi edited Revision 28. Comment: Additional information for For block variable
Nonki Takahashi edited Revision 27. Comment: added For block variable information for Visual Basic
Nonki Takahashi edited Revision 26. Comment: rewrote about array in Visual Basic
Nonki Takahashi edited Revision 25. Comment: Added another difference between Small Basic and Visual Basic
Ed Price - MSFT edited Revision 23. Comment: Serhard, we're not adding "(en-US)" to the titles anymore because it causes difficulties. But please keep adding "en-US" to the tags of English articles. Thanks!
Nonki Takahashi edited Revision 19. Comment: for boolean as type Primitive in VB
Nonki Takahashi edited Revision 18. Comment: for two dimensional array
This is great, Nonki!
Another possibility is to use LDDebug in the LitDev extension.
Anounced in :
social.msdn.microsoft.com/.../819fd350-6895-4746-b825-b9b91027d495
All good advice and clearly explained. Only things I would add are:
Test thoroughly at each stage, not just at the end.
If you find an error one way to debug is to systematically simplify the code be deleting irrelevant bits while checking the error is still there.
Try creating a simple prototype to reproduce the error with the simplest code possible.