Improve code design-Summary)

Source: Internet
Author: User
Series of blogs

1. Improved code design-composing of optimization functions)

2. Improved code design-optimized the features between objects (moving features between objects)

3. Improve code design-organize your data)

4. Improved code design-Simplified conditional expressions (Simplifying conditional expressions)

5. Improved code design-making method callsimpler)

6. Improve code design-dealing with Generalization)

Find the places to be reconstructed

The most obvious places that may need to be reconstructed include comments, long methods, long classes, and long parameter lists.

Comments)

1. There is a comment in the description below in the functionCodeWhat is a block doing?Extract MethodPut the code in a separate method.

2. If the annotation above a function is used to explain what the function is for, in most cases, the method name is not obtained very well.Rename MethodRename.

3. If annotations are required to explain how the code runs, consider usingIntroduce assertion.

In addition, I think it is wrong to pursue code without comments too much, and the comments should be right.

Long Method)

1. Search for comments in the Code and useExtract MethodDivides a function into a small part.

2. check whether there are too many duplicate codes in the code.Extract MethodSeparate the code and call these small methods in the original code.

Long class)

The code of the class is too long because of the following two reasons:

1. Over time, new features are constantly added to the class.Extract class,Extract subclass,Extract InterfaceSolve this problem.

2. There are a lot of interface-related code in the class, such as updating a control. You can useDuplicate observed dataTo help extract a class used to update the interface, that is, the so-called "interface and business separation", but the class responsible for updating the interface should write the update synchronization mechanism.

Long parameter list)

1. If a parameter can be directly obtained through a function, you should remove this parameter and useReplace parameter with Method, Called directly in the function.

2. If an object can provide all the parameters required by the function, the entire object can be passed as a parameter to the function,Preserve whole object.

3. If several functions contain a certain number of parameters, these parameters are likely to be called data consorflow (Data clumps), Try to useIntroduce parameter object, Encapsulate the data in an object so that the function can directly call this object.

Rebuild and express your own opinions

When it comes to refactoring, a lot of people need to spray their stomachs. some people are excited about the many benefits brought by restructuring. A few people are angry about the benefits they bring, even the devastating results of an inappropriate refactoring. there are also many people who are discussing whether a refactoring is worth doing (this should be done based on the specific analysis of different situations ).

Reconstruction is like building a building with blocks. Reconstruction is like cutting a piece of wood into small blocks. Most of the problems to be solved by the design model are the problems to be solved. the majority of consensus on the benefits of refactoring is that after refactoring, the code can be understood and understood by others, discover hidden defects in the code, and help improve software design, new requirements can improve programming efficiency.

I remember that my first independent program was a course schedule Software (shown on the left ).VB. NETIt took three days to write.2000Multiple rows. At that time, a total of nearly10This interface is used to set the daily course for a week and other miscellaneous content. at that time, I also knew that there were a lot of "repeated" code, but I only thought that "it would be good to run", so I copied a large piece of code to several other templates. if you want to optimize such code, you should not refactor it, but rewrite it. later, I didn't need such clumsy tools to store the curriculum.HTML + CSSWrite a webpage.

We should not write a bunch of code in a mess before thinking about how to improve it. We should constantly try to optimize the existing Code during the writing process, at least do not use variable names and method names that are too confusing.

Refactoring takes a lot of time. If the system is about to be releasedDeadlineIn this case, it is not suitable for large-scale refactoring.

Many people think that restructuring is not used. They think that improper restructuring will lead to abnormal operations of the system that can run well. or restructuring requires a lot of energy and brain cells, and the benefits of restructuring may not belong to him. some people do not like frequent function calls and think that refactoring will reduce system performance. but do not use refactoring because it is not used, not to learn it, or to attack refactoring to bring practical benefits (often because you do not understand it ).

Reference

These articles are a summary of the code I read and think of myself. There is little nonsense. I would like to thank many friends who have been very careful enough to help me correct some mistakes, some valuable suggestions are also put forward.

Refer to <refactoring-improving existing code design>, <refactoring manual>, and some understanding and experience.

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.