Message Boards Message Boards

0
|
2317 Views
|
6 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Stepping through code, or following function calls

Posted 1 year ago

When working on a complex project, especially where one where the code is scattered across multiple pages and documents, it's really important to be able to follow exactly what is happening. Is there any way in Wolfram language to step through the code as it is executing line by line?

Additionally, is there any way to follow where the execution will jump? For example, if I am calling a function named Foo, is there an easy and quick way to jump to the document or part of the document where Foo is defined, so I can get to what will execute when Foo is called?

Again, the codebase I'm working with has a lot of jumping around like this, and it would be extremely helpful for me to understand what is happening if I could be able to do this.

POSTED BY: Shion Arita
6 Replies
Posted 1 year ago

Is there any way in Wolfram language to step through the code as it is executing line by line?

WL doesn't execute "line by line", so I don't think you can get exactly what you're looking for. However, look at http://reference.wolfram.com/language/guide/TuningAndDebugging.html and you'll probably find something helpful. Maybe TraceDialog?

is there an easy and quick way to jump to the document or part of the document where Foo is defined

There are two different interpretations here. First, if you're asking for a way to jump to the specific cell or text where the user input the code that "defined" Foo, then the only way I know is just regular search (Edit -> Find in the menus). But that user-input code is not the definition of Foo. The definition of Foo is what results from executing the user's code, and you find that result in the DownValues, OwnValues, UpValues, and SubValues for the symbol Foo. So, the second answer is to just evaluate DownValues[Foo] (or whichever definition you want). Or you can get summary with Information["Foo"].

POSTED BY: Eric Rimbey
Posted 1 year ago

For Information, one can use the short form ?function

POSTED BY: Hans Milton
Posted 1 year ago

Let me be a bit more specific with what I'm trying to do.

let's say that there's a function being called, let's call it MyFunction. so there will be something like

MyFunction[argument0, argument1, argument2]

Let's say that I want to figure out what's going to happen when this is executed.

Somewhere else in the codebase, which is comprised of hundreds of documents in an extremely complicated tree-like structure, there must be something like:

MyFunction[a_, b_, c_] := <some stuff>

How can I reliably get here, to see what is going to happen? Using search for the string MyFunction is not particularly useful, because in the codebase, MyFunction will be called up to hundreds or thousands of times, so it will take an extremely long time to find the right match of the string.

in other IDEs in other languages I've used, there is a feature where, if I want to do this, there's a command (such as clicking the middle mouse button on the function name), it will open the document that contains this, and focus on the relevant line. Is there any good way to do something similar here?

POSTED BY: Shion Arita
Posted 1 year ago

First off, I have no experience with using Wolfram Language in Eclipse or the Wolfram Workbench stuff or any other more sophisticated development environments. Those tools might have some fancy UI that will do what you want. I'm just using vanilla Mathematica, so keep that in mind.

Somewhere in the codebase we have:

MyFunction[a_, b_, c_] := {a, 2 b, 3 c}

When that particular chunk of code was executed, it stored a new item in DownValues in the "memory" of the environment. You don't need to go to the actual location in the code where the above line appears to retrieve the definition for MyFunction. You can just execute this:

DownValues[MyFunction]

Which will give you this:

{HoldPattern[MyFunction[a_, b_, c_]] :> {a, 2 b, 3 c}}

I think this addresses your concern of:

I want to figure out what's going to happen

You can also execute this:

Information["MyFunction"]

So, if you're insisting on a UI feature that will navigate you to the particular instance of the text MyFunction that was used in a SetDelayed expression, then I don't know how to do that. But if you want to know "what's going to happen", i.e. if you want to see the definition the system has "remembered" for MyFunction, then there are ways to do that directly (as I've shown above) without actually navigating anywhere.

POSTED BY: Eric Rimbey
Posted 1 year ago

Thanks, this does help somewhat! it does let me see what's inside these functions. It would be nice to have a navigation feature, as, in figuring out this codebase, the downvalues go multiple levels deep and often contain essentially very verbose tables of data, so it would be nice to be able to find it where it was originally written. I will look into if there's a way to do this in my IDE.

POSTED BY: Shion Arita

Not all of what you are asking, but there is a really nice SymbolDependencyGraph resource function that will show all custom functions used in your code and their relation.

POSTED BY: Gustavo Delfino
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract