Refactoring to improve existing code design--Reconstruction technique 09:substitute algorithm (replacement algorithm)

Source: Internet
Author: User

You want to replace an algorithm with another one that is clearer. Replace the function body with another algorithm.

string Foundperson (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""; }

Refactoring:

string Foundperson (string[] people)        {            List<string> candidates = people. Tolist<string>();              for (int i = 0; i < candidates. Count; i++)            {                if  (candidates. Contains (People[i])                    {return  people[i];}            }             return "";        }

Motivation: There are several ways to solve a problem. The same is true for algorithms. If you find that there is a clearer way to do something, you should replace the complex way in a clearer way. Refactoring can break down complex things into simpler chunks, but sometimes you have to delete the entire algorithm and replace it with a simple one. With more understanding of the problem, you will often find that there is a simpler solution than the previous one, and the original algorithm needs to be changed at this point. If you start using the library and some of the features/features provided in it are duplicated with your own code, you also need to change the original algorithm.

Sometimes you might want to modify the original algorithm to do something that is slightly different from the original one. At this point you can also replace the original algorithm with a more easily modified algorithm, 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 difficult to replace a large and complex algorithm. The first thing you can do is to break it down into smaller, simpler functions, and then you'll be able to do the algorithm replacement work with confidence.

Procedure: 1, prepare another algorithm, let it provide compilation.

2, for the existing test, the implementation of the above algorithm. If the result is the same as the original result, the refactoring ends.

3, if the test results are different from the original, in the testing and debugging process, the old algorithm as a comparative reference standard. corresponding to each test case, the new and old 2 algorithms are executed, and observe whether the results of 2 are the same. This can help you see which test case is in trouble and what kind of trouble it has.

Summary: Optimization of algorithms

Refactoring to improve existing code design--Reconstruction technique 09:substitute algorithm (replacement algorithm)

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.