3
$\begingroup$

I am a novice so imagine my confusion with Mathematica's very poor error and trace messages. Debugging should be an easy thing to do even for beginning programmers but not with Mathematica. What are some tricks/tips or tools you use for debugging especially for beginners?

$\endgroup$
2
1
$\begingroup$

One of the easiest debugging methods all beginners do is drop Print[var] statements throughout code. Sometimes it is difficult to interpret the output when many Print statements are used so beginners often do something like Print[Row[{"x: ",x}]] rewriting the variable name twice. However this is unnecessary if you know how to access a variables symbol name. For that reason I created a simple short function that only requires writing the symbol once as an argument.

SetAttributes[{prt}, HoldFirst];
objectName = Function[Null, SymbolName[Unevaluated[#]], {HoldFirst}];
objectName::usage = 
  "objectName@# returns Unevaluated shortened SymbolName.";
prt[symbol_] := Print[Row[{objectName[symbol], symbol}, " \[Rule] "]];

it is used like this...

x=123;
prt[x]

should output...

x -> 123

if x is nested inside a function, block, module, etc. and it doesn't work you may need to add fnname to the SetAttributes[{prt,fnname},HoldFirst] in the first line above.

$\endgroup$
6
  • 4
    $\begingroup$ Yes, but Print has many disadvantages also. The main is that it clutters the code, so the code becomes harder to read with many call to print in the middle of the algorithm. Mathematica needs an easy to use debugger. In Matlab, I never needed print() to debug. If there is a problem, I tell the debugger to stop at an error, or break point at some function and very quickly find the error. In Mathematica debugging is much much harder specially as the program getting larger. $\endgroup$ – Nasser Jun 25 '20 at 3:56
  • 2
    $\begingroup$ Print also has the disadvantage that it returns Null. I like to add Echo instead, which returns its input as output as well as printing it out, so it acts as a convenient “pass-through” that can typically be added almost anywhere. You can also label its output using the two argument syntax of Echo. $\endgroup$ – MarcoB Jun 25 '20 at 4:12
  • 1
    $\begingroup$ ... but I agree with @Nasser ’s sentiment: a decent debugger would be very nice indeed. $\endgroup$ – MarcoB Jun 25 '20 at 4:14
  • 2
    $\begingroup$ statements are only temporary and the minute you think the code is OK and you remove them, you'll find a new problem, and then you end up wasting time typing them all over again. I've been there and done that. And if you put too few print statements, then you find you need more since the ones you have do not give enough information. Overall, using print for debugging is so 1980's these days. Notice that I am not talking about logging here. That is different thing. Logging is very important, and used for example by web servers all the time and that serve different purpose. $\endgroup$ – Nasser Jun 25 '20 at 9:33
  • 2
    $\begingroup$ The way I do things now, if have print statements, that I can turn on/off by a flag. But keep them there. But as I said, the code becomes hard to read, since there are many of them, and I do not know when I might need them again so I keep them but turned off when not needed to see. For small program, debugging is easy. I am talking about large programs here, with 10's of functions or more and and complex algorithms in many places. When the program gets over about 1,000-1,500 lines is the tipping point for me when I might need a good debugger. $\endgroup$ – Nasser Jun 25 '20 at 9:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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