Alex Rivera | Logout

Does TDD mean not thinking about class design?

Asked 2010-01-27T18:29:55.277
10

I am making a role playing game for fun and attempting to use TDD while developing it. Many of the TDD examples I see focus on creating the test first, then creating objects that are needed to get the test to pass.

For example:

[Test]
public void Character_WhenHealthIsBelowZero_IsDead()
{
   // create default character with 10 health
   Character character = new Character();
   character.SubtractHealth(20);
   Assert.That(character.IsAlive, Is.EqualTo(false));
}

So based on this I'll create the character class and appropriate properties/methods. This seems fine and all but should my class design really come out of continually refining my tests? Is that better than mapping out the possible objects that my game will need ahead of time? For example, I would normally think of a base Character class, then subclasses such as Wizard, Fighter, Theif.

Or is a balanced approach the way to go? One where I map out the possible classes and hierarchy I'll need but writing tests first to verify they are actually needed?

Edit
Report

2 Answers

6

Should my class design really come out of continually refining my tests?

Yes.

Does TDD mean not thinking about class design?

Absolutely not. It means thinking about class design in the course of writing your tests, and the rest of your code. Class design is at play throughout the Red-Green-Refactor TDD life cycle.

answered 2010-01-27T18:41:25.110
0

TDD is about design! So doing TDD you will be sure that your design is completely testable.

Also, the "balanced approach" is the way to go IMO. You already know a high level architecture and TDD will drive this architecture to be testable.

EDIT: Although, if you want just to practice TDD I would not do the "balanced approach". I would just do TDD with "baby steps" and maybe coding Dojos

answered 2010-01-27T18:36:43.323

Your Answer