In various integrated development environments, such as Microsoft Visual Studio, there is an integrated debugger where you can step through code sections, examine the state of variables and figure out where things are going wrong. With more complex Mathematica programs a debugger would be an extremely valuable asset. I was wondering what people use to debug Mathematica programs?

I found a curious style of debugging that I call "Epicsauce Debugging Level 2."

First you type this:

x = 0;
While[True,
Pause[1];
(*Dynamic[x]*) x++
];

Then you highlight the Dynamic[x] and press Ctrl+Shift+Enter. Then you execute the cell and BE AMAZED!!! It's quite hilarious, but I wonder how useful it would be for more serious code.

I've already been using this for some minor things, like to check that a value is actually being modified. The fact that you can place it right by the variable itself is conceptually significant, I think.

Edit: This is actually pretty cool. Here's one practical use. You know when you compose a bunch of stuff, like a graphics expression:

And then your script starts getting a little complicated, and you aren't even sure if the issue is with one of your small components or it's an issue with the final composition? Well you can do this:

Remember that's a live, rotatable object in the comment. One that auto-updates. The nice thing here is that you can debug individual components without breaking out of the flow of your program as a whole.

Edit2: You can highlight the line where the cuboid is being defined and hit Ctrl+Shift+Enter to only evaluate that line and not the whole expression, and see your changes in the comment. It appears that expressions that return Null (such as those ending with ;) aren't replaced when you Ctrl+Shift+Enter on them. Also remember that Ctrl+. will let you quickly select whole expressions.

Mathematica is a registered trademark of Wolfram Research, Inc. While the mark is used herein with the limited permission of Wolfram Research, Stack Exchange and this site disclaim all affiliation therewith.