TDD Again

作者: 贾尼 | 来源:发表于2018-09-11 13:04 被阅读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.

  • What's TDD
  • What Helps did TDD give to me
  • Simple Example
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
  • 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.


image.png

I will talk about it via a simple example later.

What helps did TDD give to me?

image.png
  • 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.

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

相关文章

  • TDD Again

    Today. My topic is TDD again. But why TDD again. I shared...

  • 深度解读 - TDD(测试驱动开发)

    本文结构: 什么是 TDD 为什么要 TDD 怎么 TDD FAQ 学习路径 延伸阅读 什么是 TDD TDD 有...

  • TDD和BDD

    TDD(Test-Driven Development)——测试驱动开发 1.为什么使用TDD: 1)TDD根据客...

  • 初识TDD

    什么是TDD 本文所说的 TDD 指狭义上的 TDD,也就是「单元测试驱动开发」。 TDD 是敏捷开发中的一项核心...

  • 为什么TDD很难在项目上推动?

    经常在TDD训练营中有学员提这个问题:学了TDD,在项目上也没法落地,为什么TDD很难在项目上推动? TDD本身就...

  • 轻松TDD之旅

    TDD简介 TDD是什么 TDD一般是Test Driven Development(测试驱动开发)的缩写,它以测...

  • AppRTC Configuration On Ubuntu 1

    I tried again and again and again to successfully configu...

  • 认识 TDD

    什么是TDD? TDD 有广义和狭义之分,常说的是狭义的 TDD,也就是 UTDD(Unit Test Drive...

  • 测试驱动开发(TDD)总结——原理篇

    标签 | TDD Java 测试驱动开发(TDD)总结——原理篇

  • 《Begin Again》,again,again

    为什么这部豆瓣评分8.5的电影我会看了一遍又一遍,已经三遍了,我自己也很好奇想再看一遍捋捋清楚。 最初发现这部电影...

网友评论

      本文标题:TDD Again

      本文链接:https://www.haomeiwen.com/subject/nyvjgftx.html