Best way to debug? (Opinions Welcome)

edited June 2017 in Using Processing

Hello all, I have been wondering for quite some time now, what is an efficient way to debut your program? I have ran into some bugs that have caused major problems and take days to figure out, just sitting there thinking about the problem and your code. Is there a better way to find the problem? I have tried to use the debug tool, but I don't fully understand it, and i'm not sure how useful it can be. If you have a strategy for debugging, feel free to tell me and the viewers of this thread what it is!

Thanks in advance!

Tagged:

Answers

  • edited June 2017 Answer ✓

    IMO, best way to debug is to always hit CTRL+T in order to have my code formatted all the time! $-)

    Obviously, setting breakpoints and checking variables' current value using the debugger is the pro way to do it.

    But I admit I never do it for small sketches. I end up spreading println() everywhere! X_X

    Another interesting approach is to use assert keyword in place of println(). *-:)
    Like breakpoints, it has the advantage of not filling up the console w/ values @ 60 FPS. O:-)

    It works like this. Let's say you've got some variable called x which should never be less than 0 & greater than width at some point in your code.

    Thus you can use assert like this: assert x >= 0 & x < width: "x is " + x;

    If it happens x is currently -5 when the assert is checked, the sketch crashes and shows at the bottom red bar: Assertion Error: x is -5.

    So you know that somehow variable x is outside its supposed range of values! \m/

    Take a look at this example using assert inside function move() below: B-)

    // forum.Processing.org/two/discussion/22866/best-way-to-debug-opinions-welcome
    // GoToLoop (2017-Jun-01)
    
    static final int VX = 3;
    int x, d;
    
    void setup() {
      size(800, 200);
      smooth(3);
    
      fill(#FFFF00);
      stroke(#FF0000);
      strokeWeight(2.5);
    
      d = height >> 1;
      x = d;
    }
    
    void draw() {
      background(#0000FF);
      move(VX);
      ellipse(x, d, d, d);
    }
    
    void move(final int n) {
      x += n;
    
    assert x >= d>>1 & x < width - d/2: 
      "x is " + x;
    }
    
  • edited June 2017

    Thanks for the advice this helps so much! I was able to figure out the flaws in my sketch!

  • edited June 2017 Answer ✓

    The best way to debug is to not have to debug. If you develop your code in steps, making sure it works after each step, then you can easily narrow down issues that do arise to sections of code that were added since you last checked it still worked. This is why I always suggest people start with a blank sketch, and gradually add working elements to it.

  • Answer ✓

    When the sketch used to run and now it doesn't, the last thing/ things you changed is / are responsible

    So just look at the last bit

    Also run regularly to make sure it still works

Sign In or Register to comment.