Transcript Bugs.

Bugs
how to prevent them,
how to find them and
how to terminate them
CS100
Bugs
• Programming Errors
• First bug
– A moth stuck in a Harvard Mark II
mainframe in 1947.
Bugs are bad
• 1990 – AT&T long distance service
failed for 9 hours and was traced to a
single faulty line of code
• 1991 – Scud missile killed 28 soldiers
because a bug caused the Patriot
defense system to be off by 0.34
seconds
• 2000 – Y2K
Today’s Lecture
• How to prevent bugs ?
– Understand the problem
– Understand Java
– Follow good programming practices
• How to find bugs ?
– Testing
• How to kill bugs
Program Development
Design
Implement
Testing
Program Design : Classes
Student
double averageGrade
int grades[6]
double weight[6]
calcAverageGrade()
getAverageGrade()
LetterGradeStudent
printGrade()
PassFailStudent
printGrade()
Course
double averageGrade
double max
double min
double sum
int numOfStudent
Student students[]
calcAverageGrade()
getAverageGrade()
Program Design : Pseudocode
Average grade for students
for ( i = 0 .. 6)
average += grade[i]*weight[i]
return average
Average Grade for course
for each student
sum += student’s weight average
return sum/number of students;
Get input
get number of students
for i = 1 .. number of students
get name
get enrollment status
get all six grades
if enroll as pass fail then
create a PassFailStudent
else
create a LetterGradeStudent
Program Design : Data Flow
pass/fail ?
name
6 grades
create a student
final grade
get final grade
Student obj
calc student’s
average grade
average grade
Program Design : Control Flow
i=0
i == # of
students ?
get name
get status
get grades
increment i
Good Program vs. Bad Program
• Easy to Read
–
–
–
–
Good Comments
Meaningful Names
Properly Indented
Blank Lines
• Well-Designed
– Covered all cases
– Anticipate Changes
– Reusable
Good Design
•
•
•
•
Anticipate Changes
Reusable
Encapsulation
Think “LEGO”
What if ..
• create histogram for data between 1 .. 200 ?
• tally data for smaller intervals ?
1 – 5 | **
6 – 10 | *****
11 – 15 | *
:
:
196 – 200 | ***
• draw a histogram for average grades of all
students for this class ?
Bug Prevention
• code reuse
– fewer lines of code to write, fewer bugs
• anticipate changes
– fewer lines of code to change,
fewer bugs
• encapsulation
– bugs are confined to one place, easier to
detect and fix.
Good Programs
• Easy to read
– blank lines
– indentation
– comments
– meaningful names
• Easy to read, easy to spot bugs !
Finding Bugs
• Wrong attitude :
“My program works ! I am done.”
• Did you test it with all possible inputs ?
– negative numbers ?
– zero ?
• Did you test all possible path of
execution ?
Component Testing
• Another motivation for encapsulations !
• Test each component separately
• Make sure they worked before using
them
Debugging Techniques :
Think “high-level”
1. scan from left to right
2. swap two adjacent elements if
they are out of order
3. repeat until everything is in
order
Debugging Techniques :
Printout
•
•
•
•
•
Print out your code
Spread it on a large table
Walkthrough your code
Draw diagrams
Make notes
Debugging Techniques :
Explain it to Someone Else
• Old Chinese Proverb :
“Onlookers see most of the game;
Players see very little”
Debugging Techniques :
System.err.println
• Let you inspect the intermediate value
of variables
53
34
10
82
72
2147483647
2147483647
2147483647
2147483647
2147483647
0
0
0
0
0
• Can you guess what is wrong now ?
Debugging Techniques :
assert()
• a method to make sure that your
invariants are true.
void assert(boolean condition, String errorMessage)
{
if (!condition)
throw new Error(errorMessage);
}
Debugging Techniques :
assert()
• The Error exception will cause the
stack trace to be printed.
java.lang.Error: data 364 is out of range
at java.lang.Throwable.<init>
at java.lang.Error.<init>
at Histogram.assert
at Histogram.addData
at P2Q4.createHistogram
at P2Q4.main
Debugging Techniques :
Debugger
breakpoint
pause execution
step
incremental execution
continue
resume execution
watch
stop if value of a
variable changes
call stack
variables
list currently active
frames
inspect value of
variables
Summary
• Bug Prevention
– understand the problem and language
– design before sit in front of computer
– design for change/reuse
• Bug Discovery
– test all flow of controls
– test small components separately before
using it
Summary
• Bug Termination
– re-think your algorithm from a higher-level
– manually trace through your program
– explain your program to others
– System.err.print
– assert()
– use a Debugger