06.01.2013 Views

Learning Processing: A Beginner's Guide to Programming Images ...

Learning Processing: A Beginner's Guide to Programming Images ...

Learning Processing: A Beginner's Guide to Programming Images ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

11<br />

Debugging<br />

“ Th e diff erence between the right word and the almost right word<br />

is the diff erence between lightning and a lightning bug. ”<br />

—Mark Twain<br />

“ L’appétit vient en mangeant. ”<br />

— Th e French<br />

Bugs happen.<br />

Debugging 191<br />

Five minutes ago, your code was working perfectly and you swear, all you did was change the color of<br />

some object! But now, when the spaceship hits the asteroid, it doesn’t spin any more. But it was <strong>to</strong>tally<br />

spinning fi ve minutes ago! And your friend agrees: “ Yeah, I saw it spin. Th at was cool. ” Th e rotate( )<br />

function is there. What happened? It should work. Th is makes no sense at all! Th e computer is probably<br />

broken. Yeah. Yeah . It is defi nitely the computer’s fault.<br />

No matter how much time you spend studying computer science, reading programming books, or playing<br />

audio recordings of code while you sleep hoping it will soak in that way, there is just no way <strong>to</strong> avoid<br />

getting stuck on a bug.<br />

It can be really frustrating.<br />

A bug is any defect in a program. Sometimes it is obvious that you have a bug; your sketch will quit (or<br />

not run at all) and display an error in the message console. Th ese types of bugs can be caused by simple<br />

typos, variables that were never initialized, looking for an element in an array that doesn’t exist, and so on.<br />

For some additional clues on “ error ” bugs, take a look at the Appendix on errors at the end of this book.<br />

Bugs can also be more sinister and mysterious. If your <strong>Processing</strong> sketch does not function the way you<br />

intended it <strong>to</strong>, you have a bug. In this case, your sketch might run without producing any errors in the<br />

message console. Finding this type of bug is more diffi cult since it will not necessarily be as obvious<br />

where <strong>to</strong> start looking in the code.<br />

In this chapter, we will discuss a few basic strategies for fi xing bugs ( “ debugging ” ) with <strong>Processing</strong> .<br />

11.1<br />

Tip #1: Take a break.<br />

Seriously. Go away from your computer. Sleep. Go jogging. Eat an orange. Play scrabble. Do something<br />

other than working on your code. I can’t tell you how many times I have stared at my code for hours<br />

unable <strong>to</strong> fi x it, only <strong>to</strong> wake up the next morning and solve the problem in fi ve minutes .<br />

11.2<br />

Tip #2: Get another human being involved.<br />

Talk through the problem with a friend. Th e process of showing your code <strong>to</strong> another programmer (or<br />

nonprogrammer, even) and walking through the logic out loud will often reveal the bug. In many cases, it<br />

is something obvious that you did not see because you know your code so well. Th e process of explaining<br />

it <strong>to</strong> someone else, however, forces you <strong>to</strong> go through the code more slowly. If you do not have a friend<br />

nearby, you can also do this out loud <strong>to</strong> yourself. Yes, you will look silly, but it helps.

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

Saved successfully!

Ooh no, something went wrong!