Debugging

  1. Divide and conquer.
    • Do not write long code without testing. Write in inncrements. Test inbetween.


  2. Is my grammar correct?
    • No errors in the console


  3. Is the code/area running?
    • Place println() strategically. If they show up, that part of the code is running

      The follwing code does not do what I want: draw white lines when I press the mouse and otherwise draws black line.
      void setup() {
        size(480, 120);
        strokeWeight(4);
        stroke(0, 102);
      }
      
      void draw() {
        if(mousePressed = true){
          stroke(255);
        } else {
          stroke(0);
        }
        line(mouseX, mouseY, pmouseX, pmouseY);
      }
      
      Let's insert println() statements in key areas. I also print out the value of mousePressed by giving it as an argument to println()
      void setup() {
        size(480, 120);
        strokeWeight(4);
        stroke(0, 102);
      }
      
      void draw() {
        if(mousePressed = true){
          stroke(255);
          println("if clause:");
        } else {
          stroke(0);
          println("else clause:");
        }
        line(mouseX, mouseY, pmouseX, pmouseY);
      }
      
    From the print out, we learn that the else clause never runs


  4. Are my assumptions correct?
    • Use arguments to println() strategically.
    • If you are testing (n < 10), do you know what the value of n? -> println(n);
    • In this case, we focus on the value of mousePressed
      void setup() {
        size(480, 120);
        strokeWeight(4);
        stroke(0, 102);
      }
      
      void draw() {
        if(mousePressed = true){
          stroke(255);
          println("if clause:");
          println(mousePressed); 
        } else {
          stroke(0);
          println("else clause:");
          println(mousePressed); 
        }
        line(mouseX, mouseY, pmouseX, pmouseY);
      }
      


  5. Debug tool

    • Set breakpoints by clicking on the line number
    • When you run, processing will stop at the breakpoint
    • Use can see the variables in the variables panel
    • You can advance to the next breakpoint by selecting "Continue" or advance line by line through the code with "Step". Stepping only works within the scope of the current function being run.


      Processing 3 Debugger from Processing Foundation on Vimeo.




      souce code for the bouncing ball sketch: [bouncingball.pde]


  6. Instead of Sketch -> Run, try >Sketch -> Tweak
    • enabled with you are using setup() and draw() mode
    • Open: Examples -> Basic -> Math -> NoiseWave
Notes

Setup and Draw

Remeber the last thing you do in draw is persistent and will effect the beginning of the next frame (next iteration of the draw)







For a predictable behavior, I suggest you set everything up so that your assumptions are clear at the beginning of the draw function





Notes

For loops and Draw

A complete image will be drawn after the draw function completes. So if you use a for loop to do something in the draw function, the result of the for loop after it has completed will be drawn on the window.

You will not see the intermediate results of the for loops being animated.





Notes