Small Basic Curriculum: Lesson 3.6: Debugging Aids
Small Basic > Curriculum >** **Online > Lesson 3.6: Debugging Aids
Estimated time to complete this lesson: 1 hour
Debugging Aids
In this lesson, you will learn how to:
- Use the TextWindow.WriteLine operation to debug your programs.
Debugging
Debugging is how you detect and resolve any defects in your computer program. Every programmer develops the skill to detect bugs and debug his or her program.
Even a minor bug in a program can defeat the efforts of the programmer!
While working on large programs, programmers write debugging support code that is not included in the final program. This code helps determine the point at which a program fails to run.
TextWindow.WriteLine As a Debugging Helper
So how can you debug programs in Small Basic?
To debug your program, you can use the TextWindow.WriteLine operation. This operation acts as a debugging helper, and it provides information to help debug your program.
Let’s look at an example that uses the TextWindow.WriteLine operation for debugging.
In this program, you first display an ellipse in the graphics window. If your program does not run as expected, you must debug it!
You can use the TextWindow.WriteLine operation as a debugger at this point. This operation can help you trace the value of “i” on every iteration of the For…EndFor loop. The value of “i” appears in a separate text window so that you can easily detect a bug and fix it.
GraphicsWindow.Height = 400
GraphicsWindow.Width = 600
Ellipse = Shapes.AddEllipse(200,200)
Shapes.Move(Ellipse,200,100)
For i = 1 To 5
Program.Delay(1000)
Shapes.SetOpacity(Ellipse,5*i)
Shapes.Zoom(Ellipse,i * 0.4,i * 0.4)
TextWindow.WriteLine("Value of i at this moment is " + i)
EndFor
http://msdn.microsoft.com/gg715117.Debugging-(en-us,MSDN.10).jpg
Now, let’s use the TextWindow.WriteLine operation in another program and see how it can help you debug the program.
GraphicsWindow.Show()
GraphicsWindow.DrawImage(Flickr.GetPictureOfMoment(), 0, 0)
Program.Delay(2000)
GraphicsWindow.Clear()
In this example, you use the Flickr object to download a photo from www.flickr.com and display it on the graphics window.
http://msdn.microsoft.com/gg715117.Graphics_Window(en-us,MSDN.10).jpg
The image that you requested does not appear in the graphics window. It’s time to debug your code!
You might not be connected to the Internet, or you might not have access to the website.
To check this possibility, you can use the TextWindow.WriteLine operation to display the path of the image from the website. If the Internet connection is working correctly, the path of the image will appear in the text window. Otherwise, the text window will not display any path.
GraphicsWindow.Show()
GraphicsWindow.DrawImage(Flickr.GetPictureOfMoment(), 0, 0)
TextWindow.WriteLine("The path of image from flickr" + Flickr.GetPictureOfMoment())
Program.Delay(2000)
GraphicsWindow.Clear()
http://msdn.microsoft.com/gg715117.Debugging_Helper(en-us,MSDN.10).jpg
As you can see, the text window is not displaying the path of the image, which means that you are not connected to the Internet.
Now, let’s write a program to copy a file and display its contents in the text window.
In this example, if the CopyFile operation is successful, the text window displays the contents of the file.
Sourcepath = Program.Directory+"\Source\data.txt"
Destpath = Program.Directory+"\dest"
file.CopyFile(Sourcepath,Destpath)
TextWindow.WriteLine(File.ReadContents(File.ReadContents(destPath+"\data.txt")))
You may not see the contents of the file in the text window because:
- The specified source or destination may be incorrect.
- The file may not exist at the specified source.
- The name of the specified file may be incorrect.
http://msdn.microsoft.com/gg715117.Debugging_Aids(en-us,MSDN.10).jpg
Now how do we detect the error? It’s time to debug your code again…
Let’s use the TextWindow.WriteLine operation to detect the error.
Sourcepath = Program.Directory+"\Source\data.txt"
Destpath = Program.Directory+"\dest"
TextWindow.WriteLine(file.CopyFile(Sourcepath,Destpath))
TextWindow.WriteLine(File.ReadContents(File.ReadContents(destPath+"\data.txt")))
http://msdn.microsoft.com/gg715117.Failed(en-us,MSDN.10).jpg
You can modify your program and use TextWindow.WriteLine to display the return value of the CopyFile operation in the text window. The return value will be either SUCCESS or FAILED.
So, if your program cannot run because of the CopyFile operation error, you immediately know about it!
Let’s Summarize…
Congratulations!
Now you know how to:
- Use the TextWindow.WriteLine operation to debug programs.
Show What You Know
A program creates a slide show of images in the graphics window. All these images are stored in a local folder. With every mouse click, a different image appears in the graphics window.
Use the TextWindow.WriteLine operation as a debugging helper by displaying the path of each image in the text window.
To see the answers to these questions, go to the Answer Key page.
PowerPoint Downloads