Code Earthquake (Wang Wei Gang Jan 2004) 1 The problem introduces very unfortunate, this is another failed software development experience. A experienced project manager led the seven handful programmers, using five months to develop a "silver certificate" management system for a bank - here, everyone does not need to be "silver certificate" The specific process and function of the system, we know that this is a real-time transaction and transfer system involving banks, securities companies, and customers. Because the system needs to connect to different physical locations, it is necessary to provide 7 × 24-hour trading services to customers through as many means as possible (this is a typical contragable environment. E-commerce model), the project team decides to select Web Service as the core support technology of the system, and select the C # language to encode implementation. The development of five months has successfully concluded that the project team has successfully constructed a set of functions, clear, clear, compliant, and component service model requirements. Figure 1 System Architecture Schematic diagram Figure 1 shows that the system is mainly composed of two clients distributed in the off-site Web Service Services and three different types (GUI, Web, calling centers). Both servers are connected to their respective databases via the ADO.NET data access interface. Servers B is more complicated with a concurrent control assembly than the service program A because the service program A is to manage shared resources. All three types of clients complete the transaction function through the Web Service interface provided by the SOAP Protocol call service. After the development of the development, the project team had almost no truly difficulties successfully completed the system installation and debugging in the customer site. The system performs well in the actual operating environment, whether it is functionally or performance, can meet the needs of users. It looks so that this project is so perfect from beginning to end. If it is not the emergency that occurs after three months, the project manager and his confident group members will be able to get the highest bonus of the company when the year ends. At the end of the third month of the system, the customer proposes to change the functionality of the system. In fact, this is not a particularly large functional adjustment. Briefly, the customer has changed the existing business system. Under the request of the new system, the "silver certificate" system needs to increase a new business code, and increases before another originally made of business code. 3 English letters composed. The project manager only glanced at the "Demand Change Manual", which was very cheering to say to the sales person responsible for the project: "Little means, one person can settle a day. This way, in order to steadily, you can promise three The problem in the sky is formally online. "Of course, things are not as simple as the project manager - if everything goes well, I don't have to have a multi-fold tongue here - this seems easy Modifying work, allowing the entire project group to fall into a dilemma. Ok, let's take a look at it, after the demand change, what happened in the project group. On the first day, the project manager determined according to its own "one person," asked the programmer to complete all relevant code changes and system tests in the day of the day. Early the next morning, the project manager found that unlucky programmers were red, not sleeping overnight, still looking for and modifying the code. No, the programmer B is in the project manager, and it has also been added to the row of code modifiers. On the third day, the situation still did not improve, and the hearts of the project manager have vaguely hidden. On the fourth day, the deadline for the customer commitment is getting closer and closer. The project manager also supplements the programmer to the Change group. On the fifth day of the morning, the three programmers participating in the modification agree that all code changes related to the new demand have been completed, and the new system can be tested. The project manager immediately adjusted two programmers who were responsible for testing. Unfortunately, the test work has fallen from the beginning.
At first, the system was unable to run smoothly in the test environment. After several recoimal and links, the client and server program can start normally; then, the programmer discovered that two important transactions in the system could not complete; worse Since performing a pressure test, the service program will soon lose the response .. Fifth day, on the sixth day, on the week, the bug report, code debugging, bug positioning, code modification, and compile links. Over, the project seems to have no signs of improvement. Salesperson uses a variety of Du's reason to explain the reasons for modifying the work. The project manager must be carefully reported to the competent leadership. On the eighth day, the project manager can no longer tolerate this confusion. ADO.NET ADO.NET Database A Database B Server A Server B Client A .. GUI .. Graphics Interface Component Web Service Connection Component Customer B .. Web .. Web Interface Component Web Service Connection Component Customer C. Call center .. Voice Network Component Web Service Connection Component SOAP SOAP Web Service Services Component Transaction Processing Component Data Access Control Component Web Service Services Component Transaction Processing Components Control Components Data Access Control Components
He summoned all the programs of the red eyes and commanded everyone to put down the work. He tried to make his own emotions, firmly said: "The project team encountered difficulties, this is my fault - I misses the situation. Now, we must return to the starting point, re-investigate, until we put below Three questions thoroughly figure out how many things need to be modified, do you need to modify the code? Is these modifications to cause defects or risks on the system architecture? Do you need to adjust some components before code change? Internal design? "The project manager pulled the project from the edge of the collapse at the critical moment. The programmer spent two days and carefully reviewed all code and design documents before the demand change, and the list of required code changes as follows: Program / module Modify the content database A / B table structure Add a code Field, modify the definition of a code field A / B Data Access Control Component and Table Structure Change Server A / B Data Access Control Component Interface Method Parameter List and Related Call Code Change Server B Con C Configuration Changing the Server BSCS Structure of the Component Shared Structure BACT BRIDE Control Component Shared Storage Area Operation Code Change Server A / B Transaction Processing Component Transaction Interface and Change Server A / B Transaction Processing Component Transaction Processing Code Change Server A / B Web Service server component web service interface parameter change Server A / B Web Service server component client incoming parameter legitimacy Change client A (GUI) graphics interface component data entry Interface Add a new edit box, modify the property client A (GUI) graphics interface component data modification interface to add a new editing box, modify the property client A (GUI) graphics interface component data entry interface in an edit box. Sex Change Clutch Change Client A (GUI) Graphics Interface Component Data Modification Interface Change Client A (GUI) Graphics Interface Component Data Retrieval Interface Change Client A Change Client A ( GUI) Graphical Interface Component Data Modify Interface Change Client B (Web) Web Network Web Network Component Query Interface Add New Edit Box Modifying a Size Customer Code of an Edit Box C (Call Center) Voice Interface Component for New Business Code Add a voice broadcast project client C (call center) Voice interface component temporary file structure change customer program C (call center) voice interface component temporary file, retrieved Change client A / B / C Web Service connection component web Service call code changes until now, the programmer clearly sees the complexity of the system upgrade. Customers just want to add a business code, modify the constitution rules of a business code, and the programmers must modify the program in dozens of places in the system! More importantly, after listing the huge modification work list, the project team must also carefully evaluate what kind of programmer who needs each modification, how long it takes, what kind of change after the modified code should Unit testing, code changes may cause system risks. For modules such as concurrent control components, the project team must also clarify the principles and methods of risk assessment and prevention before modification. In order to ensure that the quality of the software is unaffected, after all code modifications are completed, the project group also strict testing over the functionality and performance of the entire system. According to such ideas, the project team under the leadership of the project manager, and spent a whole three weeks completed all code modifications and system testing.
When the upgrade system successfully launched in the customer site, the customer's project leader only said to the poor project manager: "You are really hard, the results of the upgrade are also good - but why do you want to Lifening me to finish it within five days? Are you not a clearly lingering? "The project manager can't laugh and feels thousands. Why can a small demand change can bring such a large amount of modification? Chaosists have predicted that a butterfly fan is enough to trigger a tropical storm outside thousands of kilometers. This time, a slightly negligible demand change, actually triggered a "code seismic" of the software system: a modification of database table structure results in changes in data access control components, and then leads to transaction processing and concurrent control code changes. Next, there are service interfaces, client connection code, client interface components need to be modified .. More destiny, all code changes may trigger risks, they need to retest, critical code changes, even touch the infrastructure of software. This "seismic effect" once appears in the software, it will inevitably be like a snowball and cannot be cleaned. If the project manager is calm, the "Silver Condition" project is probably only worse. Now, I am eager to know that the project manager is that similar demand changes will trigger a "code earthquake"? Does the system's design and encoding process have an inevitable connection with this "code earthquake"? Is there an effective "shock", "damping" or "seismic" measures? 2 There are some cases in front of the forehead mentioned the issue of the technical personnel year-end bonus. For software companies, this is always a sensitive topic. It should be said that the basic salary (monthly salary or annual salary of the project manager, programmer) reflects the level of experience and work ability of technical personnel; various forms of rewards, including year-end awards, equity allocation, etc. It is the performance of the technician within the assessment period. The former is usually hooked with the position of the technician, there is a relatively clear standard; the latter, that is, the amount of the reward is not so good. Some companies directly determine the reward quota according to the position or level of the technicians, which actually erases the fundamental difference between basic salary and rewards, reflecting the encouragement and disciplinary role of bonuses; other companies have experience by boss or departmental manager Decided that the subordinate reward amount, under this distribution system, even if the leaders responsible for bonus allocation, there is no hierarchy, which is also likely to appear in "hardship" rather than the unreasonable situation allocated by "credit". Is there a reasonable, objective work performance evaluation method? its
Really, as long as the project management and financial system are sound, we are fully likely to use the technician to contribute to the company's "money", and we can further apply this aspect. The way is simplified to calculate the "contribution rate" of the technicians within the unit time. There are many ways to calculate the "contribution rate". I personally prefer the "contribution rate" to assess the "contribution rate" in units. That is, first calculate the "contribution rate" of the project group, then give the total bonus amount of the project group. The bonus allocation of each member within the project group is determined by the project manager. Compared to the approach to each technician "contribution rate", this approach ensures the accuracy of the evaluation rules and the accuracy of the reference data, and also gives the project manager's basic power to implement the reward and punishment within the project group. We can use the formula to calculate the contribution rate of the project group in unit time: the contribution rate of the project group (R) = the actual efficiency (V) of the project group is the actual efficiency (V) of the project group, the total cost and cost of the project team within the unit time (total) C). The fracture part of the formula is easily determined (check financial data, generally including management allocate and equipment depreciation cost, travel and office cost, wage benefits, etc.). More difficult is the molecular V. Usually the idea is to use the project's sales to reflect the benefits of the project group as the company. But we must consider some special projects from the company. For example, in order to be entered into a new industry, the company can sacrifice profits and customer signles. In this way, sales reflect the value of the company. For example, some R & D projects cannot bring any actual sales in a short period of time, how is it calculated? My personal opinion is that the actual benefits of the project can be calculated more than sales of normal projects, but the final decision is in the company's project regulatory authorities. For example, an item that can receive 1 million yuan in the event of a normal competition, because in special circumstances, the actual sales is 750,000 yuan, then the project supervision department will be 100 when the project supervision department is calculating the project team. Wan Yuan is a calculation basis (determined that the actual benefit of research and development project is relatively complex, here no longer expand). In the case of the "Silver Clearance" project mentioned in this article, the project team completed the development work on time, if the maintenance work is relatively normal, then the "contribution rate" of the project team will not be very low. For simple demand changes in the case, the project regulatory authorities must reassess whether or not the project regulatory authority will re-assess whether the project team has increased to the company's contribution. Objectively speaking, similar software upgrades are unlikely to increase the actual benefits of the project under any circumstances, most companies will exempt the user's upgrade fees for normal maintenance work. However, because there is a "code seismic" problem, the project group consumes six people to complete the modification work. The cost and cost of the project suddenly increases, and the "contribution rate" of the project team will decline accordingly. In this way, the reduction in the year-end revenue of the project group is inevitable (here we evaluate the work of the project team to the company instead of its work). That is, we must prevent the emergence of "code earthquake" during the design and development process, which can not only reduce the maintenance work of the project, but also effectively protect the bonus benefits of the member of the project team. 3 Case analysis or return to the technical issue. Simply put, the "silver certificate" system in the case does not have much problem in the system design, and the system relies on the hierarchical builds established by general interface technology such as Web Service, the basic model of component independence and distributed deployment. Pigeon. From the modification process of the project group, the reason why small demand changes can trigger a terrible "code earthquake", which is mainly due to the large number of code repetitions in the system.
Kent Beck and Martin Fowler will "duplicated code" as one of the "bad taste" in the code. Typically, the code repeat means the repetition of errors and risks. When the demand change occurs, it will inevitably lead to increase the power volume, modified cost, and modification risk. Martin Fowler's code repeats listed in the "Reconstruction" book is limited to methods and methods, between classes, and class duplicates between algorithms and algorithms, these common code repeat issues can pass "refining functions" , "Refining", "Value Domain Transfer", "Model Template Function", "Reconstruction Algorithm", and other methods or means resolved. However, in the "Silver Channel" project in the case, the problem of duplication has deepened into every level of software development. The code repeating problem encountered by the project team encompasses the database and application between code, the code duplicate between the modules, the code between the components is repeated, the code between the interface is repeated, and the code between the distributed system is more In order to complicate, even if the minor adjustment of the database structure is enough to move, the project group is not in love into the dream of "code earthquake". In order to fundamentally solve the code repetition problem within the complex system, the project group must prevent and manage each potential code repetition risk in the whole process of design and development. Below, we will use the "Silver CONTER" project as an example to list the main code repetitions in the project one by one, and discuss the prevention and solution of each type of problem. The code between the database and the application repeats the two service programs of the "Silver Clearance" project requires access to their respective databases. Each service program has a relatively independent data access control component to isolate business functions and data operations. The specific encoding method is such that each database has a set of initialization scripts (written in SQL language), and the data access control components in the program use the Struct structure to represent the structure of each table in the database. For example, for table Transactions, the creation statement in the database initialization script is (take transact-sql language): Create Table [Transactions] ([Name] varchar (20), [code] varchar (20), [Refcode] varchar (20), [RoleId] int, [value); defined in the C # language, the corresponding data structure is: struct transport {
Public string name; public string refcode; public int ring; public int ax od; public datetime date; Name, restriction conditions, and definitions of related query statements or stored procedures. This coding method is very common in the development of the database application system. It gives us a very obvious: When you need to modify the table structure, the programmer must first change the SQL statement in each database initialization script, and then modify it again. Data Access Control Components All definition statements related to the change field. In addition, the "Yin Diagram" project group still has a fatal issue when designing data access control components - each interface method will need to pass the fields, like this, in the parameter table: Public Bool NewTrans (String Name, String Code, String Refcode, Int RoleId, Int OpnodeId, DateTime DateTime); The consequence of this is that the database structure changes, all interface methods must be adjusted, all calls in other components These The statement of the interface method should also be adjusted accordingly. The above two issues belong to the code repeat problem with the database. The way to solve the problem is relatively simple, just insist on only the pointer or reference to the data structure in a similar parameter transmission, like this: Public Bool NewTrans (Transaction Data); the previous problem, the database of the table structure Indicates that the repetition problem between the (SQL script) and the code representation (Struct and related definitions in C #) is not so good. Moreover, in a component and hierarchical system design, the more logical levels of the system, the more similar repetitions. In fact, for such issues, even Microsoft's engineers are difficult to give a simple and effective solution. In the sample program Duwamish provided by Visual Studio .NET, almost every table structure in the database is created in the database, stored procedure code, service entity layer code, and data access layer code. The structure of modifying the database in duwamish is obviously a bitter thing, because the changes on any data structure inevitably raise the modification operation of the above four types of source code. A relatively simple solution I have ever seen is to embed the generated, modification, query, etc. of the database as a string constant as a string constant, and put together with the Struct structure and related definitions. The database installer provided by the system also uses the C # language implementation and directly creates a table structure using the SQL instruction saved in the code. If the change in the table structure occurs, the programmer can modify all the code related to the table in one source file. The second feasible method is to use the RTTI and REFLECTION mechanism provided by the .NET platform, directly from the data structure in the C # code (the name and data type of each element in the Struct structure in the runtime) in the C # code. , Modify and query the relevant SQL statement. This approach is high for programming language and programming technology, and is only applicable to language (such as C #, Java and other languages). Hao Gang talked about another viable solution in an article, that is, use the XML language to define database structure, the database system directly creates a table structure according to XML definitions, data access control code (including data structures in code) The general control is automatically generated according to XML definitions.
This method is more difficult to implement, but it should be applicable to most programming languages. The CMP Entity Bean technology in the J2EE system combines the advantages of the first two programs: the programmer defines the table structure in the ejb-jar.xml file, and the corresponding operation interface is declared in the implementation code of the Entity Bean (the abstract interface only declares Method), the package tool provided by the application service automatically generates the table structure and related operational code in the database according to the above definition. It should be said that J2EE's CMP technology has provided us to solve similar problems with a fairly excellent example. The code in the transaction processing component repeats the "Silver ID" project, there is a large number of code repeat problems between the transaction processing components of the two servers. The transaction processing components in the two servers are developed by different programmers. When the programmer is developing these components, it is not aware of the risk of the code repetition, nor does it care about whether there is a repeated part of the two sets of code. In fact, although the two service programs provide completely different business functions, there are many basic business operations, such as open households, currency conversions, interest rates, and exchange rate calculations, are repeated. If you do not pay attention to development, these duplicate business logic will inevitably generate duplicate code. In the source code of the "Silver CONT" system, repetition of transaction processing components includes a constant repetition that describes business attributes, deduplication with basic business logic, duplicate definitions, functions, functions of class members, or Methods of internal code. "Reconstruction", "Refining Function", "Refining", and other methods of "Refining", "Refining", etc., can effectively solve the code repetition problem in the transaction processing component. But the current problem is that how do we complete the "refining" of key code for transaction processing components distributed in different service programs? This problem can be attributed to the problem of extracting shared components between different programs or different components. For example, to resolve the code repeating problem between different service programs in the "Silver Channel" system, the usual practice is to extract the code involving the same or similar business logic, constitute basic business operations or tool classes, and combine these classes In a public library (for C # language, the public library here refers to .NET Class Library, which can be shared by different programs, shared by different servers. However, after the software development is completed, look up the duplicate code and refine the public library, which is a solution for a kind of desire. Best practice
However, in the design, it is possible to identify and extract the public characteristics between different components as much as possible. Pay attention to different programmers at any time during the development process, and to prevent code repetition generation, refining sharing as much as possible Public components - do not tell, this kind of high-house construction is responsible for the full power of system designers engaged in overall design. The code in the communication interface repeats all the service programs and client programs of the "Silver ID" system and the client exchange information to complete the transaction. In the interface code on both ends of the Web Service, there is also a relatively serious code repetition problem. For example, in the web service interface method provided in Server A and Server B, all parameters are parallel to the parameter table in parallel: [WebMethod ()] public int GetTransactionsCount (String Code, String Refcode, int RoleId, int OPNodeID, DateTime DateTime); because almost all transactions are related to the database, most parameters correspond directly to the relevant fields in the database. As mentioned earlier, the interface parameter table of the database access control component is similar, and there is a problem that the design of this communication interface is a problem with repetition and difficulty. Obviously, the client calling the web service interface must pass all parameters in a similar manner, and the client's higher-level components (such as interface components) will repeat this lengthy parameter table when calling the underlying interface. Once the database structure needs to be adjusted, each layer of code extending to the client and the server in both directions of Web Service is the center and the server, definition, and calling statements must be modified. Any omissions of modifications may cause fatal errors in compilation, link and even run. Avoiding the method of repetition in the communication interface is to use the entire data structure after the refining, replacing the full parameter table, for example, the above interface method example can be changed to: [WebMethod ()] public int GetTransactionsCount (gtcparamstruct param); Thus, when the change of the database structure needs to adjust the Web Service interface, it is possible to overwrite the definition of the GTCParamstruct structure. The code in the user interface is repeated in the "Code Earthquake" case, the code in the client program user interface component is repeated accounts for about 40% of the total amount of code repetition. Among them, the repetition of the interface control is quite common. For example, the data entry interface of the client program A, the data modification interface requires a replicated control to enter or modify the same data content, and the HTML control in the client B is also exactly the same field content. These user interfaces need to be modified without exception when the demand change occurs. The client program C is a voice handler used by the call center, with multiple IVR voice response modules that require the same field content, and there is a quite a number of code repeats. Some programmers tend to use dynamic binding and dynamic interface generation techniques to solve repetition of user interfaces, that is, automatically generate user interfaces by dynamically generating programs depending on the table structure or specific format of the database. However, dynamic interface generation technology is often related to specific language and development environments, it is difficult to transplant in different languages; dynamically generated user interfaces are not as beautiful and exquisite. Therefore, most programmers will still choose self-drawing user interfaces, and tolerate the "power of the interface code repeat". Another worthless question of interface programming is the code repetition problem of the data legitimate check module. In the interface of data entry, data modification, data query, etc., there is a need for legitimacy checking for the same field, which is obviously exactly the same. If you do not extract the check code in the public component when designing and developing, then once the business needs change the constitution rules of the field, we will hold the hands and take care of this.
The code in concurrent control components repeats the maximum obstacle encountered when the demand change is: the code and database structure of the service program B manage the shared resource, which also have many code repetition issues. Solving these code repeated problems is not difficult. It is really difficult to modify any omissions when modifying and transmitting control component code. It is likely to cause concurrent conflicts, service termination, incomplete data. "Programming Nightmare". Therefore, the "Yin Diagram" project team repeatedly modified the code, repeatedly tested and repeatedly encountered the failure experience to remind us that we have repeatedly emphasized the software system related to key technologies such as background services, concurrent control. It is often more important to prevent and reduce code repetition in design and development process. 4 Supplementary Description Another theorem for duplicate is: In multiplayer project group, if there is no good configuration management tool and a perfect version control method, avoiding code repetition is almost equivalent to a dream. This is because, between the resolution procedures, the main means of repeating the code between components is to extract the duplicate code and put it in the shared public library. In a multi-person project group, programmers who develop and maintain public library code and programmers develop and maintain other components must be effectively collaborated with a version control system, and they will not trigger demand, version or code chaos. . When the project group has a set of supported version control mechanisms, the functional version control software, we can even allow different programmers to maintain the code of the public library, which obviously increases the work efficiency, and to maximize Repeat of the code (see the chapter of the "Lingbo Micrush" book 4). 5 Summary .. Duplicate code means repeating errors and repetitions; duplicate code means adding maintenance costs and reducing project income. .. should prevented and reduce the code repetition during design and development. 1 Fowler M wait. Hou Jie, Xiong Translation. Reconstruction: Improved existing code design. Beijing: China Electric Press, 2003 2 Hao Gang. DUWAMISH7 Great Analysis Business Entity. 9CBS Development Master. 2003.113 Wang Wei Gang. Designed "Good-looking" user interface. Programmer. 2003.10 4 Wang Wei Gang, Zhou Hong. Ling Bo slightly: Software development alert case. Beijing: Tsinghua University Press, 2002