Rebuilding note-replacing algorithm and restructuring replacement algorithm

Source: Internet
Author: User

Rebuilding note-replacing algorithm and restructuring replacement algorithm

This article is in the study of the summary, welcome to reprint but please note the Source: http://blog.csdn.net/pistolove/article/details/42582611


In the previous article, we introduced "replacing function objects with functions ". This article introduces the reconstruction method of "replacement algorithm.

Let's learn about This refactoring method.


Open door

Discovery: You want to replace an algorithm with another clearer algorithm.

Solution: Replace the function Ontology with another algorithm.

// String foundPerson (String [] people) {for (int I = 0; I <people. length; I ++) {if (people [I]. equals ("Don") {return "Don";} if (people [I]. equals ("John") {return "John";} if (people [I]. equals ("Dave") {return "Dave" ;}} return "";}
// String foundPerson (String [] people) {List <String> candidates = Arrays. asList (new String [] {"Don", "John", "Dave"}); for (int I = 0; I <people. length; I ++) {if (candidates. contains (people [I]) {return people [I] ;}} return "";}


Motivation

There are often many ways to solve the problem, but there will always be some methods that are simpler than others. The same is true for algorithms. If you find that there is a clearer way to do one thing, you should replace the complicated way in a clearer way. "Refactoring" can break down some complicated things into simple small pieces. Sometimes you have to delete the entire algorithm and wait for a simple algorithm.

With more understanding of the problem, you will often find that there are simpler solutions beyond the original practice. At this time, you need to change the original algorithm, make it clearer and more concise.

Before using This refactoring technique, you need to determine that you have decomposed 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 Replace the algorithm.



Practice
(1) Prepare an (replace) algorithm for compilation. (2) execute the new algorithm for existing tests. If the result is the same as the original result, the reconstruction ends. (3) If the test result is different from the original one, the old algorithm will be used as the reference standard during the test and debugging.

This article mainly introduces the reconstruction method-replacement algorithm. I think the refactoring method is not specifically looking for an algorithm to replace the current algorithm, but in your development, when you find that a piece of code can become better, it can be replaced by an independent function or algorithm. It makes no sense to refactor just for reconstruction. We should judge based on the actual situation, and then consider whether to refactor. From restructuring notes -- refining functions to this article, we are all about restructuring functions in the form of "reorganizing functions. In subsequent articles, we will introduce the method of "moving objects between objects" for reconstruction. If you are interested, you can study and work together.
Finally, I hope this article will help you. If you have any questions, please leave a message. Thank you.

Rebuild note articles as follows

Rebuild notes-getting started

Rebuilding notes-bad taste of code (I)

Rebuilding notes-bad taste of Code (Part 2)

Rebuilding notes -- Building a test body

Rebuilding notes -- refining Functions

Rebuilding notes-inline functions

Refactoring notes-inline temporary variables

Rebuilding notes -- replacing temporary variables with queries

Refactoring notes -- Introducing explanatory variables

Rebuilding notes -- breaking down temporary variables

Refactoring notes -- remove the value assigned to the parameter

Rebuilding notes -- replacing functions with function objects

Rebuilding notes -- replacing Algorithms




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.