Reconstruction process for a driver (excerpt)
Author: Leili Hui (Personal Home Page: http: //www.coeffort.com)
Bad Smell Small Solution:
1. Duplicate code (Duplicated code)
Repeated program code is the most prone to odor in the program. In a big project, you can find that there may be multiple implementations of the same operation.
Method, the writing of the "fennel" word is much more. As long as you find the same program code block in more than two places, you need to be reconstructed.
. The simplest is the same code block in two methods of a class. You can use "extraction method"
Propose a common method and let two places call this function. Other, as the same complex conditions expressions, between classes
The same code, etc. should be extracted, and the shared classes allow the caller to use the same code. There is a big principle to be a
One function code is realized and only once. Otherwise, these duplicate code not only increase the size of the executable program, not only
Reading and modifying the difficulties of bugs.
2. Long Method
Martin said that the longest life and most useful is those short ways. Long approach causes more long ways to generate, because those contained
The function block in the long method is not extracted, and the added code has to copy and then change and then add it to a new method.
It became repetitive code. Short method is conducive to reading, its method name itself is a description of the function, it does not use the temporary variable
Too many, which is also conducive to reading and deactivation, more reuse. A trick to eliminate the long method is to see the comments in the code. Those fragments
The code block may be a small method.
3. Large Class
When a class is too much, it will have a lot of member variables, these member variables are in turn.
There are many repetitive code. The big class is that there are too many responsibilities, just like a bloated institution, the communication and management will be divided
External difficulties. At the same time, this big class is also like a long method, which will cause new major categories. (If you think about a factory worker
Responsible for production, packaging, sales, and even managers, is there any efficiency? How high is the requirement for workers? So, flow
Waterline and division of labor have been produced, it makes a worker only doing a certain stage of work or the best work)
4. Long Parameter List
Its drawback is that it is difficult to understand, it is difficult to use, it is difficult to expand. If you are on Windows under the creageprocess and deco in Unix
If you have made comparisons, if you have a Windows API call phobia, this is not difficult to understand the front two points.
5. Difference change (DIVERGENT CHANGE)
The phenomenon is if a requirement is changed, there must be several ways. For example, output may have a variety of different formats (such as plain text
Parts, html files, or databases, etc.), while designing only one of the designs, but it is expected that there may be other possibilities, so
The program must reserve the possibility, or use reconstruction when adding new functions to make this elasticity.
6. Shotgun Surgery (SHOTGUN SURGERY)
Metaphor is very image. If you eliminate a bug, you have multiple small changes. However, if you forget one, let's see others' face. Refactoring:
Refactoring is that the change of the internal structure of the software does not change the observational behavior, the purpose is to make it easier to understand and can be cheaper.
Make changes. So what is the benefit of reconstruction? According to Martin, there are four benefits II:
1. Refactoring the design of improved software
If there is no refactoring, the structure of the program will lose its original clarity with the addition of new needs; due to maintenance people's original system
One-sided understanding or just to achieve short-term goals will cause the program structure to be imbalance, repetition code, etc. And the more code added, the knot
The faster the structure will be corroded, so that the structure is more difficult to understand. The result of this malignant cycle is that the software is not maintainable. Poor design
It is usually characterized by repeated code. The target of reconstruction is to let the program restore a code only once in the entire program.
There is a clear structure.
2. Reconstruction makes the program more easily understood
Reconstructing smaller classes, shorter methods, fewer local variables, smaller systems coupled. Reconstruction requires you to be more careful
Mechanism, let the name reflect your intentions. If which piece code is too complicated, it affects the understanding, that is, the moment of reconstruction begins.
3. Refactoring help you find bugs
When you find that many bugs are caused by repetition code or when you refactor, some code is so uncomfortable or passing
Refactoring understanding the code, then you understand this sentence.
4. Reconstructing your programming efficiency
When you recover the high quality, design, structure, readability of the program, understand the program structure. Use the heavyweight
It is easy to use the method to add new features.
Martin also said that it is best to modify the BUG and do code reapp before the addition of the function.