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.

192 <strong>Learning</strong> <strong>Processing</strong><br />

11.3<br />

Tip #3: Simplify<br />

Simplify. Simplify! SIMPLIFY!<br />

In Chapter 10, we focused on the process of incremental development. Th e more you develop your<br />

projects step-by-step, in small, easy <strong>to</strong> manage pieces, the fewer errors and bugs you will end up having.<br />

Of course, there is no way <strong>to</strong> avoid problems completely, so when they do occur, the philosophy of<br />

incremental development can also be applied <strong>to</strong> debugging. Instead of building the code up piece by<br />

piece, debugging involves taking the code apart piece by piece.<br />

One way <strong>to</strong> accomplish this is <strong>to</strong> comment out large chunks of code in order <strong>to</strong> isolate a particular<br />

section. Following is the main tab of an example sketch. Th e sketch has an array of Snake objects,<br />

a But<strong>to</strong>n object, and an Apple object. (Th e code for the classes is not included.) Let’s assume that<br />

everything about the sketch is working properly, except that the Apple is invisible. To debug the problem,<br />

everything is commented out except for the few lines of code that deal directly with initializing and<br />

displaying the Apple object.<br />

// Snake[] snakes = new Snake[100];<br />

// But<strong>to</strong>n but<strong>to</strong>n;<br />

Apple apple;<br />

void setup() {<br />

size(200,200);<br />

apple = new Apple();<br />

/*for (int i = 0; i < snakes.length; i ++ ) {<br />

snakes[i] = new Snake();<br />

}<br />

but<strong>to</strong>n = new But<strong>to</strong>n(10,10,100,50);<br />

smooth();*/<br />

}<br />

void draw() {<br />

background(0);<br />

apple.display();<br />

// apple.move();<br />

}<br />

/*for (int i = 0; i < snakes. length; i + + ) {<br />

snakes[i].display();<br />

snakes[i].slither();<br />

snakes[i].eat(apple);<br />

}<br />

if (but<strong>to</strong>n.pressed()) {<br />

applet.restart();<br />

} */<br />

/*void mousePressed() {<br />

but<strong>to</strong>n.click(mouseX,mouseY);<br />

} */<br />

Only the code that makes the<br />

Apple object and displays the<br />

object is left uncommented. This<br />

way, we can be sure that none of<br />

the other code is the cause of the<br />

issue.<br />

Large blocks of code can be<br />

commented out between /*and*/<br />

/*All of this is<br />

commented out */

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

Saved successfully!

Ooh no, something went wrong!