PHP in Action

Spaghetti? Ravioli? Farfalle?

dagfinn | 15 July, 2007 01:24

In a blog post by Paul M. Jones, he quotes comments from slashdot claiming that TDD leads to "ravioli code":

The problem is that it [Ravioli Code] tends to lead to functions (methods, etc.) without true coherence, and it often leaves the code to implement even something fairly simple scattered over a very large number of functions.

The problem here is the phrase "tends to". Any reasonable comparison of test-first with test-later has to be between code examples written by the same programmer using the two approaches. Those who write messy code might well continue to do so even with TDD, but in a slightly different way. Even though I do believe TDD helps, it's not "the cure for all diseases".

There is another point which these commenters have missed: when you write the tests first, they are useful to you right from the start. Test-later means something like: write code for a while, then run an ad-hoc test. At that point, you've probably created two or more bugs, and you have to spend time figuring out which is which and where they're located. After you've fixed the bugs, you write some more code, and then you run another ad-hoc test, perhaps re-implementing the code you needed to do the first ad-hoc test. With TDD, you can run tests over and over; you get immediate feedback about bugs and you can fix them quickly

The diagram, which I used in the book, illustrates my point. First, there is TDD, using the tests from the start, then test-later development.

 

 

Share and Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
    blogmarks del.icio.us digg NewsVine Reddit

Comments

Re: Spaghetti? Ravioli? Farfalle?

Travis Swicegood | 15/07/2007, 11:56

To be fair, you should reduce the size of "debugging" in test first. ;-)

There are only two times when there will ever been a bug in code written using TDD: 1) when the original tester made a logical error while writing the test (the tool is only as good as the one implementing it), 2) code was added without proper test coverage (i.e., this if will rarely be hit, so I don't need to write a test for it).

We've had a few bugs crop up in SimpleTest recently. One was just code that didn't have proper test coverage for whatever reason, the other wasn't technically a bug as no expectations had been put in place on how to handle that fringe case.

I agree...

dagfinn | 15/07/2007, 14:57

dagfinn

...when you say "code written using TDD". You're talking about bugs in production code. What I was referring to as "bugs" are the mistakes you made as you write the code. You write something that doesn't work, the test fails, and you fix it. So there are "bugs" that exist for a few minutes at most, and are eliminated before the coding session is finished.

Re: Spaghetti? Ravioli? Farfalle?

Travis Swicegood | 15/07/2007, 17:17

Far enough... You're right, my definition of a bug is something that's been committed, not test cases that have yet to be implemented :-)

Re: Spaghetti? Ravioli? Farfalle?

Paul M. Jones | 15/07/2007, 17:41

From your graphics, it appears you're saying there is no design occurring in test-later development. Is that an accurate reading on my part?

If not, it won't be the first time I've read something wrong. :-)

But if so, the graphics would seem to be in error -- design can certainly happen without testing. I would say that design "before" and design "during" implementation are at least two options that are not represented. (Whether or not such design practice is better or worse than TDD is a separate discussion. ;-)

Re: Spaghetti? Ravioli? Farfalle?

dagfinn | 17/07/2007, 09:52

dagfinn

My intent with the word "design" in the graphics may not be clear in this context. It's just to say that you get mileage from the tests even before you start implementing. This is not an actual "design phase"; it's the process of thinking about the API that you have to do when writing the tests first.

 
Accessible and Valid XHTML 1.0 Strict and CSS
Powered by LifeType - Design by BalearWeb