Today. My topic is TDD again. But why TDD again. I shared some topic about TDD before. And I think I will share TDD more in future.
So let me talk about TDD again.
Today's contents include three part.
- What's TDD
- What Helps did TDD give to me
- Simple Example
TDD's concept is so simple. Test driven develop. Write a test that fails. Make it green. Then refactoring.
But less of people really understand it. You gonna say it is impossible. Let me talk anther thing first.
The sport running. I think every body know this sport. But so many details about running we don't know. What should we do before and after running. What's the better running form. What's your speed and your breathe when you run. How to protect yourself when you run. And so on. I can list many others.
Back to TDD. One day's stand-up meeting. One of teammates said: I completed function A and I tested it works. I will supply unit test for it today.
Absolutely he knows what is TDD. But why he doesn't use TDD.
I list some reasons in below:
- Anxious to coding
- Don't know how to write test without production code
- Don't understand TDD
First reason. When we get one story or some requirement. We always want to implement it fast. There is old Chinese saying: Sharpening your axe will not delay your job of chopping wood. And in fact we didn't save time when we anxious to coding. Because always has bug in our codes. Those bugs take many time to find out and fix.
The second and last reason are same thing. That is we don't understand TDD.
Like running. Before write a test that fails. There are two important steps we ignored.
- Requirement
- Tasks
First, we understand the requirement. Then we split it to many different tasks. If we do these two steps well. Those follow steps will more easier.
I will talk about it via a simple example later.
What helps did TDD give to me?
- Help me understand complicated requirement
Some requirements is very complicated. There are too many case let me feel crazy. But when I start to split requirement to tasks. It is easier to understand the requirement. - Help me focus on one simple case one time.
After I list some tasks. Each time I just need to focus on one case. I can fast and feel easy to finish it. - Let me feel confident after I completed it.
There are many unit test protect my code. Bug is so hard hide in there.
Let's see a simple example.
The requirement is: Give you some unique letters. Please help to list all permutations.
For example: abc. All permutations are: abc acb bac bca cab cba
Maybe you think it is so easy. Take 10 seconds to think about it. What do you gonna to do?
For me. It is not easy to implement it directly. Even I have no idea how to start it.
Back to those steps do TDD.
The requirement is easy. Then I start to list those tasks.
First task.
One simplest case: If you give one letter 'a' and ask me list them. I think it is very easy.
According to this simple case. I write a test that fails.
@Test
public void should_return_a_when_given_one_letter_a() {
// Given
String letters = "a";
// When
List<String> permutations = listAllPermutations(letters);
// Then
assertEquals(singletonList("a"), permutations);
}
And I take five seconds to implement it. But always do not forget refactor after make your test green.
static List<String> listAllPermutations(String letters) {
return singletonList(letters);
}
With that. I list the second task and third task.
...
Final codes: (https://github.com/janipeng/letterPermutation)
class PermutationUtil {
static List<String> listAllPermutations(String letters) {
if (letters.length() == 1) {
return singletonList(letters);
}
List<String> permutations = new ArrayList<String>();
for (int index = 0; index < letters.length(); index++) {
for (String permutation : listAllPermutations(subtractOneCharByIndex(letters, index))) {
permutations.add(letters.charAt(index) + permutation);
}
}
return permutations;
}
private static String subtractOneCharByIndex(String letters, int index) {
return letters.substring(0, index) + letters.substring(index + 1);
}
}
Like running. If we want really understand TDD. Keep running.
End
Email Address: jani.peng@qq.com