One of the scenarios where bad code can be tame: it takes a long time to live, but my new Code depends on bad code. How can I change the bad code?

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.