iT邦幫忙

2018 iT 邦幫忙鐵人賽
DAY 6
0
自我挑戰組

TDD - 紅燈,綠燈,重構,30天 TDD之路有你有我系列 第 6

Day6. 繼續牛刀小試的概念-Codewars_Fake Binary

今天一樣從Codewars的8kyu開始唄。

這一次的題目一樣是隨機出來的,反正就隨便挑了一題啦XDDD

廢話不多說就來看看題目吧,今天的題目非常簡單(廢話 因為是8kyu啊XD

https://ithelp.ithome.com.tw/upload/images/20171223/20107209MfW7YZoLMl.png

今天的題目需求很簡單,只要把大於5的數字改成1,小於5的數字改成0,形成一個「假二元」的概念XDDD

https://ithelp.ithome.com.tw/upload/images/20171223/20107209o2hNZsGCbD.jpg

接下來要拆解需求啦

  1. 把大於5的數字換成1
  2. 把小於5的數字換成0

接下來開始實作吧! 把專案創建好之後,就把第一個測試案例名稱寫出來吧!
我們要從最簡單的第一步開始,老樣子,如果覺得一開始有點卡卡的就可以加入3A原則的註解唷!

https://ithelp.ithome.com.tw/upload/images/20171223/20107209ykG1LaKLkv.png

接下來就可以寫Code,一開始Test Case中的Code會長這樣

https://ithelp.ithome.com.tw/upload/images/20171223/20107209EAyeD4KetH.png

而Production Code 就長這樣

https://ithelp.ithome.com.tw/upload/images/20171223/20107209cQuqWQtbHe.png

接下來就跑一下測試,理所當然的會Fail,如果過了就見鬼了,這裡就可以看見我們的第一個紅燈,這時候來Commit一下吧

https://ithelp.ithome.com.tw/upload/images/20171223/20107209NJBmumfqlh.png

接下來把Production Code改一下吧,用最小步伐讓Test Case Pass!
對,你沒看錯,就是這個樣子ˊ_>ˋ 反正我們已經可以讓第一個test case過惹

https://ithelp.ithome.com.tw/upload/images/20171223/20107209no89QZaEEx.png

接下來跑個測試,這個沒過的話我會很驚訝XD

https://ithelp.ithome.com.tw/upload/images/20171223/20107209SGOL9Cpcnt.png

再開始寫第二個test case之前來Commit一下吧!

現在寫了第二個test case,這一支test case會讓我們需要印出0的概念!

https://ithelp.ithome.com.tw/upload/images/20171223/20107209Sg7XgKX3jB.png

接下來跑一下測試吧,一定是紅燈!

https://ithelp.ithome.com.tw/upload/images/20171223/20107209AuevOnEiSI.png

開始改之前可以先commit一下
接下來用最簡單的方式改Production Code吧!

https://ithelp.ithome.com.tw/upload/images/20171223/20107209dXGKTHfSy0.png

對,你依然沒看錯XD 就是這樣,反正可以過test case啊!!!
沒錯,他綠燈了,這時候Commit一下唄

https://ithelp.ithome.com.tw/upload/images/20171223/20107209KdqYwGmbHg.png

接下來審視一下test code吧!
因為test code 也是我們的code 之後也是需要維護的!

Tips: 如果你有Resharper 那就用Ctrl +R + I讓他inline解決吧XD

https://ithelp.ithome.com.tw/upload/images/20171223/20107209FG3650gLVz.png

改完test code之後
跑一下測試,all pass之後就commit唄

接下來寫第三個test case吧!
第三個test case要思考一下,怎樣才能夠在「最小」的步伐內完成的測試案例,於是就產生了第三個測試案例了

https://ithelp.ithome.com.tw/upload/images/20171223/201072090nSdS5Eud3.png

第三個寫出來了,很明顯的,他一定會錯,如果他對了就見鬼了

https://ithelp.ithome.com.tw/upload/images/20171223/20107209InZ3z9xGe0.png

接下來就來改Production Code吧!!!

https://ithelp.ithome.com.tw/upload/images/20171223/20107209RUpwBKacfM.png

改好之後來跑個測試吧! 他過了,雖然code有點醜,不過至少他過了!

https://ithelp.ithome.com.tw/upload/images/20171223/20107209Qu4FmOF631.png

接下來寫第4個test case吧!
這時候其實我發現,好像第三個測試案例輸入兩個小於5的字串會比較好,比如說00,或是14之類的數字。
不過沒關係,我們就先把第三個測試案例取消然後來寫第4個測試案例吧!

取消那個測試案例只要把那個測試案例上頭的Attribute拿掉就可以了

https://ithelp.ithome.com.tw/upload/images/20171223/20107209Bgk0pRjSpq.png

現在就fail了第四個測試案例,然後來commit 一下吧

https://ithelp.ithome.com.tw/upload/images/20171223/201072099jIZtpe0Ll.png

接下來我們來改Production Code唄!

https://ithelp.ithome.com.tw/upload/images/20171223/20107209g2rQ48oyv8.png

跑個測試! Pass!!

https://ithelp.ithome.com.tw/upload/images/20171223/20107209TELr0R4pRm.png

接下來Commit一下,再來回頭審視一下Production Code,摁…似乎可以用Linq的寫法不過需求還尚未完成就先不改了吧
再來就把第三個測試案例召喚回來吧!!

https://ithelp.ithome.com.tw/upload/images/20171223/2010720990jgYfywVI.png

召喚回來之後就跑個測試,然後commit一下吧!

https://ithelp.ithome.com.tw/upload/images/20171223/20107209Bl2cgXjfgh.png

接下來就改一下Production Code唄!

https://ithelp.ithome.com.tw/upload/images/20171223/20107209myTzLyPcrx.png

跑個測試,Pass!

https://ithelp.ithome.com.tw/upload/images/20171223/20107209fUJ4ZR7dS0.png

來commit一下,然後我聞到味道了!!
可以把if else這串抽出來!
就變成這個樣子,然後跑個測試,pass,commit!

https://ithelp.ithome.com.tw/upload/images/20171223/20107209bTLI9la1SJ.png

這樣的話就很方便了,可以把foreach抽成linq語法

https://ithelp.ithome.com.tw/upload/images/20171223/20107209yQ5IHvkMWd.png

跑個測試,Pass,Commit!

基本上現在的測試已經可以涵蓋所有輸入了,不過我這個人是謹慎的人所以我們來多寫幾個測試案例吧!!!

https://ithelp.ithome.com.tw/upload/images/20171223/20107209iNZy7JxXRb.png

跑過測試都All Pass 之後就再Commit一次吧!

然後再Codewars上提交! Pass!!

https://ithelp.ithome.com.tw/upload/images/20171223/201072090mUQcuIs3j.png

通過! 接下來我最喜歡的部分就是去看看別人寫這題寫得如何XD

https://ithelp.ithome.com.tw/upload/images/20171223/20107209UdmdErc37m.png

看到這個人寫就發現我忽略了還有string.Concat這個方法啊…
而且比我的還要簡潔(跪地
我的linq又臭又長,為了改而改的感覺,感覺不改都比較好看呢O_O

看來下一次遇到要累加字串的部分可以試試Concat這個方法呢(我才不會說當初refactor才沒有依賴reSharper呢 亨~
下一題,明天見!

喜歡的可以到這個 github url 看看 commit 紀錄
https://github.com/SQZ777/Codewars_FakeBinary.git


上一篇
Day5. 套路的開始啦-Codewars_Remove First and Last Character
下一篇
Day7. 找黃金囉!-Codewars_Main in the west
系列文
TDD - 紅燈,綠燈,重構,30天 TDD之路有你有我30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言