development strategies, function design › ~bbm101 › fall15 › lectures › w07-de… · slides...

36
Development Strategies, Function Design BBM 101 - Introduction to Programming I Hacettepe University Fall 2015 Fuat Akal, Aykut Erdem, Erkut Erdem, Vahid Garousi

Upload: others

Post on 07-Jul-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Development Strategies, Function Design › ~bbm101 › fall15 › lectures › w07-de… · Slides based on material prepared by E. Grimson, J. Guttagand C. Terman in MITx 6.00.1x

DevelopmentStrategies,FunctionDesignBBM101- Introduction toProgramming I

Hacettepe UniversityFall2015

FuatAkal,AykutErdem,Erkut Erdem,Vahid Garousi

Page 2: Development Strategies, Function Design › ~bbm101 › fall15 › lectures › w07-de… · Slides based on material prepared by E. Grimson, J. Guttagand C. Terman in MITx 6.00.1x

Today

• Howtodevelopaprogram– Programdevelopmentstrategy

• MoreonTestingandDebugging– Testinganddebugging– Blackboxtesting– Glassboxtesting– Integrationtestingandunittesting– Debuggingapproaches

2

Page 3: Development Strategies, Function Design › ~bbm101 › fall15 › lectures › w07-de… · Slides based on material prepared by E. Grimson, J. Guttagand C. Terman in MITx 6.00.1x

Today

• Howtodevelopaprogram– Programdevelopmentstrategy

• MoreonTestingandDebugging– Testinganddebugging– Blackboxtesting– Glassboxtesting– Integrationtestingandunittesting– Debuggingapproaches

Slides basedonmaterialpreparedbyRuthAnderson,MichaelErnstandBillHoweinthecourseCSE140University ofWashington

3

Page 4: Development Strategies, Function Design › ~bbm101 › fall15 › lectures › w07-de… · Slides based on material prepared by E. Grimson, J. Guttagand C. Terman in MITx 6.00.1x

Programdevelopmentmethodology:Englishfirst,thenPython

1. Definetheproblem2. Decideuponanalgorithm3. Translateitintocode

Trytodothesestepsinorder

4

Page 5: Development Strategies, Function Design › ~bbm101 › fall15 › lectures › w07-de… · Slides based on material prepared by E. Grimson, J. Guttagand C. Terman in MITx 6.00.1x

Programdevelopmentmethodology:Englishfirst,thenPython

1. DefinetheproblemA. WriteanEnglishdescriptionoftheinputandoutput

forthewholeprogram.(Donotgivedetailsabouthowyouwillcomputetheoutput.)

B. Createtestcasesforthewholeprogram• Inputand expectedoutput

2. Decideuponanalgorithm3. Translateitintocode

Trytodothesestepsinorder

5

Page 6: Development Strategies, Function Design › ~bbm101 › fall15 › lectures › w07-de… · Slides based on material prepared by E. Grimson, J. Guttagand C. Terman in MITx 6.00.1x

Programdevelopmentmethodology:Englishfirst,thenPython

1. Definetheproblem2. Decideuponanalgorithm

A. ImplementitinEnglish• Writetherecipeor step-by-stepinstructions

B. Testitusingpaperandpencil• Usesmallbutnottrivialtestcases• Playcomputer,animatingthealgorithm• Beintrospective

– Noticewhatyoureallydo– Maybemoreorlessthanwhatyouwrotedown– Makethealgorithmmoreprecise

3. Translateitintocode

Trytodothesestepsinorder

6

Page 7: Development Strategies, Function Design › ~bbm101 › fall15 › lectures › w07-de… · Slides based on material prepared by E. Grimson, J. Guttagand C. Terman in MITx 6.00.1x

Programdevelopmentmethodology:Englishfirst,thenPython

1. Definetheproblem2. Decideuponanalgorithm3. Translateitintocode

A. ImplementitinPython• Decomposeitintologicalunits(functions)• Foreachfunction:

– Nameit(importantanddifficult!)– Writeitsdocumentationstring(itsspecification)– Writetests– Writeitscode– Testthefunction

B. Testthewholeprogram

Trytodothesestepsinorder

7

Page 8: Development Strategies, Function Design › ~bbm101 › fall15 › lectures › w07-de… · Slides based on material prepared by E. Grimson, J. Guttagand C. Terman in MITx 6.00.1x

Programdevelopmentmethodology:Englishfirst,thenPython

1. Definetheproblem2. Decideuponanalgorithm3. Translateitintocode

Trytodothesestepsinorder– It’sOK(evencommon)tobackuptoapreviousstep

whenyounoticeaproblem– Youareincrementallylearningabouttheproblem,

thealgorithm,andthecode– “Iterativedevelopment”

8

Page 9: Development Strategies, Function Design › ~bbm101 › fall15 › lectures › w07-de… · Slides based on material prepared by E. Grimson, J. Guttagand C. Terman in MITx 6.00.1x

TheWishfulThinkingapproachtoimplementingafunction

• Ifyouarenotsurehowtoimplementonepartofyourfunction,defineahelperfunctionthatdoesthattask– “IwishIknewhowtodotaskX”– Giveitanameandassumethatitworks– Goaheadandcompletetheimplementationofyourfunction,using thehelperfunction(andassumingitworks)

– Later,implementthehelperfunction– Thehelperfunctionshouldhaveasimpler/smallertask

9

Page 10: Development Strategies, Function Design › ~bbm101 › fall15 › lectures › w07-de… · Slides based on material prepared by E. Grimson, J. Guttagand C. Terman in MITx 6.00.1x

TheWishfulThinkingapproachtoimplementingafunction

• Canyoutesttheoriginalfunction?– Yes,byusingastub forthehelperfunction– Oftenalookuptable:worksforonly5inputs,crashesotherwise,ormaybejustreturnsthesamevalueeverytime

10

Page 11: Development Strategies, Function Design › ~bbm101 › fall15 › lectures › w07-de… · Slides based on material prepared by E. Grimson, J. Guttagand C. Terman in MITx 6.00.1x

Whyfunctions?Thereareseveralreasons:• Creatinganewfunctiongivesyouanopportunitytonameagroup

ofstatements,whichmakesyourprogrameasiertoreadanddebug.

• Functionscanmakeaprogramsmaller byeliminatingrepetitivecode.Later,ifyoumakeachange,youonlyhavetomakeitinoneplace.

• Dividingalongprogramintofunctionsallowsyoutodebugthepartsoneatatimeandthenassemblethemintoaworkingwhole.

• Well-designedfunctionsareoftenusefulformanyprograms.Onceyouwriteanddebugone,youcanreuseit.

11

Page 12: Development Strategies, Function Design › ~bbm101 › fall15 › lectures › w07-de… · Slides based on material prepared by E. Grimson, J. Guttagand C. Terman in MITx 6.00.1x

Today

• Howtodevelopaprogram– Programdevelopmentstrategy

• MoreonTestingandDebugging– Testinganddebugging– Blackboxtesting– Glassboxtesting– Integrationtestingandunittesting– Debuggingapproaches

Slides basedonmaterialpreparedbyE.Grimson, J.Guttag andC.Terman inMITx 6.00.1x 12

Page 13: Development Strategies, Function Design › ~bbm101 › fall15 › lectures › w07-de… · Slides based on material prepared by E. Grimson, J. Guttagand C. Terman in MITx 6.00.1x

TestingandDebugging

• Wouldbegreatifourcodealwaysworkedproperlythefirsttimewerunit!

• Butlifeain’t perfect,soweneed:– Testingmethods

• Waysoftryingcodeonexamplestodetermineifrunningcorrectly

– Debuggingmethods• Waysoffixingaprogramthatyouknowdoesnotworkasintended

13

Page 14: Development Strategies, Function Design › ~bbm101 › fall15 › lectures › w07-de… · Slides based on material prepared by E. Grimson, J. Guttagand C. Terman in MITx 6.00.1x

Whenshouldyoutestanddebug?

• Designyourcodeforeaseoftestinganddebugging– Breakprogramintocomponentsthatcanbetestedanddebuggedindependently

– Documentconstraintsonmodules• Expectationsoninputs,onoutputs• Evenifcodedoesnotenforceconstraints,valuablefordebuggingtohavedescription

– Documentassumptionsbehindcodedesign

14

Page 15: Development Strategies, Function Design › ~bbm101 › fall15 › lectures › w07-de… · Slides based on material prepared by E. Grimson, J. Guttagand C. Terman in MITx 6.00.1x

Whenareyoureadytotest?

• Ensurethatcodewillactuallyrun– Removesyntaxerrors– Removestaticsemanticerrors– BothofthesearetypicallyhandledbythePythoninterpreter

• Haveasetofexpectedresults(i.e.input- outputpairings)ready

15

Page 16: Development Strategies, Function Design › ~bbm101 › fall15 › lectures › w07-de… · Slides based on material prepared by E. Grimson, J. Guttagand C. Terman in MITx 6.00.1x

Testing

• Goal:– Showthatbugsexist– Wouldbegreattoprovecodeisbugfree,butgenerallyhard• Usuallycan’trunonallpossibleinputstocheck• Formalmethodssometimeshelp,butusuallyonlyonsimplercode

“Program testing can be used to show the presence ofbugs, but never to show their absence!”

– Edsger Dijkstra

16

Page 17: Development Strategies, Function Design › ~bbm101 › fall15 › lectures › w07-de… · Slides based on material prepared by E. Grimson, J. Guttagand C. Terman in MITx 6.00.1x

Testsuite

• Wanttofindacollectionofinputsthathashighlikelihoodofrevealingbugs,yetisefficient– Partitionspaceofinputs intosubsetsthatprovideequivalentinformationaboutcorrectness• Partitiondividesasetintogroupofsubsetssuchthateachelementofsetisinexactlyonesubset

– Constructtestsuite thatcontainsoneinputfromeachelementofpartition

– Runtestsuite

17

Page 18: Development Strategies, Function Design › ~bbm101 › fall15 › lectures › w07-de… · Slides based on material prepared by E. Grimson, J. Guttagand C. Terman in MITx 6.00.1x

Exampleofpartitiondef isBigger(x, y):

“““Assumes x and y are intsreturns True if x is less than yelse False”””

• Inputspaceisallpairsofintegers• Possiblepartition

– xpositive,ypositive– xnegative,ynegative– xpositive,ynegative– xnegative,ypositive– x=0,y=0– x=0,y!=0– x!=0,y=0

18

Page 19: Development Strategies, Function Design › ~bbm101 › fall15 › lectures › w07-de… · Slides based on material prepared by E. Grimson, J. Guttagand C. Terman in MITx 6.00.1x

Whythispartition?

• Lotsofotherchoices– E.g.,xprime,ynot;yprime,xnot;bothprime;bothnot

• Spaceofinputsoftenhavenaturalboundaries– Integersarepositive,negativeorzero– Fromthisperspective,have9subsets•Splitx=0,y!=0intox=0,ypositiveandx=0,ynegative•Sameforx!=0,y=0

19

Page 20: Development Strategies, Function Design › ~bbm101 › fall15 › lectures › w07-de… · Slides based on material prepared by E. Grimson, J. Guttagand C. Terman in MITx 6.00.1x

Partitioning

• Whatifnonaturalpartitiontoinputspace?

– Randomtesting– probabilitythatcodeiscorrectincreaseswithnumberoftrials;butshouldbeabletousecodetodobetter

– Useheuristicsbasedonexploringpathsthroughthespecifications– black-boxtesting

– Useheuristicsbasedonexploringpathsthroughthecode– glass-boxtesting

20

Page 21: Development Strategies, Function Design › ~bbm101 › fall15 › lectures › w07-de… · Slides based on material prepared by E. Grimson, J. Guttagand C. Terman in MITx 6.00.1x

Black-boxtesting

• Testsuitedesignedwithoutlookingatcode– Canbedonebysomeoneotherthanimplementer– Willavoidinherentbiasesofimplementer,exposingpotentialbugsmoreeasily

– Testingdesignedwithoutknowledgeofimplementation,thuscanbereusedevenifimplementationchanged

21

Page 22: Development Strategies, Function Design › ~bbm101 › fall15 › lectures › w07-de… · Slides based on material prepared by E. Grimson, J. Guttagand C. Terman in MITx 6.00.1x

Pathsthroughaspecificationdef sqrt(x, eps):

“““Assumes x, eps floatsx >= 0eps > 0

returns res such thatx-eps <= res*res <= x+eps”””

• Pathsthroughspecification:– x=0– x>0

• Butclearlynotenough

22

Page 23: Development Strategies, Function Design › ~bbm101 › fall15 › lectures › w07-de… · Slides based on material prepared by E. Grimson, J. Guttagand C. Terman in MITx 6.00.1x

Pathsthroughaspecification

• Alsogoodtoconsiderboundarycases– Forlists:emptylist,singletonlist,manyelementlist– Fornumbers,verysmall,verylarge,“typical”

23

Page 24: Development Strategies, Function Design › ~bbm101 › fall15 › lectures › w07-de… · Slides based on material prepared by E. Grimson, J. Guttagand C. Terman in MITx 6.00.1x

Example• Foroursqrt case,trythese:– Firstfouraretypical• Perfectsquare• Irrationalsquareroot• Examplelessthan1

– Lastfivetestextremes• Ifbug,mightbecode,ormightbespec(e.g.don’ttrytofindrootifeps tiny)

x eps

0.0 0.0001

25.0 0.0001

.05 0.0001

2.0 0.0001

2.0 1.0/2.0**64.0

1.0/2.0**64.0 1.0/2.0**64.0

2.0**64.0 1.0/2.0**64.0

1.0/2.0**64.0 2.0**64.0

2.0**64.0 2.0**64.0

24

Page 25: Development Strategies, Function Design › ~bbm101 › fall15 › lectures › w07-de… · Slides based on material prepared by E. Grimson, J. Guttagand C. Terman in MITx 6.00.1x

Glass-boxTesting

• Usecodedirectlytoguidedesignoftestcases

• Glass-boxtestsuiteispath-completeifeverypotentialpaththroughthecodeistestedatleastonce– Notalwayspossibleifloopcanbeexercisedarbitrarytimes,orrecursioncanbearbitrarilydeep

• Evenpath-completesuitecanmissabug,dependingonchoiceofexamples

25

Page 26: Development Strategies, Function Design › ~bbm101 › fall15 › lectures › w07-de… · Slides based on material prepared by E. Grimson, J. Guttagand C. Terman in MITx 6.00.1x

Exampledef abs(x):

“““Assumes x is an intreturns x if x>=0 and –x otherwise”””

if x < -1:return –x

else:return x

• Testsuiteof{-2,2}willbepathcomplete• Butwillmissabs(-1) whichincorrectlyreturns-1– Testingboundarycasesandtypicalcaseswouldcatchthis{-2-1,2}

26

Page 27: Development Strategies, Function Design › ~bbm101 › fall15 › lectures › w07-de… · Slides based on material prepared by E. Grimson, J. Guttagand C. Terman in MITx 6.00.1x

Rulesofthumbforglass-boxtesting

• Exercisebothbranchesofallifstatements• Ensureeachexceptclauseisexecuted• Foreachforloop,havetestswhere:

– Loopisnotentered– Bodyofloopexecutedexactlyonce– Bodyofloopexecutedmorethanonce

• Foreachwhileloop,– Samecasesasforloops– Casesthatcatchallwaystoexitloop

• Forrecursivefunctions,testwithnorecursivecalls,onerecursivecall,andmorethanonerecursivecall

27

Page 28: Development Strategies, Function Design › ~bbm101 › fall15 › lectures › w07-de… · Slides based on material prepared by E. Grimson, J. Guttagand C. Terman in MITx 6.00.1x

Conductingtests

• Startwithunittesting– Checkthateachmodule(e.g.function)workscorrectly

• Movetointegrationtesting– Checkthatsystemaswholeworkscorrectly

• Cyclebetweenthesephases

28

Page 29: Development Strategies, Function Design › ~bbm101 › fall15 › lectures › w07-de… · Slides based on material prepared by E. Grimson, J. Guttagand C. Terman in MITx 6.00.1x

TestDriversandStubs

• Driversarecodethat– Setupenvironmentneededtoruncode– Invokecodeonpredefinedsequenceofinputs– Saveresults,and– Report

• Driverssimulatepartsofprogramthatuseunitbeingtested

• Stubssimulatepartsofprogramusedbyunitbeingtested– Allowyoutotestunitsthatdependonsoftwarenotyetwritten

29

Page 30: Development Strategies, Function Design › ~bbm101 › fall15 › lectures › w07-de… · Slides based on material prepared by E. Grimson, J. Guttagand C. Terman in MITx 6.00.1x

Goodtestingpractice

• Startwithunittesting• Movetointegrationtesting• Aftercodeiscorrected,besuretodoregressiontesting:– Checkthatprogramstillpassesallthetestsitusedtopass,i.e.,thatyourcodefixhasn’tbrokensomethingthatusedtowork

30

Page 31: Development Strategies, Function Design › ~bbm101 › fall15 › lectures › w07-de… · Slides based on material prepared by E. Grimson, J. Guttagand C. Terman in MITx 6.00.1x

“Most people, if you describe a train of events to them, will tell you what the resultwould be. They can put those events together in their minds, and argue from them thatsomething will come to pass. There are few people, however, who, if you told them aresult, would be able to evolve from their own inner consciousness what the steps werewhich led up to that result. This power is what I mean when I talk of reasoningbackwards, or analytically.“ -- Sherlock Holmes (A Study in Scarlet, by Sir Arthur Conan Doyle)

Debugging

31

Page 32: Development Strategies, Function Design › ~bbm101 › fall15 › lectures › w07-de… · Slides based on material prepared by E. Grimson, J. Guttagand C. Terman in MITx 6.00.1x

Runtimebugs

• Overtvs.covert:– Overthasanobviousmanifestation– codecrashesorrunsforever

– Coverthasnoobviousmanifestation– codereturnsavalue,whichmaybeincorrectbuthardtodetermine

• Persistentvs.intermittent:– Persistentoccurseverytimecodeisrun– Intermittentonlyoccurssometimes,evenifrunonsameinput

32

Page 33: Development Strategies, Function Design › ~bbm101 › fall15 › lectures › w07-de… · Slides based on material prepared by E. Grimson, J. Guttagand C. Terman in MITx 6.00.1x

Categoriesofbugs

• Overtandpersistent– Obvioustodetect– Goodprogrammersusedefensiveprogrammingtotrytoensurethatiferrorismade,bugwillfallintothiscategory

• Overtandintermittent– Morefrustrating,canbehardertodebug,butifconditionsthatpromptbugcanbereproduced,canbehandled

• Covert– Highlydangerous,asusersmaynotrealizeanswersareincorrectuntilcodehasbeenrunforlongperiod

33

Page 34: Development Strategies, Function Design › ~bbm101 › fall15 › lectures › w07-de… · Slides based on material prepared by E. Grimson, J. Guttagand C. Terman in MITx 6.00.1x

Debuggingskills

• Treatasasearchproblem:lookingforexplanationforincorrectbehavior– Studyavailabledata– bothcorrecttestcasesandincorrectones– Formanhypothesisconsistentwiththedata– Designandrunarepeatableexperimentwithpotentialtorefute

thehypothesis– Keeprecordofexperimentsperformed:usenarrowrangeof

hypotheses

34

Page 35: Development Strategies, Function Design › ~bbm101 › fall15 › lectures › w07-de… · Slides based on material prepared by E. Grimson, J. Guttagand C. Terman in MITx 6.00.1x

Debuggingassearch

• Wanttonarrowdownspaceofpossiblesourcesoferror• Designexperimentsthatexposeintermediatestagesof

computation(useprint statements!),anduseresultstofurthernarrowsearch

• Binarysearchcanbeapowerfultoolforthis

35

Page 36: Development Strategies, Function Design › ~bbm101 › fall15 › lectures › w07-de… · Slides based on material prepared by E. Grimson, J. Guttagand C. Terman in MITx 6.00.1x

Somepragmatichints

• Lookfortheusualsuspects• Askwhythecodeisdoingwhatitis,notwhyitisnot

doingwhatyouwant• Thebugisprobablynotwhereyouthinkitis– eliminate

locations• Explaintheproblemtosomeoneelse• Don’tbelievethedocumentation• Takeabreakandcomebacktothebuglater

36