Guiding Ideology: implement logical separation between new code and bad code. This is only to write unit tests for new code and implement TDD, without having to write unit tests for bad code.
Method 1 (when the responsibilities of the new Code are significantly different from those of the bad code, and the classes of the bad code are full of many different responsibilities, the new responsibilities are called in many places ): external application, that is, the decorator mode is used to overwrite the new responsibilities on the responsibility of bad code, and TDD is used to develop new duties.
Method 2 (when the responsibilities of the new Code are significantly different from those of the bad code, and the class of the bad code is difficult to instantiate and put into the unit test tool): new class, add new responsibilities to a newly written class and use TDD to develop new classes. Bad code calls New Class methods.
Method 3 (when the responsibilities of New Code are as important as those of bad code, the code is very fragile, and the IDE refactoring tool is poor): overwrite the method, create a new method with the same name as the original method of bad code, call the original method of bad code in the new method, or simply create a new method and use the TDD method to develop a new method.
Method 4 (the responsibilities of previous methods for bad code have been clearly presented): a new method, that is, the TDD method is used to create a new method called by the bad code.
One idea: Save time. Although it takes some time to write new code with TDD, the responsibilities of New Code are not stirred up in bad code, so that I can understand the code in the future, saving me time for reading code in the future.
Method 2: a sense of accomplishment. The responsibilities of the new Code are covered by TDD unit tests, rather than being confused and unable to be tested in the rotten code. In this way, the implementation of the new Code is not frustrated, increases your pride in professionalism.