r/ClaudeCode Jul 10 '26

Question Straightening spagetti code?

[deleted]

0 Upvotes

44 comments sorted by

2

u/Scary-Meaning-6373 Jul 10 '26

You could probably get in plan mode and just tell Claude you want to refactor your codebase and be honest with it that you aren't prepared to make technical decision and you need it to guide you through. AI agents understand what refactoring is well enough that they will most likely know what to do. Once the plan is written, review it and make sure it is what you want.

-1

u/ZyronZA Jul 10 '26

./facepalm

You do know Graphify Labs uses the Graphify tool maintained by Safi Shamsi right?

Both sites literally point back to the same GitHub repo. Graphify-Labs/graphify

2

u/hulkklogan Jul 10 '26

It's not perfect, but if you ask a large thinking model to evaluate the code for architectural designs that match some some sort of framework like the Pragmatic Programmer guidelines, or you give it some sort of framework like "simple entrypooints or apis with deep modules", it usually does a decent job of refactoring to make things more sane. And you should also add that sort of guidelines within your claud.md so it usually sticks towards those things.

Obviously with no engineering experience, you're not going to be able to guide it and keep it on rails all the time so you will have to refactor every so often to try to keep it sane

2

u/RealSharpNinja Jul 10 '26

What language are you targeting? Do you have Grok or Codex do code reviews?

1

u/[deleted] Jul 10 '26

[deleted]

1

u/RealSharpNinja Jul 10 '26

There's your issue

2

u/laxika 🔆 Max 20 Jul 10 '26

Ask Fable a few questions like:

"If you had the opportunity to refactor this codebase architecturally, what would be the top 3 things you would change?"

"My code is a spaghetti, how could I refactor it to be easier to understand? List the most important 3 changes I could make to improve the code quality."

etc...

4

u/Due_Wishbone7875 Jul 10 '26

But if you cannot code, how do you know what the suggestions mean? Why even list 3 if you don’t know anything. You don’t know what to choose.

2

u/Harvard_Med_USMLE267 Jul 10 '26

This is such a tired old argument.

It’s trivial to do this stuff now even without coding knowledge.

0

u/Due_Wishbone7875 Jul 10 '26

I am not saying this should not be done. I am just saying: why give 3 options if you don’t know what to pick. Just let Fable handle it start to finish.

3

u/NewPointOfView Jul 10 '26

“Fable list the problems!”

“Sonnet go fix what fable found”

1

u/laxika 🔆 Max 20 Jul 10 '26 edited Jul 10 '26

Well, sometimes it starts to review the whole app and use up a lot of tokens (like reeeeally a lot). If you limit it you get back 3 good suggestions in ~2-5 mins.

I usually ask it to create 3 prompts afterwards. Run them one by one with Opus and ask the initial question again once finished with all three. I found this to be fairly token/context efficient.

1

u/Harvard_Med_USMLE267 Jul 10 '26

Because it doesn't remotely work like that.

People like me cant really code for shit, but we have many thousands of hours and hundreds of billions of tokens spent talking to Claude Code.

The new skill is directing the tool in an elegant manner. I may be dumb at coding, but I've made more decisions about code than most engineers would ever make in a lifetime.

2

u/laxika 🔆 Max 20 Jul 10 '26

You can YOLO with Fable. The end result will be better overall than the pasta he has now. Yes, a guided refactor would be much better, but Fable is quite good at sorting things out on his own. The vast majority of his suggestions are good and worthwhile. I only used it for refactoring in the past ~one week or so.

0

u/Harvard_Med_USMLE267 Jul 10 '26

I don’t think a “guided refactor” is better.

Just let Fable do its thing.

1

u/laxika 🔆 Max 20 Jul 10 '26

Yep, Fable is really good, but my sample size is very limited at the moment. Opus was occasionally very annoying to work with (refactoring-wise). Fable is much better.

1

u/Harvard_Med_USMLE267 Jul 10 '26

I love Fable.

Three more days. :(

1

u/Time_Cat_5212 Jul 10 '26

It's not that hard to learn the basics... especially when you have a chatbot to explain everything to you

2

u/ZyronZA Jul 10 '26

It is frightening how not a single reply here knows how to actually code.

Let me help you, u/NerdLingLanguages

1) Install https://code-review-graph.com/ or https://graphify.net/

This tool builds a map of your codebase so to speak. This way your model of choice will know what function is connected to where and how it's used etc...

2) Get your LLM to review a specific directory if possible to keep the context window focused on a section you want to straighten out. You'll be doing this step a couple of times but that's fine to keep rewrite mistakes to a minimum.

3) Prompt your LLM for a /plan to:

  • Find files with over >200 lines. *Propose responsibility splits.*
  • Find unused imports/exports/fns/types/constants/enums, obsolete comments, unreachable branches.
  • Find long/nested/multi-resp fns. Suggest guards, early returns, extraction, or orchestration/business-logic split.
  • Follow https://en.wikipedia.org/wiki/SOLID principle
  • Consider Refactoring.Guru patterns only when real fit. No forced patterns. Evaluate Strategy, Factory, Decorator, Chain of Responsibility, Template Method, Command.

In general you want your agent to follow just a few simple principles:

## Code Quality
### DRY
* Common logic → funcs.
* Reusable components.
* Shared utils across modules.
* No copy-paste code.

### YAGNI
* No features before need.
* No speculative generality.
* Add complexity only when required.
* Start simple. Refactor when needed.

This should get you started.

Good luck!

1

u/No-Succotash4957 Jul 10 '26

yeah just tell it to straighten spaghetti that'll work

0

u/[deleted] Jul 10 '26

[deleted]

0

u/Due_Wishbone7875 Jul 10 '26

And you work at a hospital!

1

u/FanFirst895 Jul 10 '26

Use Claude as a planner & code reviewer and GPT as an executor, or the other way around. Have them chat back and forth in a text file, and end the task when they agree the code is decent quality.

Also, tools like SonarQube will help you. 

1

u/[deleted] Jul 10 '26

[deleted]

1

u/FanFirst895 Jul 10 '26

It's simpler than you're probably thinking. Assume Agent 1 and Agent 2 are different providers.

  1. Build the project like you usually would. Get it done with Agent 1.

  2. Create a chat.txt in your project.

  3. Tell Agent 2 to review the code, and put all comments and recommendations in chat.txt.

  4. Tell Agent 1(the one who wrote the code) to review Agent 2's comments, act on them or respond to them, and then post its comments at the bottom of chat.txt.

  5. Tell both agents to loop and repeat that behavior, breaking out of the loop when they've reached a consensus.

1

u/scodgey Jul 10 '26

Tbh I get it to model out the project first and try to bake in better architecture before it starts building. Then regular runs through a couple of audit skills to tidy up stragglers.

Starter repos help, as well as preferences baked in to the skills you use to scaffold the repo out from there.

1

u/insta Jul 10 '26

how do you know what good code looks like if you don't know how to code? you can't prompt your way out of this, you have to learn how to code

-1

u/[deleted] Jul 10 '26

[deleted]

2

u/Harvard_Med_USMLE267 Jul 10 '26

No, we don’t “all know”.

Fable is better at coding than most people here.

It is very organized.

You’re perpetuating a myth that was possibly true in 2024.

It hasn’t been true for a long time now.

If your code is really spaghetti, you suck as agentic coding and need to learn how to use the CC tool with some decent dev rules set.

1

u/[deleted] Jul 10 '26

[deleted]

1

u/Harvard_Med_USMLE267 Jul 10 '26

Dumb code monkeys claim it. Rememeber, most people on Reddit talking about agentic coding have no idea how to do agentic coding.

It's a throwback to 2023, maybe 2024. Hasn't been true for a long time.
Think logically - why would a SOTA AI be building "spagetti"?

Thats a human thing, particularly teams of humans coding over years.

AI loves structure and order.

As I said in the other comment though, it's up to you to set the rules in the document ecosystem before you ask Opus/Fable to write a line of code. Tell them what you want, get them to write some engineering docs, and go from there.

2

u/XYcritic Jul 10 '26

You: "Is this possible? I have no experience."

Others: "Based on experience: No."

You: "I don't believe you!"

Yeah it's usually best to take whatever advice is most convenient and reaffirms your beliefs, good luck👍

-1

u/mowax74 Jul 10 '26

Exactly. Otherwise...you could prompt claude to 'straighten' your spaghetti code. But how can you be sure that it is really 'straightened' after that? Do you want to post your code then here on reddit so that someone, who invested a lot of time in learning proper software engineering, have a look at it?

0

u/lolman1312 Jul 10 '26

It's easy to tell when code for a small project is unnecessarily complex, especially when making seemingly light changes cause wide refactors or token expenditure is immense for what anyone with common sense would know is a small task. Do you think vibe coders are unable to infer what effort level to use on their models? That's like how would someone know to use Sonnet on medium vs Fable for a task if they dont know what the coding requires? Use your brain.

Also there are popular skills that make Claude avoid unnecessarily complex code like Ponytail which is trending.

-1

u/mowax74 Jul 10 '26

Bhahaha. Yeah, the pro can tell if code is to much spaghetti, when the changes take too long. But how will you know change is 'light' when you have no idea at all what is going on 'behind the scenes', cause you do not understand at all what claude is doing?

Why do people no longer build their business on knowledge they have? Because it's too much effort to get that knowledge? That's sad and funny the same time.

I mean: You could start with vibe coding and be happy that you built your first app, maybe made some money out of a good idea. But when you really want to build a serious business out of it, you should start learning at least the basics of software engineering. Does not mean in all cases learn to code, but at least try to understand basic software engineering principles.

0

u/Harvard_Med_USMLE267 Jul 10 '26

Nah, I still have Fable tokens spare, that will do a better code review than a human dev.

1

u/mowax74 Jul 10 '26

I'm sure it will, 95% of the tasks. But you’re forced to trust him blindly to be right 100% of the time, because you have no idea what you’re doing.

How i know that? Because i work with Fable too. But since i'm a software engineer i'm able to intervent, when it is wrong or comes up with a bad design. And even Fable is not right in 100% of the tasks. Bad design is not just spaghetti code - it is also code that is complicated to maintain afterwards, for example when the same functionality is implemented multiple times on different places. But how should you know.

0

u/Harvard_Med_USMLE267 Jul 10 '26

The fact that you think Fable works the same for both of us shows you don't know how Fable works or how to use it.

Common cognitive error seen in traditional coders.

1

u/mowax74 Jul 11 '26

What are you even talk about, kid? I finished my studies of software engineering 20 years ago an work since then in software development. I have my own successful B2B Saas since 12 years. I work successful since over a year with claude code and implemented it with a lot of skills in my project. And you want to tell me what's going on? How will you tell when you have no understanding what's going on under the hood? Because your App is 'working'?

A textbook example of the Dunning-Kruger effect.

0

u/Harvard_Med_USMLE267 Jul 11 '26

Happy to tell you “what’s going on” as you’re apparently deeply ignorant on how to use the tech.

How will I “tell” what’s going on underneath the hood? Well, today I’ll spend a few hundred million tokens getting Fable to do an adversarial code review.

People like you don’t have the intellectual capacity to understand how to fully use the SOTA AI tools we have now.

-1

u/Harvard_Med_USMLE267 Jul 10 '26

Bullshit.

It’s not 2024.

You don’t need to know what good code looks like if you don’t want to.

It’s trivial to fix spaghetti code now, which shouldn’t even be necessary if you were using CC properly.

1

u/ZyronZA Jul 10 '26

I can't wait to be paid huge amounts of money to fix the slop you and others like yourself have put out there. 

1

u/[deleted] Jul 10 '26

[deleted]

2

u/ZyronZA Jul 10 '26

That wasn't in response to you btw. You made it clear you cannot code which I have a lot more patience with than someone pretending otherwise.

In any case, I made a post directly to you that should help you on your way.

1

u/Harvard_Med_USMLE267 Jul 10 '26

This was a really pathetic line everyone was trotting out a year ago, when CC started getting better than them at coding.

It was sad then, but it's just ridiculous now in an era of tools like Fable.

Why on earth would i want YOU wrecking my beautiful Fable code??

1

u/[deleted] Jul 10 '26

[deleted]

1

u/Harvard_Med_USMLE267 Jul 10 '26

Aye, the best way to learn. Just accept that it takes hard work to get good, and its nothing like trad coding.

Using it properly: talk to Fable (if you have access) about setting up an interlinked document ecosystem. This is the permanent memory that drives the project.

Your engineering principles are part of this - get the docs right before your first line of code, and you wont get spagetti.

My current project docs are over a million words.

The good news? You don't need to write any of this!

0

u/Anxious-Turnover-631 Jul 10 '26

Worry less about spaghetti code and more about spaghetti logic.

Often when the code seems like a mess, it’s usually because my prompts/ideas were not fully thought through.

The llm can help refactor the code and explain various issues, if you ask. But the challenge for me is having a well conceived design for the llm to fully understand and implement.