cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Help us improve the PTC Community by taking this short Community Survey! X

Better than "return" — debugging function for ridiculously long programs

SPauliszyn
13-Aquamarine

Better than "return" — debugging function for ridiculously long programs

I made a couple simple functions that I would like to share that helps with debugging of long Mathcad Prime (circa v9)  programs.  (file attached)

 

SPauliszyn_0-1695737701917.png

I was making a VERY LONG convoluted program that called several sub-functions (in order to split it up).  But it still had bugs by the time it was fully grown (don't we all;).

 

I made this function so that I could locate the errors before it happened.  It is basically a logger that lets the program continue.  It is better than a "return" statement in a few ways:

 

  • Since there is no 'remarks' statement in programing, this can also be a substitute—they do not need to be removed (except for speeding up long iteration functions).
  • You do not have to delete the "debug" functions in order to keep the program running and running to a proper conclusion whereas "return" statement must be added/removed every time it is needed.
  • "debug" functions do not screw up the output if it is in a sub-function
  • You can display the debug data wherever you need in your sheet, also as a confirmation that your function is working fine

Notes:

  • if you have "Auto Calculation" on, press F9 to make sure debug("") is refreshed.  The debug function does not dependant on your program so Mathcad will not refresh its output automatically.
  • When Mathcad Prime adds a function to delete a file, a command debug("delete") can be added.  Otherwise the file is saved in the same folder as the sheet it's in.

 

 

 

 

P.S.  Please forgive me if there is another post like this but there are lots to search though and I am not sure I used the right keywords.  There are a few posts about writing data to file but this is a complete function you can cut and paste into your sheet (or include).

6 REPLIES 6
StuartBruff
23-Emerald III
(To:SPauliszyn)

Neat.  

 

Here's another method I've found helpful in hunting down the bugs once I know where they live ...

 

2024 10 17 C.png

 

Mathcad Prime 10

 

Stuart

Hi @StuartBruff 

 

Using "Try/On Error" is another way to handle errors.  I mostly use them when I can expect an error as a normal course of calculations.  I use the debug method when I am trying to find the error and I do not want to rewrite/reorganize the structure by adding Try statements—it is much easier to move the debug statement around.

StuartBruff
23-Emerald III
(To:SPauliszyn)


@SPauliszyn wrote:

Hi @StuartBruff 

 

Using "Try/On Error" is another way to handle errors.  I mostly use them when I can expect an error as a normal course of calculations.  I use the debug method when I am trying to find the error and I do not want to rewrite/reorganize the structure by adding Try statements—it is much easier to move the debug statement around.



I agree.

 

As I tend to keep many Mathcad worksheets within a single folder and often work on several worksheets in parallel, I'd probably add a file argument to your function to allow simultaneous use of different debug files.

 

A more generic problem, harking back to Mathsoft days, is the minimal debugging information available to a programmer/user.   Mathcad 15 had, IMO, better facilities but was still wanting. It is challenging to know which part of a program/function has raised an error; the error messages don't chain, and the errors provide little contextual information - that's why I return the values in force at the point of error.  The inability to query an error message within a worksheet is also limiting; for example, any error message is only available in tiny point (hard to read on a 4K monitor at 80% zoom), can't be copied, and can't be retrieved to a variable.

 

I'd also like a functional form of try-else.  I dislike programs taking up unnecessary vertical space; a try(expression, error_response) form would allow, IMO, for a more readable and compact program on many occasions.

 

Stuart

I see.

StuartBruff
23-Emerald III
(To:SPauliszyn)


@SPauliszyn wrote:

I see. Sorry this “debug” post doesn’t work out for you.

 


I don't think I said that; quite the opposite.  Both the post and your debug function are certainly relevant and helpful.  I was referring to Mathcad debugging in general, which I had hoped was clear from the context.

 

Stuart

That will work.  Actually I use a more robust debug function that has an index parameter.  It can be a string (tag), or an index.  The data is kept in a vertical matrix in the single file so you don't clutter up your folder.   

Announcements

Top Tags