Re:The Clean Coder: The Craftsman 62, The Dark Path.
by xyzzyz on Monday October 04, 2010 @12:00PM
(#1756092)
I do not say I do not test my code. I just see no purpose in implementing code that I hope will work. I need to believe it will work, and if it does not, I am sure that it is an implementation, not an algorithm issue. The first attempt in the article was just silly, and the second was just wrong. I would not waste time writing these, instead I would use this time to figure out the real solution and then I would implement it.
Here are a few corner cases I can think of for word-wrapping:
1) The line is empty.
2) The line is 1 character shorter/1 character longer/exactly the same length as the wrap limit.
3) The first break point for the line falls before/after/on the wrap limit.
4) The first character(s) of the newly-wrapped line are spaces.
I don't think that I could just eyeball a word-wrap algorithm and know that it handles all these cases correctly. I would, at a minimum, need to check each case. But at that point, I'm just as well off writing test cases, and allowing the computer to verify that my algorithm does the right thing for each. Of course, that's not as good as a proof by induction for each desired property, but it's a _lot_ less work, and it's almost always adequate. And I don't need to worry about mistakes in my proof.
Am I misunderstanding something about your approach? How would you handle this?