We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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!
Answers
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 thanwidth
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 theassert
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-)Thanks for the advice this helps so much! I was able to figure out the flaws in my sketch!
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.
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