I have recently got my hands on the code that is a definition of Doom.

It has no tests. A real Legacy Code, according to Micheal Feather.

I have never seen that many Gods in my life. God objects, God methods, and competitive-coding variables that I have to ask God for help.

What have I done so far?

I added an approval test to protect the core behaviors and some branches. I can't cover them all, because I don't have much knowledge about this code yet, and there are so many nested branches that writing that many tests looks inefficient to me.

I renamed ambiguous variables to show the intent.

What is blocking me?

This is a God object with 20 dependencies + 1 hidden global dependency. All of them are used by a single method that does many things that I have lost counting.

That long method has a lot of variables. Some variables are mutated everywhere inside a lot of nested ifs and for loops.

Extracting methods looks off because I have to pass the variable reference and mutate its value inside that method.

Since I have to "own" this code from now, I want to refactor it for me not have to scream every time I work on this.

If you say "if it does not break, don't fix it", then you are right. I don't want to touch it if it does not have this many bug tickets waiting to be resolved.

I have done the famous Gilded Rose, Tennis, Yatzy Katas, yet I still feel so useless looking at this code.

What is your process when you have to refactor legacy code?

submitted by /u/Pristine_Purple9033
[link] [留言]