Refactor this for me

In a coding session today, I ran into the following interaction with a Large Language Model. I had something like this:

The time variables need to change their value based on the query, but there’s also an extra source of state updates, so I wanted to refactor it to using local state and adding an effect. I entered a pairing session with Claude 3.5 Sonnet and this is what happened:

I asked Claude to do some changes to the startTime code. Then, I just requested to “apply the same changes to endTime” — I didn’t provide further context.

The changes aren’t correct because it’s using the same function getTimeFromValue to calculate endTime. I can fix that easily, or perhaps I should have provided better prompts. There’s also the question if the end result is better than the previous state, but that’s the programmer’s fault, not the machine’s. Whether this has produced better or correct code is not the point. The question I’m interested in is different: do LLMs to edit code improve conventional editing tools?

By any measure, this was a small refactoring. But just as small as it was, it hints at how they do. They give you access to many more code actions than any editor can bundle, and chaining them is a seamless process that doesn’t take you out of the flow. It’s also trivial to apply the same chain of actions to a different piece of code. Right after finishing this refactoring, I had a flashback: my younger self was using emacs macros to repeat some simple edits across a few dozens of lines — it was a more limited experience.

It feels like, with time, it’d go beyond incremental improvements and refactorings will be expressed in a higher-level vocabulary than conventional editing tools.


Comments

3 responses to “Refactor this for me”

  1. These are strange times in technology, simultaneously interesting and concerning. I love the juxtaposition here:

    > my younger self was using emacs macros to repeat some simple edits […;] with time, [LLM use] would go beyond incremental improvements, and refactorings will be expressed in a higher-level vocabulary than conventional editing tools.

    In my case, it’s Vim macros, not Emacs, but the point is the same. One develops a skillset to speed up the work, a skillset based on determinism: those keystrokes, recording a series of editor commands, ultimately yield a magical cascade of transformations, and this cascade is deterministic, for good and bad. LLMs are the opposite: fuzzy, sometimes making up for our lack of clarity, sometimes annoyingly taking the wrong liberties.

    It’s expected that they’ll become better at this, of course. I hope that in the process they also become fairer and lighter, but that’s a whole other debate, of course. The ergonomics — everything built around the LLM — is what will make the real difference, too, I think.

    Your interesting point about a possible emergence of a high-level vocabulary fits nicely in the question of ergonomics. It seems conceivable that we’ll able to actively but organically build this language with a lang model — and share it across an organisation or large project, for example, thereby promoting consistency and visibility.

    In the meantime, I still find that I save time and/or flow by using macros… but I acknowledge that this is a dying art that speaks to this poor romantic.

    1. > recording a series of editor commands, ultimately yield a magical cascade of transformations, and this cascade is deterministic, for good and bad. LLMs are the opposite: fuzzy, sometimes making up for our lack of clarity, sometimes annoyingly taking the wrong liberties.

      I think this is a great point that I can illustrate with an example.

      The video in the post was recorded _after_ having used the LLM, so I had to revert the code changes to the previous state and run the commands again. I forgot the exact words I had used, so the first two attempts at recording what I had done before weren’t successful because the LLM would do a different thing.

      LLMs, by their nature, are stochastic. The promise is that they’ll be right most of the time — statistically speaking. Their current state is still messier than it needs to be for these tools to be widely adopted in production-ready code. It also depends a lot on what API you’re working with: I had very good results with established APIs and astonishingly bad ones with new, private, project-specific APIs.

      I think framing them as “tools that can offer higher-level editing mechanics” rather than “tools that will replace programmers” offers more realistic expectations about them.

      1. > I think framing them as “tools that can offer higher-level editing mechanics” rather than “tools that will replace programmers” offers more realistic expectations about them.

        Nice maxim. 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *