Top
Best
New

Posted by javaeeeee 6 hours ago

The Economic Benefit of Refactoring(martinfowler.com)
149 points | 70 commentspage 2
jimbokun 2 hours ago|
> Add a new ItemWatchStore public async trait to the Firestore layer, following existing patterns exactly. The trait must have three methods:

async fn watch_item(&self, item_id: &str, user_id: &str) -> Result<()> async fn unwatch_item(&self, item_id: &str, user_id: &str) -> Result<()> async fn watched_items_for_user(&self, user_id: &str) -> Result<Vec<String>>

This shows the limitations of vibe coding. It takes someone with a long history of software development to prompt for something like this.

Even though the model is writing 100% of the code, still needed someone with a lot of programming knowledge to write the prompt.

skydhash 2 hours ago|
After reading a fair amount of OSS code, it's rather glaring that the data structures and algorithms are more like atoms than molecules in software design. We do have some molecules in the Design patterns, but they are more suitable to the OOP universe, like the collection api (filter, map, take,...) when dealing with groups of objects or the reactive api for dealing with concurrent tasks.

Most of software development is looking at some process and then decomposing it recursively until you get to those molecules/atoms of the computing world. Coding them is trivial, and while you can gain a certain boost from the AI, after a while you no longer have to write that much code. It will turn into a balancing act where the introduction of a new concept has to be done carefully.

danbruc 5 hours ago||
Interesting that the amount of code remained essentially unchanged. In my experience it is not unusual that refactoring messy code cuts the number of lines in half.
stingraycharles 5 hours ago||
It can go both ways. Lines of code is a terrible metric for pretty much any goal that refactorings are meant to tackle.
danbruc 4 hours ago|||
I did not mean it as a metric but as an indicator what was wrong with the code.

- the code is essentially good but all is in one file, you split it up, lines of code stay the same

- the code is essentially good but lacks some structure, for a function that does five things directly, you extract the functionality into five functions and call them from the original function, lines of code goes very slightly up

But once the code is actually bad - code duplication, bad abstractions, inefficient language use, ... - I would generally expect the lines of code to significantly drop. What scenarios are there where the code is actually bad but refactoring does not reduce the lines of code? It is certainly possible but at moment I am having a hard time comming up with a good example.

zahlman 4 hours ago||
> What scenarios are there where the code is actually bad but refactoring does not reduce the lines of code?

IMX: the scenarios where people hold higher standards than what you describe :) (i.e. such that "the function does five things" is deemed "actually bad". Of course, that does depend on the refactoring not causing an unacceptable performance hit, which can happen depending on the environment.)

danbruc 41 minutes ago||
Don't get me wrong, a function doing five things is bad. I consider code not good if you write if(condition) instead of if (condition) or a +b instead of a + b. Not that it is really bad on its own, but it indicates to me that the code was written with little care and I should probably expect bigger issues.

What I really want to get at is the distinction between leaving the actual code untouched and just moving it a bit around - to other functions, other classes, other files - and having to change the code - from deduplicating to completely rewritting it.

NichoPaolucci 5 hours ago|||
I don’t know. LOC to me is indispensable for certain refactoring goals in my opinion.

Taking a component and turning it from 7K lines to 3K lines and maintaining functionality obviously means there’s less complexity introduced, less to go wrong now, and less overhead to modify in the future.

Sure it can go the other way, the component needs to support something it might need, we need to adjust larger patterns, this function needs to be refactored into something more robust.

But lines of code is a pretty decent metric of success for “trimming down and cleaning up” style refactoring, to me at least. It’s not everything of course, but it’s definitely an indicator.

Shish2k 4 hours ago|||
A couple of days ago I split a single class which did 5 barely-connected things, where each of the 5 entry points had another 5-10 private helper methods (none of which were shared between entry points), into 5 separate classes which each do one thing - total number of lines went up (because they all have the same boilerplate import statements at the top), but being able to focus on one entry point and it's helpers at a time has made things much easier to work with
Catloafdev 4 hours ago||||
That's LOC in a single file, that's a bit different from total LOC as a metric.

Total LOC is a garbage metric. Things like reducing line count in specific files or components is a big benefit, but those lines are often moved, not dropped.

j45 5 hours ago|||
LOC can be one measure, but not always the sole or best ones.

Since LLMs are word generators, and have a propensity to generating words, they need to be shaped to understand simplest is best, more isn't more, and less isn't more always.

Trimming down and cleaning up could be formatting, standardization, commenting, or even some basic re-architecting that was overdue.

One of the biggest benefits of llms for refactoring I'm finding is reducing technical debt.

gga 2 hours ago|||
Yep, this was a real surprise to me, too. That’s why I included that top line in the figure. It also doesn’t line up with the agent’s predictions when it laid out the refactoring before proceeding.

There’s further work to do to understand exactly what’s going on here.

whats_a_quasar 4 hours ago|||
Yeah, I sorta expected the punchline to be "this 17,000 lines of code became 2,000" but it is still about the same quantity. Kinda interesting. Also interesting that the last change has by far the largest effect (4x reduction in tokens) and that also corresponded to the biggest reduction in single file LOC
j45 5 hours ago||
Quality of lines > Quantity of lines.

In some projects, the code might not have much, or enough testing, or documentation/commenting.

Code is largely for others and the future if the creators of it want to move onto other projects.

imoverclocked 5 hours ago||
It’s interesting that cyclomatic complexity or cognitive complexity scales with token usage. A codebase that enforces upper bounds on one of these two also (potentially) helps AI agents stay efficient.
persedes 2 hours ago||
Was hoping for a process diagram on how refactoring removes bottlenecks during software developement and allows you to ship faster etc :)

Still a nice writeup and love how these meta analysises (presumeably) done via AI can now easily capture metrics that inform your workflow.

t2ance 4 hours ago||
Can't agree more. Feels like LLMs are not able to understand your requirements at a high level and always add complexity by default. Really need a handbook to guide them to do those cleanups. Obviously they know the knowledge in their weights but they just don't know how to apply it.
ChulioZ 2 hours ago||
I can very much relate to the experiences described in the article. In my projects, I have multiple Claude skills and rules aiming at making my files (the code, but also the Claude files themselves) more token-effective. I have seen huge differences in token usage before and after invoking those skills, which I regularly do as part of having Claude audit my projects; although I haven't measured them. Those projects are (like the one in the article) fully developed by Claude Code, so I found such audits and mindful token usage very necessary.
vehemenz 5 hours ago||
Opus/Fable 5 (or really, any thinking model) do a better job of refactoring than Sonnet would. I think the takeaways are still valid, just not as valid when using a more expensive model at a higher effort level.
vaylian 5 hours ago||
Interesting take-away:

> Claude is unable to look at code, look at refactorings in general and work out which are suitable to apply: a human needs to actively guide it.

Claude is happy to produce a very large Rust file. But you need human guidance to make it smaller.

vehemenz 5 hours ago|
"I would have written a shorter letter but did not have the time"
janpeuker 2 hours ago||
> Claude is unable to look at code, look at refactorings in general and work out which are suitable to apply

I am just sitting here waiting for Grady Booch to write "Architecture!"

awsglkhj 1 hour ago|
I wonder, how close can we get to a reasonable simulator for testing software engineering practices before inflicting them onto the real world?
More comments...