bugs

25
Bugs CS100 how to prevent them, how to find them and how to terminate them

Upload: nico

Post on 06-Jan-2016

67 views

Category:

Documents


0 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Bugs

Bugs

CS100

how to prevent them, how to find them and how to terminate them

Page 2: Bugs

Bugs

• Programming Errors• First bug

– A moth stuck in a Harvard Mark II mainframe in 1947.

Page 3: Bugs

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

Page 4: Bugs

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

Page 5: Bugs

Program Development

Design

Implement

Testing

Page 6: Bugs

Program Design : Classes

Studentdouble averageGradeint grades[6]double weight[6]calcAverageGrade()getAverageGrade()

LetterGradeStudentprintGrade()

PassFailStudentprintGrade()

Coursedouble averageGradedouble maxdouble mindouble sumint numOfStudentStudent students[]calcAverageGrade()getAverageGrade()

Page 7: Bugs

Program Design : Pseudocode

Average grade for studentsfor ( i = 0 .. 6) average += grade[i]*weight[i]return average

Average Grade for coursefor each student sum += student’s weight averagereturn sum/number of students;

Get inputget number of studentsfor 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

Page 8: Bugs

Program Design : Data Flow

create a student

name

pass/fail ?

6 grades

calc student’s average grade

Student objget final grade

average grade

final grade

Page 9: Bugs

Program Design : Control Flow

i = 0

i == # of students ?

get nameget statusget grades

increment i

Page 10: Bugs

Good Program vs. Bad Program

• Easy to Read– Good Comments– Meaningful Names– Properly Indented– Blank Lines

• Well-Designed– Covered all cases– Anticipate Changes– Reusable

Page 11: Bugs

Good Design

• Anticipate Changes• Reusable• Encapsulation• Think “LEGO”

Page 12: Bugs

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 ?

Page 13: Bugs

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.

Page 14: Bugs

Good Programs

• Easy to read– blank lines– indentation– comments– meaningful names

• Easy to read, easy to spot bugs !

Page 15: 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 ?

Page 16: Bugs

Component Testing

• Another motivation for encapsulations !

• Test each component separately• Make sure they worked before

using them

Page 17: Bugs

Debugging Techniques : Think “high-level”

1. scan from left to right2. swap two adjacent elements

if they are out of order3. repeat until everything is in

order

Page 18: Bugs

Debugging Techniques :Printout

• Print out your code• Spread it on a large table• Walkthrough your code• Draw diagrams• Make notes

Page 19: Bugs

Debugging Techniques : Explain it to Someone Else

• Old Chinese Proverb :

“Onlookers see most of the game; Players see very little”

Page 20: Bugs

Debugging Techniques :System.err.println

• Let you inspect the intermediate value of variables

53 2147483647 034 2147483647 010 2147483647 0 82 2147483647 0 72 2147483647 0

• Can you guess what is wrong now ?

Page 21: Bugs

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);

}

Page 22: Bugs

Debugging Techniques : assert()

• The Error exception will cause the stack trace to be printed.

java.lang.Error: data 364 is out of rangeat java.lang.Throwable.<init>at java.lang.Error.<init>at Histogram.assertat Histogram.addDataat P2Q4.createHistogramat P2Q4.main

Page 23: Bugs

Debugging Techniques :Debugger

breakpoint pause execution

stepincremental execution

continue resume execution

watchstop if value of a variable changes

call stacklist currently active frames

variablesinspect value of variables

Page 24: Bugs

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

Page 25: Bugs

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