Coding dojo: First Time

Source: Internet
Author: User
On November 10/23, I conducted an internal study within the Department and conducted a TDD development drill using the currently popular coding dojo method. The drill questions are as follows: for the introduction of the coding dojo, please Baidu and I will not introduce it more. From the perspective of the effect, it basically achieves the purpose of conveying the development method of TDD. In particular, from the initial focus on how to implement this program, how to design algorithms, and gradually to first think about how to test, from the simplest implementation, finally evolved into the final design. Of course, so far, the participants have only understood the development method of TDD and cannot talk about how to use TDD for development. This requires a longer period of self-training and use. The most important thing with TDD is the change of thinking methods. First of all, we must firmly believe that all programs can be tested. If we cannot test them, it is not caused by product features, but by insufficient capabilities, and there is a design problem. Therefore, the design must be changed to make the program measurable. For example, it is generally considered that the display of the curve is normal, and automatic testing cannot be used. Another angle: If the display is only a data-coordinate conversion, the focus of the test is to check whether the data is correct, which is completely measurable. Second, although global considerations are required, we should start with a simple and evolutionary design. This point is evident in this dojo drill. At the beginning of this dojo, many people thought that a certain algorithm should be established to display the required characters. So the first function is: void displaysegmentdigital (string input), and then write those subfunctions. How to test this function? This is a function output to the screen. It can only be judged by eyes. It is obviously not suitable for automatic testing or unit testing. Therefore, TDD is not a method to first implement the framework and then implement specific functions. Output to the screen is only the last process, but also a simple process, so it does not have to be the focus. What is the data displayed? Therefore, the function is changed to: String displaysegmentdigital (string input ). At this time, this function is no longer output to the screen, but to output a string, and then output to the screen by another string display function. This function became measurable. Therefore, the first test function is written:
String strtext = "910"; string strtextresult = ". _. | _ | .. | ..... | .. |. _. |. | _ | "; string stroutput = digitalsegment. displaysegmentdigital (strtext); assertequals (stroutput, strtextresult );
The first test passed successfully, because the implementation is very simple:
Public String displaysegmentdigital (string strtext) {return ". _. | _ |... |. _. |. | _ | ";}
The next difficulty is: what is the next test? Test "3456" output? OK. Let's try it first, so we want to write the second test:

String strtext = "3456"; string strtextresult = "???????? ";

The question goes on: what is the question mark? Does this test really make sense? Almost all people think that there is a problem. After a brief discussion, the conclusion is that the display of each number should be tested, rather than a string. Therefore, the test becomes:

String strtext = "9"; string strtextresult = ". _. | _ |... | ";

String stroutput = digitalsegment. displaysegmentdigital (strtext );

Assertequals (stroutput, strtextresult );

Implementation changes:

Public String displaysegmentdigital (string strtext ){

If (strtext = "9 ")

Return ". _. | _ |... | ";

Else

Return NULL;

}

After implementation, test:

Strtext = "1"; strtextresult = "... | ";

Stroutput = digitalsegment. displaysegmentdigital (strtext );

Assertequals (stroutput, strtextresult );

The implementation also changes:

Public String displaysegmentdigital (string strtext ){

String [] strresult = new string [10];

Strresult [0] = ". _. |. | _ | ";

Strresult [1] = "... | ";

Strresult [9] = ". _. | _ |... | ";

Return strresult [integer. parseint (strtext)];

}

At this point, it is clear that our algorithms are born naturally. It may not be the same as the algorithm design that many people start with, but it should not be the same :). This is TDD evolution design.

But there is a problem. _. | _ |... | what is it? How can I know that the final output is correct. Therefore, we have slightly changed the Syntax:

String strtext = "9"; string strtextresult = ". _." +
"| _ |" +
"... | ";

String stroutput = digitalsegment. displaysegmentdigital (strtext );

Assertequals (stroutput, strtextresult );

Strtext = "1 ";

Strtextresult = "..." +

"... |" +

"... | ";

Stroutput = digitalsegment. displaysegmentdigital (strtext );

Assertequals (stroutput, strtextresult );

Implementation changes:

Public String displaysegmentdigital (string strtext ){

String [] strresult = new string [10];

Strresult [0] = ". _." +

"|. |" +

"| _ | ";

Strresult [1] = "..." +

"... |" +

"... | ";

Strresult [9] = ". _." +

"| _ |" +

"... | ";

Return strresult [integer. parseint (strtext)];

}

It is much more intuitive now.

Why do we need to be intuitive? The purpose is not just for the ease of coding of the program, but more importantly, the test should not copy the implementation code, and the Implementation should not copy the test code, otherwise, the consequences will be serious. When writing test code, you must write the test code with the testing mentality and the user's mentality instead of thinking about it. In this case, TDD fails. This is also why TDD requires that the test code be written first and then the implementation code be written. Because once we first think about implementation, the next test will inevitably follow the implementation logic, thus violating the principle of "test independence. Implementation errors cannot be found during testing.

Next, we need to sort out the code. Obviously, the function name displaysegmentdigital is not so accurate. Later, we talked about it and came up with the name getdigitaldisplaycontent. After that, I think getdigitalfont may be better.

Okay, I will write it here for the first time, leaving a small problem: we have not designed any display algorithms so far. How can we improve the current design to facilitate output?

Coding dojo: First Time

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.