Geek Smithology

April 5, 2006

TDD: Rocky Lhotka doesn’t get it

Filed under: Craft of Dev by Nathan @ 10:12 pm

I detest what you say, but will defend to the death your right to say it. – Hall

…but you’re wrong. – Smith

I enjoy debate. I like comparative discussions about technologies, and learning about how people think and how they work. And I can generally sense that point at which the only solution is to agree to disagree.

But when somebody who is clearly misguided starts spouting off about something they obviously have not investigated in a forum where they can have undue influence, I get a little belligerent. You know how it gets: blood spilled, oaths sworn, composure lost.

I heard this during my commute and I was so infuriated I had to write something. In Episode 169 of the .NET Rocks! podcast, guest Rocky Lhotka spews more misinformation about Test Driven Development (TDD) in four minutes than an hour’s worth of MSDN articles.

Shall we?

Rocky: My major critique, really, is that it I think comes out of a different world from most of us that have spent many years working in the Microsoft space. I think a world where the tools that we’re used to, that we just take for granted, haven’t existed.

Haven’t existed? I’m currently developing in C#, but I come from the Java space. They have IDEs that have long since eclipsed Visual Studio as the brightest star in the IDE firmament.

Rocky: So test driven development comes from the premise that you don’t have Intellisense and all of these other cool things, most of which we lose if we sit down to start writing tests before we start writing code.

In my experience, Intellisense is useless exactly once during the TDD cycle: when the method doesn’t exist. But naming that method is a design decision and I’m comfortable with no Inellisense. It turns out that when you’re calling a method from within a test, there is a subconscious mental shift towards designing for usability rather than writability. A testable method is a callable method. Method names stay sensible, parameter lists make sense, and side effects are minimized. Oh, and after I type the name, I just press Alt-Shift-F10 and generate the stub.

Rocky: I think it’s really a subtle ordering of events. If you sit down and say I’m going to write all my tests first. I’m going to write a bunch of code to test things that don’t exist. Well, this is true, but on a larger system I’m not convinced that that’s overly realistic.

Nobody doing TDD writes all their tests first. You write a failing test. You pass the test with the minimum amount of code possible, then you write the next test. Generally you’ll be writing 5-10 lines to test things that don’t exist. But then you make it exist. That’s the point. As for larger systems, TDD enforces modularity and clean tier separation. So it’s still a good idea.

After this, one of the hosts puts his foot so far into his mouth it comes out the back of his head…

DNR: I’m almost seeing that the antithesis of test driven development is this fast refactor cycle that we get into writing code where I write a bit of code, I look how it behaves, I write a bit more code, then I refactor it to consolidate some functionality and then do it again…something that really fights against test driven development.
Rocky: Interestingly, at least in theory, both ideas come from the same space (Agile) so you wouldn’t think they’d fight, but they do.
DNR: Because if I’ve gone through the effort of developing that test so that I have a standardized behaviour, I’m not willing to refactor it and break my tests.

I’m only willing to refactor if I have a comprehensive set of unit tests to back me up. If I break a test, it means I broke the system.

What happens if you refactor and there are no tests to break? Maybe you didn’t break anything. Maybe you did. Maybe your customer will find out for you. Refactoring without unit tests is the software equivalent of Russian Roulette. Remember the Deer Hunter, my friends.

Remember the Deer Hunter.

Leave a Reply

Your email address will not be published. Required fields are marked *

 

Powered by WordPress