Oct 31, 2023
Debug by coroutine: a 2-minute video sketch

Setup:

  • I have a graphical program.
  • Debugging it can get subtle. It's often not easy to tell, when tracing through it, whether a value at a specific point is right or wrong.
  • I'd like domain-specific debugging experiences.
  • I'd like to be able to create such experiences quickly on the fly without getting side-tracked from the problem I'm working on.

Here's a potential solution that slows time down without quite pausing like a breakpoint would.

audio/video; 2.5 minutes

Kind of a sequel to this post, if you squint.

Transcript:

I've been doing a lot more graphical and numerical programming lately, and I've been reminded of some of the unique challenges such programs pose.

It's hard to write automated tests for them.

Also, errors can be subtle. It's very difficult, when I'm tracing through a program, to stare at a line of code and a value of a variable and tell if it's right or wrong.

So I've been thinking about how I can improve the debugging experience for myself, now that I have this live programming environment. I feel like I'm not using it to its full potential.

Here's a very simple testbed program. I press a key, it performs some computation and it prints a value on the on the screen.

Let's look at the code for it.

As I said, it's printing a result to the screen and the result is computed on a keypress. And this is the computation over here.

The result is computed in a single shot. But if I insert a line that looks like this, now I can see the intermediate results render as the computation is performed.

This is kind of nice and of course, we don't have to be limited to textual rendering. It's like debug print but with greater expressivity.

However, there are some drawbacks. For one, I keep finding myself pushed when I program in this style, to not have local variables. Any intermediate result I might want to render wants to be a global variable.

That's why the result of the computation is a global. Instead of saving the result to the global in the caller, which makes the computation purely functional, I need to compute directly into the global.

I'm using coroutines under the hood and by writing directly to the globals, I don't have to think too hard about how to resume the coroutine. I can treat arbitrary computations the same way. Each coroutine is responsible for its side effects.

But again, globals all over the place, which makes me nervous.

Anyway, that's where I am. Thank you.

This post is part of my Freewheeling Apps Devlog.

Comments gratefully appreciated. Please send them to me by any method of your choice and I'll include them here.

archive
projects
writings
videos
subscribe
Mastodon
RSS (?)
twtxt (?)
Station (?)