Top
Best
New

Posted by javaeeeee 5 hours ago

The Economic Benefit of Refactoring(martinfowler.com)
149 points | 70 comments
Viliam1234 3 hours ago|
I find it funny how the best practices for programmers, ignored in most IT companies, get reinvented as the best practices for AIs.

Boring: The documentation should be in code, not in external Word documents uploaded to the company SharePoint server.

Exciting: The documentation for the AI should be in code, not in external Word documents uploaded to the company SharePoint server.

Boring: You should give your developers the big picture of the project, not just micromanage them using Jira tasks.

Exciting: You should give your AI the big picture of the project in CLAUDE.md, not just micromanage it using prompts.

Boring: Refactoring makes your developers more productive in long term.

Exciting: Refactoring makes your AI more productive in long term.

hungryhobbit 31 minutes ago||
To be clear, the guy who wrote this article is Martin Fowler. Fowler was the author of a book literally titled "Refactoring", which he wrote over 20 years ago (it didn't invent the term "refactoring", but it's widely credited as popularizing it).

In other words, this is not some new AI-bandwagon-jumping blogger trying to rebrand old practices as something new ... this is the guy who coined the very term "refactoring"! He's not pretending what's old is new, he's arguing that best practices from 20+ years ago remain relevant (and showing receipts).

FartinMowler 5 minutes ago|||
My namesake, er, not quite ;).
spot5010 23 minutes ago||||
The website is martinfowler.com, but the article seems to be written by Giles Edwards-Alexander.
hungryhobbit 8 minutes ago||
Oh, I stand corrected (thanks for the catch)!
jdlshore 22 minutes ago|||
It’s an easy mistake to make, given the name of the website, but Fowler frequently hosts guest authors. This article was written by Giles Edwards-Alexander, ThoughtWorks CTO for EMEA.
Ericson2314 2 hours ago|||
No this isn't mindless reinventing --- this is finally having clear empiric evidence for something that we knew the entire time. This is a huge relief!

Next up is demonstrating the AI is more productive with better programming languages.

tikhonj 2 hours ago|||
I've already seen at least one promising experiment about how static checking helps LLMs: https://arxiv.org/abs/2606.01522

Key parts of the abstract:

> This raises a question the programming-language community has not previously had reason to ask: should error-message detail be calibrated differently for AI agents than for humans?

> We investigate this question through a controlled experiment using Shplait, an ML-style statically typed language. We construct a suite of programs containing a single deliberate type error each, and measure how often an AI agent repairs them under ablation: a detailed error context using the unification stack; a proximate error location; a minimal type error; and a dynamic (test suite) error only. An automated oracle uses a test suite to classify each repair attempt as a type error, semantically incorrect, or semantically correct.

> We find concrete evidence that more detailed error messages generally improve an agent's ability to fix type errors. We also find that the presence of a type system appears to help more than only test suite failure reports.

kagevf 1 hour ago|||
> Next up is demonstrating the AI is more productive with better programming languages.

That actually would be pretty exciting!

But, wouldn't AI be biased toward more popular languages, since those will by nature of their popularity provide more sources for training material?

Even still, the AI could demonstrate which algorithms or maybe patterns and techniques are more productive, in the context of the popular languages.

bobthepanda 57 minutes ago||
That’s not necessarily a bad thing.

As an example, 110v American outlet plugs are almost certainly not the most efficient way to power devices and appliances; but, because they are so standard and are good enough, using them massively eases the burden of making, buying and selling powered appliances in the US. Similar story with USB C.

mkehrt 32 minutes ago||
Outlets benefit from standards, however. Every device (well kind of) uses the same few plug types, and a different plug would be a disadvantage.

On the other hand, having a bunch of different languages is an advantage. People can pick the best tool for the job, and experiment with new language features.

fithisux 52 minutes ago||
Exactly
whats_a_quasar 3 hours ago||
This is such a nice piece, this is how people should write about AI. Specific, grounded to how the tools are actually being used, and quantitative. There is so much bad AI commentary that is incredibly vague, divorced from any actual use cases, or written by people who don't actually use the tools. It is good to see a critique that is "here is a thing AI is bad at and measurements to show it" rather than gesturing "here is why I think AI is problematic for society."

It is rather different but another piece of research I liked for the same reason was this report that interviewed Boko Haram members about how they used AI to assist terrorism. You get these interminable online debates that are so unproductive and reporting that is specific is such a breath of fresh air.

https://casp.ac/reports/ai-enabled-terrorism

jimbokun 1 hour ago|
Yes, more like this please, universe!
gga 1 hour ago||
I’ll try to keep it up.
firasd 4 hours ago||
I think this is one of those things where a human in the loop is indispensable

An agentic refactoring pass does make sense cause one LLM reviewing work can spot things the ‘generator’ LLM missed while focused on the initial task output

But can the reviewer agent ever actually have in mind what this project actually is? And how the code all comes together to do the work involved? In other words what parts of the code are redundant or can be made more elegant

Asking coding agents to refactor your codebase is maybe like asking trauma surgeons to increase your exercise capacity. The agents are gonna need a really holistic POV to do this properly

I guess part of my point is that just splitting big files into multiple files is only refactoring in a superficial sense without having a theory of what code belongs together and what can be extracted into utility functions etc. Is splitting files actually like decomposing factors or is it like splitting a larger number into smaller numbers that still eventually get added together

A good example of what I mean is that agents often don’t ~actually~ understand the whole system anyway. They might implement a system to store and calculate something that is already being fetched via API. Humans often have a dual perspective — a holistic sense of the project and (when applying our mind to a task) a precise scalpel: ‘oh if we just look at this this JSON it has a key with this data already’

zahlman 3 hours ago||
> Asking coding agents to refactor your codebase is maybe like asking trauma surgeons to increase your exercise capacity. The agents are gonna need a really holistic POV to do this properly

If you ask it to implement a specific kind of refactoring over a specific section of code it seems that current LLMs can do just fine. Even things as complex as "use `functools.partial` to implement the Command pattern here, rather than dataclasses".

> Is splitting files actually like decomposing factors or is it like splitting a larger number into smaller numbers that still eventually get added together

The file boundaries represent logical subsystem boundaries in the code, making it easier to reason about. The training data abundantly represents the idea of treating the contents of another file as opaque by default while other functionality in the same file can be used freely. I think it's reasonable to assume that there's something objective about the benefits that humans get from this, and it isn't just a consequence of how human cognition works.

j45 4 hours ago|||
I was able to largely refactor something that was going to take months in a week or so by being the narrating human in the loop.

It was helped by having refactoring experience and approaches to codebases by others, and in my case, being the original architect and being able to speak to the original and current intents, where needed.

This was using a less common, but capable and easy language for the LLM without a ton of dependancy brittleness to manage.. once the effort to remove javascript/python bias was in place, it became so powerful that once the lightbulb went on, it really got cruising.

The project was playing in the world of JSR-223 languages, where you could script in many popular languages, but it all got to run in the JVM, which was an environmental requirement.

https://en.wikipedia.org/wiki/Scripting_for_the_Java_Platfor...

zuzululu 4 hours ago||
Your take is outdated. Agents are excellent at refactoring now. If you told me what you wrote during an interview, that would be grounds to not continue.

It's very important that you are using coding agents with the latest frontier models and know exactly what it can't and can't do if you want to be hired in this market.

firasd 3 hours ago||
Interesting that you said the word ‘using’. Why is that? If an agent could refactor excellently without a human who is the user
zuzululu 3 hours ago||
"Refactor this code spin up subagents" try that in codex to start your journey of improving your agentic coding skills but if you are too lazy to do even then there's just no hope for you.

I am currently working 3 remote jobs thanks to agentic coding and one of the jobs require me to interview and hire other devs. I find it amusing how many are irrationaly resistant and unwilling to use agentic coding. They are also the loudest in the room with opinions that are outdated. On the flip side, it's just as funny how to see how complicated engineers try to make agentic coding. It doesn't have to be and that's not its design. Don't get in its way as much as possible. The frontier models are good enough that adding complexity only wastes more tokens. We've come a long way from the summer you were fiddling with Sonnet 4.5 in Cursor or windsurf.

The job market isn't suddenly going to swoop in and save you by banning AI agents. Keep up or get left behind. Don't let other people's opinions about AI agents hinder your own progress, chances are they have no skin in the game.

apsurd 3 hours ago|||
lol - I guess, if I heard that in an interview that would be grounds not to continue.

    "enhance"
fwiw codex is very good. arguably better than the sonnet equivalent (value-wise). I use both daily, professionally. And "refactor this code spin up subagents" is hardly an expert-level "I wouldn't hire you" flex.

EDIT: agree with your comment updates. I'd just say it more positively leaning especially to people who are stressed financially. SOTA has advanced a lot so counter-intuitively being behind isn't really behind, it can almost be a way to leap-frog, lean on the Agents. But definitely you have to commit to putting in the reps and learning and not just mindlessly instructing AI to "fix it". Think that's where the discussion splits.

JacesArchivist 3 hours ago|||
> "Refactor this code spin up subagents" try that in codex

The deferral of all agency as a product owner over to the stochastic-word-box-in-a-do-loop feels like the kind of thing that, if you mentioned you do that in an interview, would be grounds to not continue.

Re: your updated comment, I'm glad you've found success with this pattern of behavior, but I'd suggest that you might be extrapolating "I haven't been punished yet for this shortcut" to "This is the future of work and those who disagree are luddites".

BenoitEssiambre 4 hours ago||
The benefits go beyond reducing token consumption. Compact contexts also foster better reasoning, enable intelligence across more layers if you can load them in a single context, and writing software to enable this, results in more correct software, software that _generalizes_, that has higher probability of being correct not just for tested cases, but for the interpolations and extrapolations of these cases.

Refactoring towards good abstractions is more powerful than people realize. There's information theoretic bayesian math to back this up.

It's a bit of a divine coincidence that software that is more economically and energy efficient to process and run tends to also be more correct.

It's all about reducing the entropy of your code. https://benoitessiambre.com/entropy.html

hammock 4 hours ago||
> It's all about reducing the entropy of your code

In all corners of our world and the universe at large, reducing entropy in anything can be thought of as “building.”

golemotron 33 minutes ago||
Imagine what happens when human readability is discarded as a goal. LLMs are already very good at inferring meaning with little context. If the objective function is reduction of token consumption, it's hard to know where we will end up.
jmartrican 11 minutes ago||
I think the name of the game is going to be "how do we maximize output while keeping token counts low". For example, finding ways to replace AI workflows, even if minor ones, with scripts/code.

Anthropic told me that I have till August 19th to be get my act together becuase they are going to reduce my token count by 50%. lol. I have been abusing my Pro Max allowance and need to start being less wasteful.

Articles like this, can help us come up with ideas on how to do it.

pmg101 4 hours ago||
It's very interesting to have some data on this.

It matches my experience which is that LLMs greatly benefit from well factored code, but are not particularly adept at creating such code.

Much like most human developers I suppose!

jerf 4 hours ago||
I've been budgeting myself explicit "slop removal" time. In fact I'm in it right now one window to my right here.

I still get a big win from AI on the net, but you do need to budget some time to clean up. I'm still on team "read every line".

In fact this is a case where I deliberately deferred some review because I was a blocker for another team. Now that I've got something to them I'm going back and I'm going to eat a bigger chunk of debt than I normally would, but it's worth it for unblocking the other team sooner. AI has made tech debt easier to take out, in all senses of that term.

It is also pretty decent, in my experience, at being guided into how to fix tech debt. Some other people's experience varies: https://news.ycombinator.com/item?id=49035455 YMMV.

tengbretson 3 hours ago||
It's totally worth doing. Especially since it only takes one or two manual improvements before you can just say "ok, replicate this new pattern throughout the codebase. Go."
j45 4 hours ago||
Out of the box, this is what I've found too.

I've had some luck while refactoring by helping it shape how to refactor, which can improve how well factored code should look like.

Providing examples of well factored code can go a long way, even if it's an open source repo of what to do / not to do.

holtkam2 2 hours ago||
Great piece, but it misses the elephant in the room: the lion's share of economic benefit from refactoring will come from the fact that it makes it easier for humans to understand. That means 3am pages get resolved faster, fewer bugs will end up in prod, and your team can ship faster than your competition - gaining a leg up in the market. Most importantly, folks will be more comfortable accepting responsibility and ownership of a system when they understand it... this means that if/when something goes wrong, people will more quickly jump in and fix it, and when things could be better, folks will jump in and improve it.
pragmatic 3 hours ago||
Agent code can only be read and understood by agents. We’ve reached the point where people just can’t comprehend these giant code blobs.

Feature, bug or emergent property?

I don’t think the distinction matters as much as the reality.

We’re being locked into using the AI tooling bc the code was generated with AI tooling.

These giant files of doom were being generated by humans anyway and were very hard to work with. With LLMs it’s at last manageable or feasible to edit, refactor etc.

I honestly think LLMs are going to save us from ourselves as the codebases became too large and “messy” for humans to comprehend. (Mono repos of doom)

On a personal level these giant files are abhorrent but that’s just personal taste and I don’t think any of the Martin Fowler refactor/cleanup stuff is going to matter at all anymore. Kinda sad on some level.

Catloafdev 3 hours ago||
> Agent code can only be read and understood by agents.

This just fundamentally isn't true and if this is your perspective then you're using LLMs wrong.

ahalay-mahalay 3 hours ago||
Fundamentally, the code can be understood by humans, but the volume of the generated code outpaces human capacity to understand it.
apsurd 3 hours ago|||
There was always bad code. Extend that to the general challenge of a bad hire doing net-negative things for the company. AI is a new problem because it 1000x a bad hire's blast radius. Personally, I'd even state that AI tends to turn an average and sometimes-but-not-always-good hire into bad hires because finally they get to do the thing they've always wanted at lightning speed in a way that previously the company red-tape wouldn't allow.
antonvs 3 hours ago|||
> These giant files of doom were being generated by humans anyway

Right, I've never seen an agent produce code anywhere near as bad as some of the human-generated code that I've worked on.

And, if your agent is producing huge files or functions, you can just tell it not to and it'll comply.

sltr 4 hours ago||
the punchline: "Refactoring reduces token consumption"

I appreciate the effort to quantify the benefit rather than pontificate. It's worth mentioning Martin Fowler wrote a whole book on refactoring [1], in which he states, "to refactor, the essential precondition is [...] solid tests", which I think is the real benefit here, AI or not. Good tests protect against regressions, whether human or robot. They also help encode the spec, which humans and robots can read.

[1] https://www.oreilly.com/library/view/refactoring-improving-t...

pmg101 4 hours ago|
(Just to note that although the article is on martinfowler.com Martin is not the author. It's attributed to Thoughtworks CTO Giles Edwards-Alexander.)
sltr 4 hours ago||
Thanks for catching that. Updated my post
jmartrican 16 minutes ago|
The author said that Claude was not good at refactoring. But the author was using Sonnet 5. I supect Opus 5 would have done a lot better at refactoring. It would have come up with a multstep approach, i suspect.
More comments...