TDD is BS?
Jacob Carlborg
doob at me.com
Thu Jun 20 02:49:21 PDT 2013
On 2013-06-19 23:54, Walter Bright wrote:
> TDD strikes me as an ad-hoc way of constructing code, and is a poor
> substitute for thinking about the problem as a whole. For example, I
> don't really see how getting a square root function to pass its test
> cases is going to lead one to implementing one of the classic algorithms
> for computing square roots. I don't see how TDD for a compiler will lead
> one to rediscover what is known about the best way to organize the logic
> of a compiler.
>
> TDD is "curve fitting" which is what one does when one has no
> understanding.
I have not watch the video(s) but I think you completely failed to
understand what TDD is about. This is how I think about TDD:
1. You have a problem to solve
2. You think about how to solve the problem and how a design can look like
3. You come up with a design
4. Write a test according to the design
5. Write the implementation
6. Run the test to see that the implementation matches your design
7. Repeat 2-6 until satisfied
Example, write a compiler:
Problem: Lex string literals
Test:
auto code = `"asd"`;
auto token = lex(code);
assert(token.lexeme == code);
assert(token.type == Type.stringLiteral);
Implementation:
Write the implementation to pass the above test.
Token lex (string code)
{
return Token(code, Type.stringLiteral);
}
This is obviously a dummy implementation but it's enough to pass the
test. The you need write more tests that force you to implement the
function correctly. Like below.
Test again:
auto code = `"foo""bar"`;
auto token = lex(code);
assert(token.lexeme == `"foo"`);
assert(token.type == Type.stringLiteral);
Fix the implementation to pass the above test.
Test again:
auto code = `"foo`;
auto token = lex(code);
assert(token.type == Type.error); // unterminated string literal
Fix the implementation to pass the above test.
Usually I write the test first and immediately write the "correct"
implementation instead of these dummy implementation. Then I add some
more tests which possibly will fail because I found an edge case I
forgot to think about when I implemented the code.
BTW, when you don't know how to implement something and you're trying
your way forward. You can write proper tests instead of using printf or
similar.
--
/Jacob Carlborg
More information about the Digitalmars-d
mailing list