I've inherited a genuinely difficult legacy system with no automated tests, several God objects and methods, around 20 dependencies, a hidden global dependency, and deeply nested conditionals and loops. Many local variables are mutated throughout the main method, so extracting methods would require passing mutable references everywhere.
So far, I've added approval tests to capture the most important behavior and renamed ambiguous variables to make their intent clearer. I can't cover every branch yet because I'm still learning the code and there are too many paths to test efficiently.
I'll be responsible for maintaining this system, and it already has a backlog of bugs, so leaving it untouched isn't realistic. What process or techniques do you use to safely improve code like this without attempting a risky rewrite all at once?
3 Answers
Start by creating seams around the code you need to change. Characterize one narrow behavior with an approval or integration test, then put wrappers around the global and external dependencies. After that, make small behavior-preserving changes and run the tests after every step. Don’t try to clean up the entire God object just because you found it; improve the parts you actually need to touch.
The strangler approach works well here: define a small responsibility, implement the replacement beside the old code, and route that responsibility to the new component once its behavior is covered. Repeat this gradually until the old implementation has little left to do. It’s less risky than a rewrite and gives you a clean boundary to work toward.
Your approval tests are a strong starting point. Keep adding focused characterization tests around behaviors as you encounter them, and commit very small changes. Rename things, isolate dependencies, introduce state objects, and extract only when the tests give you confidence. The goal isn’t to make the whole codebase beautiful in one pass; it’s to leave each area safer and easier to change than you found it.

Related Questions
How To: Running Codex CLI on Windows with Azure OpenAI
Set Wordpress Featured Image Using Javascript
How To Fix PHP Random Being The Same
Why no WebP Support with Wordpress
Replace Wordpress Cron With Linux Cron
Customize Yoast Canonical URL Programmatically