Advanced TDD Word Wrap Example

143 views
Skip to first unread message

Philip Morris

unread,
Jul 16, 2013, 12:12:00 PM7/16/13
to clean-code...@googlegroups.com
Hi Uncle Bob,

I noticed during the word wrap algorithm when calculating the break point you started the search for the last index from the width s.lastIndexOf(" ",width); but you had no test case for this. You could have made all the test cases pass simply with a s.lastIndexOf(" "); and wouldn't have noticed anything was wrong until you added the acceptance test.

I mentioned this as I find I often fall into the trap of adding behaviour just because I know it's right rather than letting the tests guide me. I've noticed it's something junior developers or developers with little TDD experience tend to pick up on. It's nice that a pair can be your conscious from time to time.

Anyone else have similar experiences or advice?

Uncle Bob

unread,
Jul 17, 2013, 8:50:31 AM7/17/13
to clean-code...@googlegroups.com


On Tuesday, July 16, 2013 11:12:00 AM UTC-5, Philip Morris wrote:
Hi Uncle Bob,

I noticed during the word wrap algorithm when calculating the break point you started the search for the last index from the width s.lastIndexOf(" ",width); but you had no test case for this. You could have made all the test cases pass simply with a s.lastIndexOf(" "); and wouldn't have noticed anything was wrong until you added the acceptance test.

That's a good point.  I've done this kata so many times that I just leap to the lastIndexOf(" ", width)  without really thinking about it.  Now the question is, is that the right choice for this test?  

Making a test pass is not a blind operation.  If you know the requirements of the system, then you choose the simplest mechanism to pass the test that does not contradict those requirements.  Does "lastIndexOf(" ") contradict the requirements?  Based on my familiarity with the problem it does; because I already know that the line length must be 'width' or less.  

When making tests pass we don't make willful errors in logic so long as there are obvious choices that don't have such an error.  lastIndexOf(" ") is such a willful error.  We know it might return an index greater than 'width', so we know, for certain, that it's wrong; and we know that lastIndexOf(" ", width) will always return something less than or equal to width.  

Having said that, in order to make a willful error you have to know it is an error in the first place.  In TDD we make lots of inadvertent errors because we want the current test to pass, and we can't implement the whole algorithm in one shot. So we take a step that we know to be logically incomplete; because we aren't sure how to complete it.  However, we don't take steps that we know to be logically incorrect.  

That's the real difference.  Logically incorrect vs. logically incomplete.    

Now I should rewrite this answer and shorten it up.  I apologize for not doing that, but I've just run out of tim

 
Reply all
Reply to author
Forward
0 new messages