Some Notes on Program Development and Debugging
The following notes highlight various elements of program development and
debugging. The notes identify several practices that arise naturally in
this course. These notes are not intended to be exhaustive.
Suggestions for this page are encouraged!
Program Development
-
Program Format Parallels Function
-
Variable names should be descriptive
-
Indent lines within if, while, etc.
-
Consistency of problem and data structure(s)
-
Use a data structure that fits conceptually with the problem
-
Translation between problem concept and implementation details
invites complexity and error
-
Arrays
-
Use an array when a sequence of data of known size must be used and
reused
-
Do not use an array
-
when an item can be read, processed, and forgotten
-
when the size of the data set is unknown
-
No I/O inside computational utility functions
-
When functions can be called from various places and/or in various
contexts, pass values in and out without I/O
-
Put I/O in code that uses the functions, so I/O can be tailed to
different parts of the program
-
Refactoring
-
When the same work is done in several places in a program,
reorganize the code to place common pieces in separate procedures
-
Incremental Development
-
First write a shell with the right structure, but little or no
functionality. (In Extreme Programming, this is called a 0-feature
release.)
-
Add minimal functionality to be able to input data element,
place it in an appropriate structure, and print it
-
Progressively add and test one new function at a time
By keeping new, untested code to a few lines at a time,
errors likely will be restricted to a small code segment
Debugging Hints
-
Test cases
-
Black Box Testing: consider logical possibilities of problem
-
White Box Testing: use code to guide testing
-
Boundary conditions: Does loop start and stop at the right time
-
Every path test: Some test case should exercise each piece of code
-
Tracing Variables
-
Use one or more global variables to guard debugging print statements
-
Setting variable to 1, turns on debugging prints
-
Setting variable to 0, turns off debugging prints
-
Different variables could turn on and off different categories of
printing
This approach minimizes the risk that insertion/deletion of debugging
code will add new errors.