DOM events and mutable values — idiomatic or code-smell?

I started writing some notebooks about interacting with SVG elements and since I wanted to break them into small, readable cells, I turned to mutable for storing the outcomes of mouse events. Then, little bits of imperative code attach these event listeners to SVG elements (obtained via querySelector) and update their attributes based on the mutable values.

Before I continue with this pattern, I wanted to ask if there’s a more idiomatic alternative to using mutable for such use-cases.

(Similar to what @kelleyvanevert asked here.)

1 Like

Hi Dan,

Mike may have a different suggestion for you — but in general, I think that you’re quite right that the judicious use of mutable can help break up things that would otherwise necessarily be crammed into a single large cell … or that would otherwise update more often than they actually need to.

In fact, flexibly breaking up large cells into smaller, more readable ones was one of the initial motivations for adding the mutable keyword.

2 Likes

Thanks, Jeremy! That makes sense.