Software design philosophy: Chapter VIII reduce complexity

This chapter describes Another way to think about how to create a deeper class. Suppose you are developing a new module, and found that the complexity of an inevitable. Which would be better yet: the user should be allowed to deal with the complexity of the module, or should deal with complexity inside the module? If the complexity associated with the function module, then the second answer is usually correct. Most of the user module and more than developers, so developers suffer more than users. As a module developer, you should try to make your module using the user's life as easy as possible, even if it means you need extra work. Another way of expressing this idea is that the module, it has a simple interface is more important than the simple implementation .

As a developer, it is easy to take the opposite way: to solve simple problems, the difficult questions to others. If unsure how to deal with the situation arise, the easiest way is to throw an exception, let the caller handle it. If you're not sure what you want to achieve policy, you can define some configuration parameters to control strategy and let the system administrator to find the best value for them.

Such an approach will make your life easier in the short term, but they add to the complexity, many people have to deal with a problem, not a person. For example, if a class throws an exception, then each caller class must deal with it. If the classes are exported configuration parameters, each system administrator at each installation must learn how to set them.

Example 8.1: based text editor

Consider the class text editor, file management GUI text, which is discussed in Chapter 6 and Chapter 7. This class provides a method to read files from disk into memory, a copy of the query and modify files in memory and the modified version written back to disk. When students must implement this class, many of them chose a line-oriented interface that has read, insert and delete entire line of method. This results in a simple implementation class, but it also brings complexity to the higher-level software. In the user interface level, the operation rarely involve the entire line. For example, the keystroke will cause a single character is inserted in an existing line; copy or delete options can be modified in several portions of different rows. Using line-oriented text interface, in order to implement the user interface, advanced software and connect the line to be split.

Character-oriented interface (The interface is described in Section 6.3) the complexity low. User interface software can now insert and delete arbitrary range of text, without the need for splitting and merging lines, therefore easier. Implement text classes may become more complicated: if it is internally represented as a set of rows of text, it will have to split and merge line to achieve operational character-oriented. This method is better because it encapsulates the text class division and complexity of the merge, thereby reducing the overall complexity of the system.

Example 8.2: the configuration parameters

A configuration parameter is not moved upward and downward example and complexity. Some classes can be derived the parameters that control its behavior, and not determine a specific behavior in the interior, for example the number of retry request or the cache size before giving up. Then the user must specify the appropriate class values ​​for the parameters. Configuration parameters are very popular in today's system; some systems have hundreds.

Supporters argue that the configuration parameters are good because they allow users according to their specific needs and work load leveling system. In some cases, the underlying infrastructure code is difficult to know the best strategy for the application, but users should be familiar with much of their domain. For example, a user may know some requests more stringent requirements than the time other requests, so the user to specify these requests higher priority is meaningful. In this case, the configuration parameters can achieve better performance over a wider area.

However, the configuration parameters are also important to avoid dealing with the issue and pass it to others provides a simple excuse. In many cases, the user or administrator difficult or impossible to determine the correct value of the parameter. In other cases, simply do a little extra work in the system implementation, it can automatically determine the correct value. Consider a network protocol must handle lost packets. If it sends a request, but no response received at a particular period of time, it resends the request. A method for determining retry intervals is introduced into the configuration parameters. However, a transport protocol may calculate their reasonable value, is the measured response time of the successful request, and then use the multiple retry time interval. This approach reduces the complexity of the user does not have to determine the correct retry interval. It also has the additional advantage of dynamically calculated retry interval, so that if the operating conditions change, it is automatically adjusted. In contrast, the configuration parameters can be easily outdated.

Therefore, you should avoid as far as possible the configuration parameters. Before exporting configuration parameters, ask yourself: "User (or higher-level modules) can be determined better than we are here to determine the value of it?" When you create the configuration parameters, see if you can automatically calculate a reasonable default values, so that the user only needs to provide value in exceptional circumstances. Ideally, each module should completely solve a problem; configuration parameters lead to incomplete solutions, increasing the complexity of the system.

8.3 overdone

When the complexity of passing down to be careful, this idea can easily be exaggerated. An extreme approach is to all the features of the entire application are placed in a class, which is obviously meaningless. If closely related to (a) reduced complexity and functionality of a conventional type, (b) reduce the complexity of the application will result in many other parts of simplification, (c) decrease the interface to simplify the complexity of the class, then reduce complexity It is the most significant. Remember, the goal is to minimize the overall system complexity.

Chapter 6 describes how some students in the class methods defined in the text reflect the user interface, such as a method backspace key functions to achieve. This seems to be a good thing, because it will be passed down complexity. However, the knowledge of the user interface to add text class and can not be simplified high-level code, and the user interface has nothing to do with the knowledge of the core functions of text classes. In this case, reduce the complexity will only lead to information leakage.

8.4 Conclusion

When developing the module, looking for opportunities to make themselves some extra pain to reduce the pain of the user.

Guess you like

Origin www.cnblogs.com/peida/p/12065701.html