TDD Again

2018-09-11  本文已影响0人  贾尼

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.

image.png

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.

image.png

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:


image.png

Like running. Before write a test that fails. There are two important steps we ignored.

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.


image.png

I will talk about it via a simple example later.

What helps did TDD give to me?

image.png

Let's see a simple example.

image.png

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

上一篇 下一篇

猜你喜欢

热点阅读