Thursday, June 23, 2011

Debugging in the P5 IDE

Recently I've been using the processing IDE to build wondrous things. It works reasonably well, having at least some of the features we've come to expect from modern IDEs, right until you try to debug a program in it. If you happen to have a missing parenthesis, curly brace, semicolon, etc. in your program the IDE will helpfully point you to a random file (chances of the error actually being in this file are pretty low) and complain that it encountered an unexpected symbol such as a }, ;, or ).

It is important to realize at this point that the }, ;, or ) is not the actual problem with your program, it's just the first thing that the parser encountered after the error. So all you have to do is find a } in your program (can't be too many of those, right?) and read backwards from it until you find your error.

Then, once you've re-read all the code in your program and didn't find the problem, you may come to the internet for help. Here is what you should do instead to find syntax errors in these programs.

Unlike other IDEs processing only deals with one error at a time. If it finds one it will immediately stop and tell you what it is, and syntax errors always take precedence over all other errors. How can we use this fact to help us find the bug?

Comment out half your code. Seriously. For half of your source code files, do a CTRL-A and CTRL-/. Then try to run your program. If the syntax error was in the files you just commented out then you will receive some other error which will not be a syntax error! Proceed to uncomment half of your commented-out code files and run again to find out which quarter of your source files the error is in. Repeat this process until you've narrowed it down to a single file. Then comment out half the methods in the file, and then a quarter, etc until you've narrowed it down to one method that you can scrutinize for the error.
 
If the error wasn't in the half of the files that you commented out then un-comment them and comment the other half and take it from there.

Note that this process becomes more complicated if you have multiple syntax errors, in which case you may need to comment the entire thing and un-comment file by file.

Here's another free tip: Don't re-declare class variables when you are initializing them in your constructor or you will be unable to access them later.

No comments:

Post a Comment