2011年2月15日 星期二

Test/Code Cycle in XP

1. Write one test
2. Compile the test. It should fail to compile, as you haven't yet implemented the code that the test calls.
3. Implement just enough to compile.
4. Run the test and see it fail.
5. Implement just enough to make the test pass.
6. Run the test and see it pass.
7. Refactor for clarity and to remove duplication.
8. Repeat from the top.

多做幾次就熟悉了。看文章可以吸收一成知識,聽演講吸收三成,照著做可以吸收七成,舉一反三可以達到九成。舉個例來說,世界上好書那麼多,如果每個人認真閱讀,並且化為實際行動,對全世界才有實際貢獻,世界上嘴砲的人太多了,尤其是台灣。前天曾雅妮登上球后寶座,這就是很好的榜樣,台灣要走出去,光在那邊唬爛,一點用也沒有。

回歸正題,今天再寫個測試,包含物體旋轉測試、組合物體測試等等 (目前還缺反轉重力測試,剛剛偷加果然失敗)。把一些細節補上,測試過了,但 duplicated code 太多,這時候就要 refactor。

例如 copy/paste CreateFixture() 兩次,那我就把這個 method 抓到共用的 header file,給一份實做就好了,將來 CreateFixture() 要改動,就不用改兩份一模一樣的 code,或是漏改其他份 code。以上是簡單的例子,更多例子可以參考 Refactoring: Improving the Design of Existing Code,謝謝大家收看。

沒有留言:

張貼留言