Refactoring Legacy Code: Where Should I Start?

I've inherited a 5-year-old internal service, and there isn't a single line of test code. Every time I add a feature, things break all over the place, and it's really stressful.

I'm planning to start by adding tests, but before that, I want to clean up the structure a bit. The problem is I have no idea where to start.

For those with experience, what order do you usually proceed in?

by 데이터덕후416

9 answers

In my experience, this order worked well. 1) Pick just 2–3 core use cases and wrap them in integration tests. 2) Split modules along those boundaries (strangler pattern). 3) Clean up one chunk at a time, starting with the pieces you split off. I'm against a full rewrite from scratch. Domain knowledge lives only in the code, so if you rewrite it, you lose all of it.

by 궁금한사람744 · ▲0

Same here ㅠㅠ Not a single test, it's pure spaghetti, and feature requests just keep piling in...

by 뉴비탈출555 · ▲0

Refactoring, what’s the big deal? It’s just making things pretty, lol. Kidding. But first, I think it comes down to how much you can actually take on. If your company doesn’t give you time to refactor, no method is going to work. Convincing them of that is the real first step.

by 무한도전러423 · ▲0

I also inherited a legacy system as a 5th-year dev and have been struggling with it for a year and a half... The real order isn't 'add tests → clean up structure,' but 'decide what to keep alive → test only that.' If you try to fix everything, you'll be frustrated 100% of the time. The method I used was digging through request logs and first extracting how many times things are actually called. Surprisingly, unused code made up about 30%. If you delete that first, the refactoring scope shrinks dramatically.

by 프롬프트장인205 · ▲0

In Michael Feathers' book (Working Effectively with Legacy Code), he talks about characterization tests, and those really help. Even for code you don't understand, if you just lock in the current output values as they are, that becomes a safety net. It doesn't have to be a perfect test.

by 데이터덕후135 · ▲0

Well, isn't that mindset of 'let's sort out the structure first' the most dangerous trap? If you touch the structure without tests, you're breaking things even more while not knowing where it'll blow up. Structural improvements should come after you've laid down the safety net of tests.

by 스타트업러869 · ▲0

Is there a source?

by 알고리즘고수967 · ▲0

There's a lot of talk that just adding tests solves everything—but the maintenance cost is no joke. After grinding for months to hit 80% coverage, you'll find yourself unable to actually build features and just fixing tests. If you're planning to handle it alone, keep the scope really small. Otherwise, you'll burn out while refactoring.

by 문과출신개발자976 · ▲0

The right move is to add tests first. Facts.

by 지나가던행인247 · ▲0