TDD Again

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子扫俺,更是在濱河造成了極大的恐慌监署,老刑警劉巖钠导,帶你破解...
    沈念sama閱讀 222,000評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件舷夺,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡礼华,警方通過查閱死者的電腦和手機(jī)勺馆,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,745評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門戏售,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人草穆,你說我怎么就攤上這事灌灾。” “怎么了悲柱?”我有些...
    開封第一講書人閱讀 168,561評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵锋喜,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我豌鸡,道長(zhǎng)跑芳,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,782評(píng)論 1 298
  • 正文 為了忘掉前任直颅,我火速辦了婚禮,結(jié)果婚禮上怀樟,老公的妹妹穿的比我還像新娘功偿。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,798評(píng)論 6 397
  • 文/花漫 我一把揭開白布械荷。 她就那樣靜靜地躺著共耍,像睡著了一般。 火紅的嫁衣襯著肌膚如雪吨瞎。 梳的紋絲不亂的頭發(fā)上痹兜,一...
    開封第一講書人閱讀 52,394評(píng)論 1 310
  • 那天,我揣著相機(jī)與錄音颤诀,去河邊找鬼字旭。 笑死,一個(gè)胖子當(dāng)著我的面吹牛崖叫,可吹牛的內(nèi)容都是我干的遗淳。 我是一名探鬼主播,決...
    沈念sama閱讀 40,952評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼心傀,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼屈暗!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起脂男,我...
    開封第一講書人閱讀 39,852評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤养叛,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后宰翅,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體弃甥,經(jīng)...
    沈念sama閱讀 46,409評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,483評(píng)論 3 341
  • 正文 我和宋清朗相戀三年堕油,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了潘飘。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,615評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡掉缺,死狀恐怖卜录,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情眶明,我是刑警寧澤艰毒,帶...
    沈念sama閱讀 36,303評(píng)論 5 350
  • 正文 年R本政府宣布,位于F島的核電站搜囱,受9級(jí)特大地震影響丑瞧,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜蜀肘,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,979評(píng)論 3 334
  • 文/蒙蒙 一绊汹、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧扮宠,春花似錦西乖、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,470評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)薄腻。三九已至,卻和暖如春届案,著一層夾襖步出監(jiān)牢的瞬間庵楷,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,571評(píng)論 1 272
  • 我被黑心中介騙來泰國(guó)打工楣颠, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留尽纽,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 49,041評(píng)論 3 377
  • 正文 我出身青樓球碉,卻偏偏與公主長(zhǎng)得像蜓斧,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子睁冬,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,630評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容