Start from the business value chain of the company - test drive development in my eyes (TDD)

xiaoxiao2021-03-06  49

This article is transferred from CNBLOGS, author Wayfarer.

Original article address:

Start from the business value chain of the company - test drive development in my eyes (TDD)

This week, I saw the "Dialogue" column of CCTV, and the program interviewed Samsung CEO Yin Zhonglong. Among them, a woman in a live woman made me impressive. She mentioned a company's run value chain, a total of three steps: first is to find value, find the target market; the second step is to produce value, produce high quality products; finally protect value or harvest value, do a good job Brand.

How to understand? This lady metaphor with Nike. The first step is to design NIKE shoes, this is the value of the value, it may get the value of $ 100; then get China to produce, about $ 10; Go to sell, you can harvest $ 90. A pair of shoes are priced at $ 200, while the production value can be harvested only $ 10. This step is the lowest interest, but our company has done best. How to find value, then how to consolidate your brand and awareness, China's company is not so good.

Recently, our team has carried out a technical exchange on test driver development. Looking at the records and comments of communication, get a common conclusion is that test driver development is a good stuff, but it does not seem to comply with China's national conditions. Many people have mentioned the reasons, the most, the project is tight, there is no time to write test code. In the project, it should not be used by TDD, most people are suspected or visually attitudes. This attitude and viewing, I remembered this paragraph in "Dialogue".

Carefully analyze the three steps of running value chains, I think it is similar to the TDD value chain developed by the software. The first step is to find value. Applying to TDD, it is the test lead, drive us to write code. Step 2, production value. Undoubtedly, this is a phase of writing code. The third step is to gain value, in TDD, we harvested not only after the development of complete products, but also harvested a complete test suite. Like the production of NIKE shoes, we have excessively pay great importance to the second step in the production value stage in software development, and for the first step and third step, or ignore it, or it is not improved to the corresponding height.

First, find value and production value

It is used to the programmer of traditional development model, which is very uncomfortable before writing code, and write the test method, which includes me. So, how do we usually find value? First, by demand analysis, then enter the design phase. During the design phase, the results of the demand analysis are repeatedly achieved from the perspective of implementation, not from the perspective of customer application. The TDD subverts this model. Because the test is required, the programmer must start from the function, starting from the application. In the process of writing test code, we need to consider those functions, the name of the corresponding class, the creation of objects, and the mode and policies that may be applied, so, in this process, such as the brown shoots Gradually set up.

In this process, we have to carefully choose the test. Although the head is rising, it is very magnificent, fast, but it tends to ignore the scenery along the way. In the test driver development, I suggest you carefully plan test samples, from the step-by-step improvement of test samples, gradually drive out your more complete code. For example, I need to develop a smart personal assistant, which is currently available to provide users to customize their own categories, then personal assistants search according to the customization of the user, and perform the results obtained by different categories storage.

Let's try the process of TDD. According to the analysis of the function, we should first have a smart assistant object, the test code is as follows: [TEST] public void testsmartassistor () {smartassistor assistor = new smartassistor (); assert.isnotnull (assistor);} Of course, this code is Continuous compilation cannot be passed, we also need to create a smartassistor type. However, don't be a little bit this step, it actually urges you to conduct a preliminary understanding of the project, at least, you need to think about this type of this will be created, what is its name? This is a driving force. (For the sake of simplicity, only the test code is listed in this article) and what can this type do? We classify the functionality of the personal intelligence assistant should include three functions: 1, customization; 2, search; 3, storage.

Think carefully, in fact, only search and storage are the responsibility of the intelligent assistant, but customized is a condition that the intelligent assistant should work. In this case, we should consider the order of the customer application, we should first achieve custom functions. To customize categories, you should have a category type, and the function of customization of categories should be taken by a dedicated controller. [Setupup] public void initObject () {category cg1; category cg2; categoryContainer cgcontainer; smartcontroller control;}

[Test] public void TestCategory () {cg1 = new Category ( "SoftWare Engineering", "TDD"); cg2 = new Category ( "SoftWare Engineering", "Design Pattern"); cgContainer = new CategoryContainer (); cgContainer.Add (cg1); cgcontainer.add (cg2); assert.isnotnull (cgcontainer); assert.arequal (cg1, cgcontainer [0]); assert.Arequal (cg2, cgcontainer [1]);}

[TEST] public void testController () {Control = new smartcontroller (); assert.isnotnull (control); assert.istrue (cgcontainer);}

The above test code is completely considered from the user's application perspective. To customize categories, you must have category type category, which should implement a constructor that delivers the primary category and subcategories. Since customized categories may be much, there is a need for a category container CategoryContainer.

And custom categories, is done by the controller SmartController. Customized method Customizecategories () custom multiple categories and return to Boolean to determine if customization is successful. Through the above test code, the corresponding program should be not difficult.

Search and storage features should be considered next. I mentioned earlier, these two functions should be the responsibility of the SmartAssistor type. First modify the initial test code: [TEST] public void testsmartassistor () {smartassistor assistor = new smartassistor (); assert.isnotn (assistor); assistor.search (); assistor.Store ();} At this time, I found some problems in some problems when writing Search () and store () methods. What should these two methods returned? Is it a Boolean value? So what is the result of search? What is the file formed after storage? Do you only need these two behaviors for users?

Careful analysis, I think, Search () and store () methods return to the Boolean value, but search should return to the search results. The Store () method should also specify the format of the stored path and file. The user not only needs to search and store, but also the displayed function should also be provided. Therefore, the need to improve the above test code: [Test] public void TestSmartAssistor () {SmartAssistor assistor = new SmartAssistor (); Assert.IsNotNull (assistor); SearchResult result = new SearchResult (); Assert.IsTrue (assistor.Search (control .Categaries, out result); assert.istrue (Assistor.Store (Result, @ "D: / smart assistor /", "result.xml"); assistor.list (result);}

At this point, according to the procedure written by the test code, it should be said to have the prototype of the personal intelligence assistant (so-called intelligence, there should be automatic search, automatic matching, automatic classification, etc., this article is just some views of TDD according to the project, So these functions are omitted).

Second, the process of reconstructing the "discovery value" through TDD is far from ending. By testing code, we will find some problems from our client's perspective. In the code already implemented, the SmartAssistor type implements the features of Search, Store, and List. But is these responsibilities really be borne? It is like this on the surface. However, according to OO's thoughts, is this SMARTASSSTOR? Its and search results, is there a way to say that the coupling is too close? Will this design be abstracted? These should be the focus of our consideration. Considering the timing, it can be the beginning of the design, or it can be reconstructed.

When refactoring, TDD is still not allowed, only it can guarantee the reliability of the program, the correctness of the reconstruction. Start reconstruction.

First consider it from behavior. Is the search function be complicated? There may be precise search, blur search; may be search online, or it may be a native search. So what about storage? Whether the operation of IO is frequent, whether the storage requirements will be gradually upgraded according to the security level? Revise again, is it necessary to display the way to the personal intelligence assistant? Obviously, the above behavior is complicated.

Then consider the abstraction. Do you need to abstore these behaviors? That is, does the carrier of these behavior will have multiple types? Obviously, the search may be a file search, which may be a text search, or the database search; the stored format also has a variety of diverse, text files, XML files, database files. The way the display may be displayed through the browser, or it may be displayed in WinForm. Perhaps the user requires a text box with a scroll bar, perhaps just a simple text display. There are many forms of objects, do you need abstraction? Obviously! After thinking about it, I found that there is too much thing that needs to be reconstructed, how should I start? First, let's take the SMARTASSSTOR's responsibilities first, use a more single object to complete their respective features. For example, we can introduce SmartEngine to manage search features. These objects are then extracted from their respective interfaces. Still write test code, consider searching, first you need to separate the objects: [TEST] public void testsearching () {searchengine engine = new searchine (); assert.isnotnull (engine); searchresult result1 = new searchResult (); SearchResult result2 = new SearchResult (); Assert.IsNotNull (result1); Assert.IsNotNull (result2); result1 = engine.ExactSearch (control.Categaries); result2 = engine.BlurSearch (control.Categaries);

SearchResult tempResult1 = new SearchResult (control.Categaries, "contents"); SearchResult tempResult2 = new SearchResult (control.Categaries, "more contents"); Assert.AreEqual (tempResult1, result1); Assert.AreEqual (tempResult2, result2);

} Run the test code in NUNIT, failed to pass. Then create the Searchengine type in the program and implement ExactSearch and Blursearch methods. Until the passed in NUNIT, all shows the green light.

Next, abstract Searchengine interface ISEARCHENGINE, and let Searchengine implement the interface. Where interface methods include ExactSearch and Blurseearch methods. The previous test code is small modification. After the modification needs to be run in NUnit, ensure smooth pass: [TEST] public void testsearching () {ibearchengine = new searchine (); assert.isnotnull

}

Investigate the Searchengine type, this type of object should only reserve an object throughout the program, so a single case mode should be used. Modify test code: [TEST] public void testsearching ()} {ibearchengine = searchengine.instance; assert.isnotnull (Engine); ......

} Change the program code according to the test code to change the constructor of the Searchengine type to Private, and read-only static property instance is provided to obtain a single case object. It is not enough to just enough. Taking into account a variety of situations, such as Internet, Local Machine, DB, etc. The Searchengine type should be specific to different types and simultaneously implement the ISEARCHEENGINE interface. For example, the search range is in the Internet, the test code is as follows: [TEST] public void testinternetsearching () {ierchengine engine = Internetsearch ;;

}

Since there are so many types, the type of creation must be managed through the factory. At this time, the test code need to make further changes: [Test] public void TestInternetSearching () {ISearchEngineFacotry fatory = new InternetSearchEngineFactory (); ISearchEngine engine = factory.CreateInstance (); Assert.IsNotNull (engine); ......

} Similarly, we should implement Testlocalsearching () and TestDBSearching () and testdbsearching ().

According to this idea, reconstruct the storage function and display function respectively. Remember, every step of doing a step, you need to strictly follow the TDD. First write the test code, then run in NUnit. If it is a red light, you need to write the corresponding code, then run NUNIT until all are green.

Third, the first step

On the surface, write the test code, program code, is indeed a few Kill Time. But we need to seriously think about the meaning of the so-called "discovery value". Through the way of testing the first way, the customer's needs are taken to simulate the status of the customer, and the end of the "Production Value" will be reached step by step by step by step. "Discovery" and "production" parallel, and the "quality inspector" has been following the same time, ensuring the quality of the product.

It is like the production of NIKE shoes, and the appearance of the attractive style must be designed with the angle of consideration, such as a large-scale production will have profitable possibilities.

The production process of TDD may be slower, but please don't ignore it, it has already saved the time of encoding unit test. After adding, how long is it? So, don't take me with "time" reasons.

Fourth, investigate the third step - harvest value

The traditional way, after the product is produced, it is a large number of tests, including unit testing; finally harvested products, a large pile of source code and documentation. The way the TDD has provoked the process of unit testing, and also harvested another gift-test or test kit.

Test class is definitely a wonderful gift. It must be recognized that its value is not just the stage of "discovery value", which is also our "gain".

First: better document than the code, better code than the document, don't have to diamond into the telecontest document, four insults. The text description of the document is neither accurate, easy to generate ambiguity, and it is easy to generate a document synchronization. Maybe it can promote your understanding of business and architecture, but for the program itself, you cannot get basic revelations from the document.

So see the source code of the program? You will wrap around in numerous objects and methods, and finally fogwate, after exhaustion, or not.

And see the test code is different, you don't need to understand the specific implementation of each method, because the test code is written from the customer's application perspective, after reading the test code, you will easily clear the context of the program structure.

Second: The best textbook for the recruit training camp may have a new employee in your project group. If he is familiar with TDD, then these test classes are the best documents he is familiar with the project; if he hasn't heard of TDD, no need to worry First give these test classes first. As long as he is not a newcomer design, I think this recruit will soon become familiar with the way of project group development. Let him write a few test samples, he will immediately put into the embrace of TDD.

Third: In the development of confidence in full load, in addition to serious, efforts, team spirit, is believed in confidence. The so-called confidence here is not a spiritual look that the ability to be optimistic and objectively evaluated, and the programs are confident in the correctness of the code. Whether these code written by themselves or others, as long as they are strictly in accordance with TDD requirements, you will be confident on them. Although it is not guaranteed that there is no bug, it must be recognized by the unit test, we have reduced BUG to the smallest.

V. Conclusion Chinese companies run the value chain in the enterprise, the second step of profit is the second step, but ignore the importance of "discovery" and "gain value" for a company. South Korea Samsung is still a 12 billion dollars of companies that have lost a loss of more than 600 billion US dollars, and now it has successfully turned into a profit and ranks among the world. There are a lot of reasons, but it cannot be ignored is that he is very good in two steps in the value chain. From the high-end products, the value has been found, the target market is found; the value has been got from the brand creation, and the world is going to the world.

I don't mean that software development must use TDD's way, it naturally has a lot of defects. However, in the development process, we also pay attention to the "discovery value" stage of the design, and then do not ignore the "value" that should also be harvested while harvesting products. From this point, maybe TDD is more in line with this value chain. And our programmers must do not pay for the end, too paranoid attention to "production value", so that in software development methods, it is always lagging behind people, which will be subject to people!

Finally, I would like to see my foolish, think about TDD's way, think that TDD is deep, about four kinds of unparalleled power: 1, driving force - driver code writing; 2. Learning - new training camp Optimum teaching materials; 3. Self-confidentiality is minimized with his credit - BUG;

Note: This document is only written by the implementation of TDD, non-actual project code, does not guarantee it to be compiled.

转载请注明原文地址:https://www.9cbs.com/read-57832.html

New Post(0)