2008-05-07
翻转句子的TDD讨论:)
http://www.javaeye.com/topic/122472?page=1
需求:把字符串"Tdd is a software devolopment technology" 按照单词反转为
"technology devolopment software a is Tdd"
是看了这个帖子后一直有困惑。直接促成了我找了Kent Beck大的Test-Driven Development
By Example来看。自此进入TDD的世界。其实这题目和书后面的那个fibonacci的解法一样。
关键在于:“分解多项式” 且听我忽悠..
ok 目前为止似乎无法继续了。那么继续添加test描述需求:"a b c"-"c b a"
Expected:c b a Actual:b c a
好吧 分析下:bc 反了,bc看做一个整体,再“交换”一次就ok了,即: reverse("b c")+"a"
还是会红,debug发现“切到剩一个词”了就不能切了嘛= =
这下绿了,想想其实prePart其实是reverse(prePart)
另外把那个需求的test加上 看看符合需求否?
最后总结:"a b c"到"c b a"
原来是a b c->[b c] a->[c b] a
需求:把字符串"Tdd is a software devolopment technology" 按照单词反转为
"technology devolopment software a is Tdd"
是看了这个帖子后一直有困惑。直接促成了我找了Kent Beck大的Test-Driven Development
By Example来看。自此进入TDD的世界。其实这题目和书后面的那个fibonacci的解法一样。
关键在于:“分解多项式” 且听我忽悠..
import junit.framework.TestCase;
public class NewStringReverserTest extends TestCase{
//"a b" - "b a"
public void testSimpleReverse(){
assertEquals("b a", reverse("a b"));//从最简单的情况开始
}
private String reverse(String string) {
return "b a";//最“直接”的实现,绿了吧- -!
}
}
public class NewStringReverserTest extends TestCase{
...
private String reverse(String string) {
return "b"+" "+"a";//拆,为什么这么拆? 往下看
}
}
public class NewStringReverserTest extends TestCase{
...
private String reverse(String string) {
//要拆得有意义,注意描述= =
//其实是一个一个拆的,每步都执行test,边绿边拆...小步快跑
String splitToken = " ";
String preWord = "a";
String postWord = "b";
//下面其实是业务逻辑了,看后面的postWord被放在了前面——所谓的交换
return postWord + splitToken + preWord;
}
}
public class NewStringReverserTest extends TestCase{
...
private String reverse(String inputString) {
String splitToken = " ";
//实现切词逻辑
String preWord = inputString.substring(0,inputString.indexOf(splitToken));
String postWord = inputString.substring(inputString.indexOf(splitToken)+1);
return postWord + splitedToken + preWord;
}
}
ok 目前为止似乎无法继续了。那么继续添加test描述需求:"a b c"-"c b a"
public class NewStringReverserTest extends TestCase{
//"a b" - "b a"
public void testSimpleReverse(){
assertEquals("b a", reverse("a b"));
assertEquals("c b a", reverse("a b c"));//红了吧?- -
}
...
}
Expected:c b a Actual:b c a
好吧 分析下:bc 反了,bc看做一个整体,再“交换”一次就ok了,即: reverse("b c")+"a"
private String reverse(String inputString) {
...
return reverse(postWord) + splitToken + preWord;
}
还是会红,debug发现“切到剩一个词”了就不能切了嘛= =
private String reverse(String inputString) {
int tokenIndex = inputString.indexOf(splitToken);
if (tokenIndex == -1)
return inputString;
String prePart = inputString.substring(0, tokenIndex);
String postPart = inputString.substring(tokenIndex + 1);
//描述为prePart,postPart 更符合语义,也是一步重构吧
return reverse(postPart)+ splitToken + prePart
}
这下绿了,想想其实prePart其实是reverse(prePart)
另外把那个需求的test加上 看看符合需求否?
public class NewStringReverserTest extends TestCase {
// "a b" - "b a"
public void testSimpleReverse() {
assertEquals("b a", reverse("a b"));
assertEquals("c b a", reverse("a b c"));
assertEquals("Tdd is a software devolopment technology", reverse("technology devolopment software a is Tdd"));
}
private String reverse(String inputString) {
String splitToken = " ";
int tokenIndex = inputString.indexOf(splitToken);
if (tokenIndex == -1)
return inputString;
String prePart = inputString.substring(0, tokenIndex);
String postPart = inputString.substring(tokenIndex + 1);
return reverse(postPart) + splitToken + reverse(prePart);
}
public void testReverseWithSingleWords() {
assertEquals("a", reverse("a"));
}
public void testReverseWithSplitTokenIn() {
assertEquals(" ", reverse(" "));
assertEquals(" ", reverse(" "));
assertEquals(" a", reverse("a "));
assertEquals("b a", reverse("a b"));
assertEquals("c b a ", reverse(" a b c"));
assertEquals("c b a", reverse("a b c"));
}
}
最后总结:"a b c"到"c b a"
原来是a b c->[b c] a->[c b] a
评论
woods
2008-05-07
My google SVN
# Project:mouse-action
# http://code.google.com/p/mouse-action/
# Non-members may check out a read-only working copy anonymously over HTTP.
svn checkout http://mouse-action.googlecode.com/svn/trunk/ mouse-action-read-only
# Project:mouse-action
# http://code.google.com/p/mouse-action/
# Non-members may check out a read-only working copy anonymously over HTTP.
svn checkout http://mouse-action.googlecode.com/svn/trunk/ mouse-action-read-only
woods
2008-05-07
一直在想最后一步”prePart 其实是reverse(prePart) ”没啥必要= =
今天突然发现,其实这对某些情况很有意义:
reverse(postPart) + splitToken + reverse(prePart);
这不描述了这个问题的实质嘛- -#
完全可以多线程分成多个part来处理,最后再合成嘛- -#
这对提高效率,处理超大文件超有意义- -#
TDD的“副作用”? :-)
今天突然发现,其实这对某些情况很有意义:
reverse(postPart) + splitToken + reverse(prePart);
这不描述了这个问题的实质嘛- -#
完全可以多线程分成多个part来处理,最后再合成嘛- -#
这对提高效率,处理超大文件超有意义- -#
TDD的“副作用”? :-)
发表评论
- 浏览: 1642 次
- 性别:

- 来自: 北京

- 详细资料
搜索本博客
最近加入圈子
最新评论
-
unicode, character, char ...
好吧 我承认我还是很混乱@@~
-- by woods -
Composite+Visitor模式的 ...
visitor的双重分派的确让人混乱- -# 偶开始怀疑他的价值了- -! Ma ...
-- by woods -
Composite+Visitor模式的 ...
这个地方有些困惑:为啥要 node.accept(visitor) ?好像 : ...
-- by woods -
Composite+Visitor模式的 ...
貌似是为了扩展性? 假如要加入一类特殊节点排斥TreePreOrderTrave ...
-- by woods -
翻转句子的TDD讨论:)
My google SVN# Project:mouse-action # h ...
-- by woods






评论排行榜