12.07.2015 Views

Think Python - Denison University

Think Python - Denison University

Think Python - Denison University

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

13.11. Glossary 133reading: Examineyourcode,readitbacktoyourself,andcheckthatitsayswhatyoumeanttosay.running: Experiment by making changes and running different versions. Often if you display theright thing at the right place in the program, the problem becomes obvious, but sometimesyou have tospend sometimetobuild scaffolding.ruminating: Take some time to think! What kind of error is it: syntax, runtime, semantic? Whatinformation can you get from the error messages, or from the output of the program? Whatkind of error could cause the problem you’re seeing? What did you change last, before theproblem appeared?retreating: At some point, the best thing to do is back off, undoing recent changes, until you getback toaprogram that works and that you understand. Then you can startrebuilding.Beginning programmers sometimes get stuck on one of these activities and forget the others. Eachactivity comes withitsown failuremode.For example, reading your code might help if the problem is a typographical error, but not if theproblem is a conceptual misunderstanding. If you don’t understand what your program does, youcan read it100 times and never seethe error,because theerror isinyour head.Running experiments can help, especially if you run small, simple tests. But if you run experimentswithout thinking or reading your code, you might fall into a pattern I call “random walkprogramming,” which is the process of making random changes until the program does the rightthing. Needless tosay, random walk programming can take along time.You have totake time tothink. Debugging islike an experimental science. You should have at leastonehypothesisaboutwhattheproblemis. Iftherearetwoormorepossibilities,trytothinkofatestthat would eliminate one of them.Takingabreakhelpswiththethinking. Sodoestalking. Ifyouexplaintheproblemtosomeoneelse(oreven yourself),you willsometimes find the answer before you finish asking thequestion.But even the best debugging techniques will fail if there are too many errors, or if the code you aretrying to fix is too big and complicated. Sometimes the best option is to retreat, simplifying theprogram until you get tosomething that works and that you understand.Beginningprogrammersareoftenreluctanttoretreatbecausetheycan’tstandtodeletealineofcode(even if it’s wrong). If it makes you feel better, copy your program into another file before you startstrippingitdown. Then you can paste thepieces back inalittlebitat atime.Findingahardbugrequiresreading,running,ruminating,andsometimesretreating. Ifyougetstuckon one of theseactivities, trytheothers.13.11 Glossarydeterministic: Pertaining to a program that does the same thing each time it runs, given the sameinputs.pseudorandom: Pertaining to a sequence of numbers that appear to be random, but are generatedby adeterministic program.default value: The value given toan optional parameter ifno argument isprovided.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!