"Refactoring – Improving the design of existing code" reading notes----code bad taste "1"

Source: Internet
Author: User

Today, we do not introduce specific refactoring techniques. Let's take a moment to look back and summarize what kind of code needs to be refactored and what kind of code has an "unpleasant smell" to suggest that we need refactoring to suggest that this code actually has an improvement. I also like the author of the code that the characteristics of the use of "bad taste", very image is also very vivid. Below to get to the chase:

"1" Duplicated code (duplicate codes)

I myself also hate to repeat the code, it seems every time to see the duplicate code is a sign that you have a problem with this code, yes, we really should cultivate this awareness, because the duplicate code indicates that your future changes are likely to be modified in many places at the same time, you can not be completely unified. Nobody is perfect, people make mistakes. Duplicate code appears you have to be killed in the cradle by repeated code, otherwise, after you want to refactor, you will find the duplication of code fragmented, you can hardly clean sweep them all. If you combine them, the program structure will get better, the angle you stand on will be higher, and you will see more of the design behind the things. There are several cases of repeating code:

1) The two functions in the same class have the same expression, meaning that you can use the extract Method to refine the duplicate into a separate function and then let the two functions call it.

2) Two siblings of each other have the same expression, meaning that you can put their common code snippet into extract method and pull the up method to push the into hyperspace class, if the code is just like not exactly the same, you can extract those same places, If you refine the name of the function is the same between the brothers, you can also set the rest of the different places as virtual functions, using the From template method to form the template method design mode, so that the structure is more perfect. If some functions do the same thing with different algorithms, you can choose a clearer one and replace the algorithm with substitute algorithm.

3) Two unrelated classes have duplicated code, you can consider using extract class to extract from the duplicate code into a separate class, and then use this class in another class. Of course, deciding whether this class should belong to the third class or the code really is one of the two classes, and another class to call him, this must be your own decision.

"2" long method (too long function)

Long functions make others feel unacceptable when viewed, because they are complex, difficult to reuse, and very low in stability. As an object-oriented program element, we have good reason to believe that the object of short function can live better, it can bring us more convenience. Refactoring long functions is nothing more than dividing these long functions into small, short functions. Then they are worth a lot:

1) They have a powerful explanatory power, and their own function names represent what they are meant to say, which is telling you what to do instead of showing you what to do. Pay attention to my words, "What to Do" and "How to do" the distance is huge. We usually read the program more should go to see what the program do, let us know what this program is specifically to do, rather than we have to see how this program is done. Sometimes we have no obligation or need to look at how he did it when faced with a program or interface written by someone else.

2) They have the ability to share. The difference between short function and long function is reusability, short function can be reused better by us, and long function is often caused by the situation factor, too many variables involved are often difficult to reuse.

3) They have the ability to choose. In view of sharing, so that they can better choose between each other, the customer in the use of functions can be more flexible.

To make a long function a short problem is to have a good name , yes, you are not mistaken, the name of a function is critical to the function. The more experienced older programmers are more cautious about the choice of function naming. In fact, we can pay attention to their naming methods, really rigorous, cut the actual semantics. And those inexperienced programmers, young programmers in the function and variable name is very hasty, int i, J, K of the era has passed. Now we need to let them have the ability to explain themselves.

Refactoring should be done with extract method, if the temporary variable becomes a hindrance to your refactoring, you can try using replace temp with query to eliminate these temporary variables. You can also try to shorten your argument list by using introduce Parameter object and preserve Whole object if you have too many parameters to refine the function. And, of course, the last trick, if you still have a lot of temporary variables and arguments, you should use the Replace method with method Object.

Point of time is also important, there is also a small trick, if you feel a piece of code in particular need a note to explain the purpose of your code, then your opportunity to come, in fact, this means that your code implementation and code semantics actually appear relatively large distance. You need to use comments to fix it. We also talked about one of the uses of extract method is to shorten the distance between implementation and semantics. So, be bold enough to refactor it. Of course, conditional expressions and loops are often the source of refinement, the former you can use decompose conditional to refactor, the latter you can take the loop and its code extract method into a separate function.

So much for today, because of the refactoring techniques I have introduced this time, there are a lot of friends who have contacted me about the meaning and necessity of refactoring, with me to discuss the degree of cleanliness and clarity of the program and the distance before refactoring. I am very grateful to these friends to interact with me, let me have the perseverance and courage to continue to write. So today is not a refactoring method, we do a little tidying up, to smell the "bad taste" I feel better:)

"Refactoring – Improving the design of existing code" reading notes----code bad taste "1"

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.