Reconstruction Method 09: Substitute algorithm (replacement algorithm)

Source: Internet
Author: User

 

You wantAlgorithmReplace it with another algorithm that is clearer.Replace the function Ontology with another algorithm.

StringFoundperson (String[] People)

{

For(IntI = 0; I <people. length; I ++)

{

If(PEOPLE [I]. Equals ("Don"))

{

Return "Don";

}

If(PEOPLE [I]. Equals ("John"))

{

Return "John";

}

If(PEOPLE [I]. Equals ("Kent"))

{

Return "Kent";

}

}

Return "";

}

 

StringFoundperson (String[] People)

{

List<String> Candidates = people. tolist <String> ();

For(IntI = 0; I <candidates. Count; I ++)

{

If(Candidates. Contains (People [I])

{

ReturnPeople [I];

}

}

Return "";

}

Motivation: There are several ways to solve the problem. The same is true for algorithms. If you find that you can do one thing in a clearer way, you should replace the complicated method in a clearer way. "Refactoring" can break down some complicated things into simple small pieces, but sometimes you have to delete the entire algorithm and replace it with a simple algorithm. With more understanding of the problem, you will often find that there is a simpler solution beyond the original practice, and then you need to change the original algorithm. If you start to useProgramLibrary, and some of the features/features provided with your ownCodeAgain, you also need to change the original algorithm.

Sometimes you want to modify the original algorithm to do something slightly different from the original one. At this time, you can replace the original algorithm with an algorithm that is easier to modify, so that subsequent modifications will be much easier.

Before using This refactoring, make sure that you break down the original function as much as possible. It is very difficult to replace a huge and complex algorithm. Only by dividing it into simple small functions can you confidently perform algorithm replacement.

Practice: 1. Prepare another algorithm for compilation.

2. Execute the above algorithm for existing tests. If the result is the same as the original result, the reconstruction ends.

3If the test result is different from the original one, the old algorithm is used as the reference standard during the test and debugging. Each test case is executed based on the new and old algorithms, and the results of the two tests are the same. This helps you to see which test case is in trouble and what is in trouble.

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.