22
$\begingroup$

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?

Is Wolfram Workbench the only/best solution?

$\endgroup$
3

2 Answers 2

22
$\begingroup$

I think you got 4 choices:

  1. Workbench. Probably the most useful of the debuggers.
  2. Mathematica has a small debugger: Evaluation -> Debugger
  3. There is DebugTrace from David (perhaps there are other packages)
  4. Use Print, Trace(Scan)[], etc type of functions.
$\endgroup$
1
  • $\begingroup$ SO Link on lower side of page there is mention of using Mathematica's built-in debugger. $\endgroup$ Apr 12, 2013 at 7:31
32
$\begingroup$

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:

enter image description here

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:

enter image description here

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.

$\endgroup$
5
  • 7
    $\begingroup$ There is no need to "Excuse the necromancy" -- in fact there is a badge for posting late-but-good answers! $\endgroup$
    – Mr.Wizard
    Apr 13, 2013 at 18:41
  • 3
    $\begingroup$ Mind. Blown. How exactly was this discovered?? $\endgroup$
    – ibeatty
    Nov 1, 2017 at 16:50
  • 2
    $\begingroup$ I'd vote this up for "best hack of 2013" if there were such an award :) $\endgroup$
    – Reb.Cabin
    Mar 11, 2019 at 16:31
  • $\begingroup$ Hi, @amr. Is it still working now? What do you mean by "highlight the Dynamic[x] and press Ctrl+Shift+Enter"? I can not see any amazing thing happen except that Mathematica just hangs there without any output $\endgroup$
    – matheorem
    Oct 31, 2021 at 1:48
  • $\begingroup$ @matheorem I also had that issue. Here are some extra details: Copy paste the While code into your notebook. Highlight Dynamic[x] inside of the comment and press Ctrl+Shift+Enter. On my laptop the Dynamic[x] changes to a number. Then right click on the cell bracket and choose "Evaluate Cells" in the menu. Then be amazed then abort with Alt+. for example. $\endgroup$ Nov 25, 2022 at 18:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.