๐ฌ Originally published on ZenOfCode
I'm reposting here to share with more devs โ feedback welcome!
How I Use ChatGPT to Refactor Smarter, Not Harder
Refactoring old code feels like walking through a minefield. One wrong move and boom โ tests fail, bugs spawn, and your day is ruined.
Hereโs how I use ChatGPT as a legit refactoring assistant โ not just a novelty.
๐งช Step 1: Let It Explain Old Code
Before I even touch anything, I drop chunks into ChatGPT and ask:
โExplain what this code does and if there are any smells.โ
It helps me:
Understand legacy patterns
Spot anti-patterns
See opportunities to clean up
๐ง Step 2: Ask for a Refactored Version
Once I know what it does, I ask:
โRefactor this code to use cleaner naming and extract logic to smaller methods.โ
Youโll get suggestions like:
Breaking up long methods
Extracting reusable logic
Better naming
๐งผ Step 3: Keep the Parts You Like
ChatGPT isnโt perfect โ but itโs a powerful second brain.
Use its suggestions as a starting point, then tweak it to your taste.
โ๏ธ Bonus: Custom Prompts That Work
Here are a few prompts Iโve saved:
โRefactor this async method to improve readability and keep exception safety.โ
โCan you convert this into a more testable service class?โ
โSuggest a more SOLID-compliant structure for this code.โ
๐ฆ Tools I Pair With It
GitHub Copilot โ for inline suggestions
TestGPT โ helps generate test cases
Prettier โ keep things neat post-refactor
๐ง TL;DR
ChatGPT can help you read, refactor, and rethink your code
Itโs not cheating โ itโs upgrading your solo dev experience
You still drive โ AI just helps navigate
๐งโโ๏ธ Like this kind of content?
Follow my dev blog โ ZenOfCode
Or drop me a follow here on Dev.to ๐ฌ