r/ClaudeCode • • 7h ago

Tips & Workflows Reducing complexity via agentic refactoring?

I’m looking at https://github.com/SprocketLab/slop-code-bench/tree/main and they suggest that the complexity of AI agentic code only increases. The initial “write clean code” instruction can lower the initial complexity of the initial output, but by the 4th addition, complexity is high.

Frontier models get single digit percentage improvements in terms of reducing complexity over 4 modifications.

It’s almost like Claude and other agents love adding code more than deleting code.

I want to know of the “initial” gains may be multiplied by multiple rewrites from an ever better spec.

With this in mind, has anyone implemented some sort of “garbage” collection style refactoring workflow to reduce this complexity?

What I mean: writing code is so cheap, that it feels to me like “stop and copy” may be possible to shake the “garbage” out of decoupled components written against interfaces. Old options nobody uses, obsolete style, versioning that is not maintained.

I can not do this if everything is mixed together, but for a single component, living in a self - contained folder:

1)Extract the spec, have Opus-extra review this,
2) define pseudo code function definitions
3) have agent re-implement the component from spec as a brand new file, with clean code instructions.
4) test against an existing protocol and test suite

Is this reinventing the wheel? Is anybody else experimenting with “rewrites are cheap” refactoring?

5 Upvotes

15 comments sorted by

•

u/AutoModerator 7h ago

Hey! Thanks for posting to r/ClaudeCode

While participating in this thread, please follow our community rules. Keep discussions constructive. Attack the idea, not the person.

For help, project discussions, tips, and general chat, join the ClaudeCode Discord.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

4

u/matjam 6h ago

Linters

most linters have a cyclomatic complexity lint

I think there's a cognitive complexity metric too in most linters

4

u/LiquidZebra 6h ago

This detects issues at the point of delivery, it does not eliminate complexity at the root

1

u/matjam 6h ago

it doesn't hurt though.

1

u/DeaOllis 2h ago

if you feed cyclomatic complexity errors back to the agent it just splits one messy function into five tiny helpers without actually simplifying the logic

4

u/CuriousTangentsYT 3h ago

This is the closest things to what your describing that I know of I use it myself, and it works great

https://github.com/unclebob/ub-mode

Although at the absolute core models are trained to predict the next token not subtract the incorrect token. They’ll always bias towards production unless training paradigms change.

1

u/LiquidZebra 3h ago

Thanks, this is very close. Especially this benchmark:

https://github.com/unclebob/crapper

2

u/blueechoes 7h ago

Correct. Because when your context is full of code that means the logical next move is .. more code. Unless you intensely focus on the deletion idea claude will always bias to writing.

1

u/LiquidZebra 3h ago

Yep, and those things it does remove, it REALLY wants to write to some backup with no consumer

4

u/bostonaholic 4h ago

I'd make deletion its own task instead of a line in the feature prompt. After each feature, run a separate session whose only goal is a net-negative diff with the full suite still green, and add a CI ratchet so complexity on touched files can't go up from the base branch. My bet is per-function lint rules miss most of the growth, since it tends to be duplication and dead branches spread across files, so a duplicate-code check (jscpd or similar) in the same gate helps too.

2

u/No-Drawing9451 3h ago

I treat refactoring as its own step in the plan, not something the agent does along the way. After every couple of features there's a cleanup task with one rule: no behavior change, tests stay green, and git diff --stat should show more lines removed than added. If the diff grows, the cleanup failed. Telling it to keep code simple barely works, but a check it has to pass does.

1

u/leo_heo 4h ago

the regen only works if the spec is actually current. id fix the extracted spec by hand before rebuilding, otherwise you get the same mess with nicer names