12

Similar to Does TDD mean not thinking about class design?, I am having trouble thinking about where the traditional 'design' stage fits into TDD.

According to the Bowling Game Kata (the 'conversation' version, whose link escapes me at the moment) TDD appears to ignore design decisions made early on (discard the frame object, roll object, etc). I can see in that example it being a good idea to follow the tests and ignore your initial design thoughts, but in bigger projects or ones where you want to leave an opening for expansion / customisation, wouldn't it be better to put things in that you don't have a test for or don't have a need for immediately in order to avoid time-consuming rewrites later?

In short - how much design is too much when doing TDD, and how much should I be following that design as I write tests and the code to pass them (ignoring my design to only worry about passing tests)?

Or am I worrying about nothing, and code written simply to follow tests is not (in practice) difficult to rewrite or refactor if you're painted into a corner? Alternatively, am I missing the point and that I should be expecting to rewrite portions of the code when I come to test a new section of functionality?

4 Answers 4

10

I would base your tests on your initial design. In many ways TDD is a discovery process. You can expect to either confirm your early design choices or find that there are better choices you can make. Do as much upfront design as you are comfortable with. Some like to fly by the seat of the chairs doing high level design and using TDD to flesh the design out. While others like to have everything on paper first.

Part of TDD is refactoring.

2
  • 3
    +1 for 'Part of TDD is refactoring': The design will need to be updated to reflect the changes found necessary while testing. Aug 16, 2010 at 16:32
  • Thanks then - so I am missing the point, and the refactoring / rewriting is all part of this emergent design. Good to know, now to try it out with something! Aug 19, 2010 at 7:31
10

There is something to be said about 'Designing Big Complex Systems' that should not be associated with TDD - especially when TDD is interpreted as 'Test Driven Design' and not 'Test Driven Development'.

In the context 'Development', using TDD will ensure you are writing testable code which give all the benefits cited about TDD ( detect bugs early, high code:test coverage ratio, easier future refactoring etc. etc.)

But in 'Designing' large complex systems, TDD does not particularly address the following concerns that are inherent in the architecture of the system

  1. (Engineering for) Performance
  2. Security
  3. Scalability
  4. Availability
  5. (and all other 'ilities')

(i.e. all of the concerns above do not magically 'emerge' through the "write a failing test case first, followed by the working implementation, Refactor - lather, rinse, repeat..." recipe).

  • For these, you will need to approach the problem by white-boarding the high-level and then low-level details of a system with respect to the constraints imposed by the requirements and the problem space.

  • Some of the above considerations compete with each other and require careful trade-offs that just don't 'emerge' through writing lots of unit tests.

  • Once key components and their responsibilities are defined and understood, TDD can be used in the implementation of these components. The process of Refactoring and continually reviewing/improving your code will ensure the low-level design details of these components are well-crafted.

I am yet to come across a significantly complex piece of software (e.g. compiler, database, operating system) that was done in a Test Driven Design style. The following blog article talks about this point extremely well (Compilers, TDD, Mastery)

Also, check the following videos on Architecture which adds a lot of common sense to the thought process.

2
  • Actually I am developing a clone of Asp.Net MVC stack, similar to vNext but based on coroutines, and I am getting mad trying to test everything. Actually I am mostly doing integration tests and unit tests only for the smallest component, like for the rendering or razor pages or the roouting handler. Being too a multithreaded environment I prepared a kind of "fake thread" to run all the various parts step by step. I know that I am not doing strict TDD but would be IMHO kinda impossibile for a "one man band".
    – Kendar
    Oct 23, 2014 at 8:25
  • By far, the most sensible answer to the question. Advocates of TDD evangelize: "Write tests first!"... but... how can you write tests if you don't have anything, not even the slightest idea about what needs to be done? You will be writing correct tests which solve the wrong problem or do not solve anything needed at all! First understand the problem domain, write a prototype inside Jupyter for example, evaluate its performance, memory footprint, scaleability and other requirements; if needed, write a proof of concept; then embrace TDD with such knowledge you gained in the prototyping phase. Feb 9, 2017 at 22:54
3

Start with a rough design idea, pick a first test and start coding, going green test after test, letting the design emerge, similar or not to the initial design. How much initial design depends on the problem complexity.

One must be attentive and listen to and sniff the code, to detect refactoring opportunities and code smells.

Strictly following TDD and the SOLID principles will bring code clean, testable and flexible, so that it can be easily refactored, leveraging on the unit tests as scaffolding to prevent regression.

3
  • So the initial design is more there in case the emergent design runs into a brick wall (or looks as if it's about to)? Aug 16, 2010 at 16:24
  • 1
    The initial design is to get started, to give the first direction, for the problem - or a subpart of the problem. The design is created incrementally.
    – philant
    Aug 16, 2010 at 16:52
  • "Writing test first" without having the slightest idea about what needs to be done ends up producing correct tests for the wrong problem. First of all it's necessary to understand the problem domain and architectural requirements, cos we are talking about complex problems, then employ small prototypes in order to feed your imagination with ideas and put these ideas under scrutiny. You just need a prototyping tool such as IPython or Jupyter in most cases or a proof of concept in more elaborate cases, aiming to validate enterprise requirements. Finally you write code and write tests for it. Feb 9, 2017 at 23:02
2

I've found three ways of doing design with TDD:

  • Allow the design to emerge naturally as duplication and complexity is removed
  • Create a perfect design up-front, using mocks combined with the single responsibility principle
  • Be pragmatic about it.

Pragmatism seems to be the best choice most times, so here's what I do. If I know that a particular pattern will suit my problem very well (for instance, MVC) I'll go straight for the mocks and assume it works. Otherwise, if the design is less clear, I'll allow it to emerge.

The cross-over point at which I feel the need to refactor an emergent design is the point at which it stops being easy to change. If a piece of code isn't perfectly designed, but another dev coming across it could easily refactor it themselves, it's good enough. If the code is becoming so complex that it stops being obvious to another dev, it's time to refactor it.

I like Real Options, and refactoring something to perfection feels to me like committing to the design without any real need to do so. I refactor to "good enough" instead; that way if my design proves itself to be wrong I've not wasted the time. Assume that your design will be wrong if you've never used it before in a similar context.

This also lets me get my code out much more quickly than if it were perfect. Having said that, it was my attempts to make the code perfect that taught me where the line was!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.