0% found this document useful (0 votes)
8 views5 pages

Debugging Techniques in Visual Basic

This document discusses debugging techniques used to identify and correct bugs in software. It describes three common debugging techniques: 1. Stubs - Small routines that temporarily replace subroutines that are not yet written or to isolate the source of an error. Stubs allow higher level modules to be tested before lower level modules are completed. 2. Flags - Boolean variables used to check if a section of code has executed. Flags help determine the execution path and identify where bugs may be occurring. 3. Debugging output statements - Statements added to output variable values or indicate execution points. This helps locate where errors first occur or see hidden processing results. Output statements are later removed once debugging is complete.

Uploaded by

tedzone9
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

Debugging Techniques in Visual Basic

This document discusses debugging techniques used to identify and correct bugs in software. It describes three common debugging techniques: 1. Stubs - Small routines that temporarily replace subroutines that are not yet written or to isolate the source of an error. Stubs allow higher level modules to be tested before lower level modules are completed. 2. Flags - Boolean variables used to check if a section of code has executed. Flags help determine the execution path and identify where bugs may be occurring. 3. Debugging output statements - Statements added to output variable values or indicate execution points. This helps locate where errors first occur or see hidden processing results. Output statements are later removed once debugging is complete.

Uploaded by

tedzone9
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

244 C hapter 5

Consider the following:

A Visual Basic procedure has been written to tally up the number of A, B, C, D and
Es stored in an array. The procedure does not work correctly yet no runtime errors
occur. The results are almost correct the first time the procedure is called, however
strange things seem to occur if subsequent calls Public Sub TallyGrades(
are made from the same routine. ByVal Grades() As String,
ByVal Total() As Integer)
The input data for the procedure is the Grades
array that is passed as a parameter to the Dim C ount As Integer
procedure. This array contains up to 50 grades For C ount = 1 To 49
and is indexed from 0 to 49. The Total array is Select C ase Grades(C ount)
C ase "A": Total(0) = Total(0) + 1
used to pass the results back to the calling
C ase "B": Total(1) = Total(1) + 1
routine. Total(0) holds the number of As, Total(1) C ase "C ": Total(2) = Total(1) + 1
the number of Bs, and so on. C ase "D": Total(3) = Total(3) + 1
C ase Else: Total(4) = Total(4) + 1
GROUP TASK Discussion End Select
Examine the code in Fig 5.25. There Next C ount
are a number of logic errors. Find End Sub
these errors and suggest methods F ig 5.25
for correcting each error. V isual B asic procedure to tally grades.
(I ncluding errors)

DEBUGGING TECHNIQUES
Errors or defects that cause software to malfunction in some way are called bugs.
Debugging is the process of identifying and correcting bugs. In this section we
consider some commonly used techniques used to assist in the debugging process.
The use of stubs, flags and debugging output statements are techniques allowing
programmers to isolate the source of
errors within source code. There are many Bug
other techniques, as well as many software An error or defect in software
tools, available to programmers. In the or hardware that causes a
HSC course we examine many more of program to malfunction.
these techniques and tools.
Stubs
As each higher-level subroutine is coded, it requires testing and debugging. Routines
that call lower-level routines are difficult to test until these lower-level routines are
coded. Stubs are used to alleviate this problem. A stub is a small routine that takes the
place of a yet to be written subroutine or is substituted for an existing subroutine when
attempting to locate the cause of an error.
Stubs allow the flow of execution to be checked before lower level subroutines are
coded. They can also be used to temporarily replace subroutines when attempting to
locate the source of an error. It is difficult to isolate the precise subroutine which is
causing an error. It is often helpful to systematically replace each called subroutine
with a simple stub which is known to return valid output.
Often a stub will set the value of appropriate variables or simply return valid output
and then end. Sometimes an output statement may be included within a stub to
indicate that a call has taken place. No real processing takes place within the stub, its
purpose is to allow testing of the calling routine or to assist the debugging process.

Software Design and Development –The Preliminary Course


I mplementing Software Solutions 245

Consider the following:

The simple structure chart in F ig 5.26 Main


describes the top-down design for a new Program
software application. The main program
will be coded first followed by the DoThis DoThis DoThat
and DoThat routines. In other words the 1 2
higher- level modules are coded first
followed by the lower-level modules. This GetIt SortIt FindIt MixIt
makes it difficult to test each module as it 1.1 1.2 2.1 2.2
is coded.
GrabIt CheckIt GetInput Shuffle ReHash
When the main program is being coded, 1.1.1 1.1.2 2.1.1 2.2.1 2.2.2
two stubs are written to take the place of
the DoThis and DoThat subroutines. F ig 5.26
Similarly when the DoThis routine is being T he top-down design for a fictitious program.
written, stubs are created to take the place
of the GetIt and SortIt subroutines.
GROUP TASK Discussion
It is normal to code software from the top down. Why do you think this is
a more common method than coding from the bottom up? Discuss.

GROUP TASK Discussion


Why bother creating stubs? Surely it would be better to code the entire
project and then test and debug the total solution. Do you agree? Discuss.

Flags
A flag is a Boolean variable used to check if a section of code has been executed.
Normally the flag is initialised to false and then set to true if the section of code is
executed. By examining the value of the flag, the programmer can determine the
execution path through the code. Knowing the execution path is of great assistance
when diagnosing the source of bugs. For example, when attempting to find the source
of an error the statement setting the flag to true can be systematically moved through
the code. If the flag is false then we are not following the execution path correctly,
conversely if it is true then we are.
Often a flag will be used to confirm that a particular condition has been met. When
debugging the programmer may suspect that say, a value becomes negative when it
should not. A statement can be added to the code that sets a flag variable should this
occur. For example, the Pascal statement TestFlag:= Average < 0 sets TestFlag to True
if Average is negative. If Average is not negative then TestFlag is set to False.
Flags are not only used for debugging, they can form part of the logic of the solution.
Often execution of particular sections of code depends on whether a certain condition
has been met. Flag variables provide a mechanism for communicating this
information to other parts of the program. For example, in the HSC course we
examine a number of algorithms for sorting data. A flag is used to signal that the data
is now sorted. As a consequence, the sort can end and execution can continue to other
sections of code.

Software Design and Development – The Preliminary Course


246 C hapter 5

GROUP TASK Discussion


Slips of paper are used as bookmarks to flag certain pages. Flags are used
by ships to communicate simple messages. White flags are carried as a sign
of peaceful intentions. Discuss the similarities between these types of flags
and the Boolean flags described above.

Debugging Output Statements


Output statements can be strategically placed within the code to indicate execution
has passed through that point. They can also be used to display the contents of
variables at particular points in the code. These statements are removed once the
source code has been tested and the bugs removed.
Often errors originating much earlier in the execution path do not surface until later in
the code. By strategically displaying the contents of variables at crucial points, the
source of many bugs can be determined.
Debugging output statements are particularly useful to display the result of processes
that are normally hidden from the user. For example, when reading data from a disk
file it is helpful during development to include an output statement to display each
data item obtained from the file immediately after it has been read. This allows the
programmer to view the raw data prior to processing.
Output statements are often used as part of a stub. A simple output statement
confirming that a call to a subroutine has occurred in many cases is the stub. Output
statements can also be used in conjunction with flags to alert the programmer that
some condition has been met.

Consider the following:

F ig 5.27 shows a simple screen and Visual Basic procedure used to test and debug the
TallyGrades procedure described in F ig 5.25. This code essentially opens a file and
loads the grades into an array. The TallyGrades procedure is called and then the results
are output. This code is known as a driver, its purpose is to test a lower- level routine.
Private Sub cmdOK_C lick(ByVal sender As [Link],
ByVal e As [Link]) Handles cmdOK.C lick
Dim Grade(50) As String
Dim Total(4) As Integer
Dim c As Integer
FileOpen(1, "c:\ [Link]", [Link])
c =0
While Not EOF(1) And c <= 50
Input(1, Grade(c ))
[Link] = [Link] & Grade(c )
c =c +1
End While
TallyGrades(Grade, Total)
For c = 0 To 4
[Link] = [Link] & " " & Total(c )
Next
FileC lose(1)
F ig 5.27
End Sub E xample of a driver used for debugging.
Software Design and Development –The Preliminary Course
I mplementing Software Solutions 247

GROUP TASK Discussion


There are three essential controls on the form shown in Fig 5.27. Can you
work out the name and purpose of each of these controls?

GROUP TASK Discussion


How does the code in Fig 5.27 help to simplify the testing and debugging
of the TallyGrades procedure? Discuss.

Automated debugging tools


Most modern programming environments provide a variety of automated debugging
tools to assist programmers locate and correct errors in their code. Common tools
include breakpoints, single line stepping and setting watch expressions.
Breakpoints halt execution and allow the contents of variables at that point to be
interrogated and even changed. Single line stepping causes execution to halt after each
line of code is executed. A single key press causes the next line to execute. Watch
expressions can be used to display the contents of variables during execution. They
can also cause execution to halt when a certain condition becomes true.

F ig 5.28
D ebugging in V isual B asic .N E T
Automated debugging tools can often be used instead of flags and debugging output
statements. They can also provide a means for performing an automated desk check of
the source code.
GROUP TASK Activity
Examine the automated debugging tools available in a programming
environment with which you are familiar. List and describe each of the
available tools.

Software Design and Development – The Preliminary Course


248 C hapter 5

SET 5C
1. The common name given to a defect in 6. A Boolean variable that is used to check
software or hardware that causes a if a section of code has been executed is
program to malfunction is called a: called a:
(A) runtime error. (A) flag.
(B) fatal error. (B) stub.
(C) blue screen of death. (C) pause statement.
(D) bug. (D) signal.
2. What can a programmer use to test a 7. Which debugging tool allows the
routine that relies on a yet to be written content of variables to be interrogated
lower-level routine? by halting execution?
(A) A flag. (A) Stubs.
(B) A stub. (B) Breakpoints.
(C) A breakpoint. (C) Flags.
(D) A watch expression. (D) Watch expressions.
3. Which debugging tool allows the 8. Other than breakpoints, what other
contents of variables to be displayed technique could a programmer use to
while execution takes place? display the content of variables at
(A) Watch expressions. various stages of execution?
(B) Breakpoints. (A) Flags.
(C) Flags. (B) Drivers.
(D) Stubs. (C) Stubs.
4. When the computer is unable to (D) Output statements.
continue executing instructions, what is
9. When the rules of a programming
the error that has occurred?
language have been violated, which
(A) Syntax error.
error is most likely to occur?
(B) Logic error.
(A) Syntax error.
(C) Runtime error.
(B) Runtime error.
(D) Operating system error.
(C) Operating system error.
5. The translation process comprises of (D) Logic error.
which three main steps?
(A) Lexical analysis, syntactical 10. If a program continues successfully
analysis and code generation. through execution however, the results
(B) Lexical analysis, syntactical are not as they are anticipated, what type
analysis and runtime error analysis. of error has most likely occurred?
(C) Lexical analysis, logic error (A) Logic error.
analysis and syntactical analysis. (B) Runtime error.
(D) Lexical analysis, logic error (C) Syntax error.
analysis and code generation. (D) Lexical error.

11. Runtime and logic errors are not mutually exclusive. What in the world does this mean?
Find out and then give an example of an error that illustrates the meaning.

12. In the text, we discussed stubs in detail and we also discussed briefly, a new type of driver.
Stubs and drivers are similar but not the same. Describe the similarities and differences.

13. Debugging output statements can be used in various ways with other debugging techniques.
Describe at least two such uses.

14. Why is it that syntax errors are never encountered when using commercially produced software
products?

15. In the text, the Pascal statement TestFlag:= Average < 0 was used in connection with setting
flags. Explain how this statement works.

Software Design and Development –The Preliminary Course

You might also like