How to refactor like a god using claude code (no bullshit)
Guides / Tutorials
Since I think I'm good at refactoring any file with claude code I thought about dropping a quick guide on how I do it. No matter how big or complex the file to refactor is:
Ask CC to generate an .md document explaining how the file to be refactored is used on your codebase.
Now ask CC again to generate a detailed .md plan file explaining how the refactor would be accomplished in very detailed steps (without implementing it yet)
Head over to any LLM (I use claude desktop for example) and after chosing the right model (Sonnet 4.5 for smaller refactors or Opus 4.1 for bigger ones), attach the both files generated previously + the file to refactor (if it's not too big), and use this prompt:
After analyzing the following attached files, I want you to give me the prompts (following the project guidelines) to refactor the <file name goes here> file (which I have also attached) into smaller elements without EVER breaking its functionality, just modularizing it.
When writing the prompts, remember that:
* They must be able to be followed by Claude code without getting lost at any point.
* There should be no implementation gaps.
* Only import tests should be run with the new files created in each phase of the process to ensure compatibility at all times.
* You do not need to write explicit source code, just clear and concise instructions for Claude Code to carry out the task.
* The last prompt must be a deep review.
Now copy and paste every prompt generated by Claude Desktop into Claude code with the /refactor customized command (get it here) and voila. Get claude code to work.
Note: If the refactor is complex, use the thinking mode (press tab), but be careful because that consumes a shit ton of tokens.
Pro tip: Don't let claude code compact conversations. If you are close to it without the refactor being completed, clear the current session context and force claude code to analyze the context from the generated files in step 1 and 2. Then carry on with the next prompt.
Hope this helps!
Post #324
401
- 🔥 3