Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TDD with unclear requirements

I know that TDD helps a lot and I like this method of development when you first create a test and then implement the functionality. It is very clear and correct way.

But due to some flavour of my projects it often happens that when I start to develop some module I know very little about what I want and how it will look at the end. The requirements appear as I develop, there may be 2 or 3 iterations when I delete all or part of the old code and write new.

I see two problems: 1. I want to see the result as soon as possible to understand are my ideas right or wrong. Unit tests slow down this process. So it often happens that I write unit tests after the code is finished what is known to be a bad pattern. 2. If I first write the tests I need to rewrite not only the code twice or more times but also the tests. It takes much time.

Could someone please tell me how can TDD be applied in such situation?

Thanks in advance!

like image 914
Andrey Minogin Avatar asked Nov 29 '09 11:11

Andrey Minogin


People also ask

What are the 3 stages of TDD?

Red, Green and Refactor is the three phase of Test Driven Development and this the sequence that get followed while writing code. When followed, this order of steps helps ensure that you have tests for the code you are writing and you are writing only the code that you have to test for.


4 Answers

I want to see the result as soon as possible to understand are my ideas right or wrong. Unit tests slow down this process.

I disagree. Unit tests and TDD can often speed up getting results because they force you to concentrate on the results rather than implementing tons of code that you might never need. It also allows you to run the different parts of your code as you write them so you can constantly see what results you are getting, rather than having to wait until your entire program is finished.

like image 56
Mark Byers Avatar answered Sep 23 '22 20:09

Mark Byers


I find that TDD works particularly well in this kind of situation; in fact, I would say that having unclear and/or changing requirements is actually very common.

I find that the best uses of TDD is ensuring that your code is doing what you expect it to do. When you're writing any code, you should know what you want it to do, whether the requirements are clear or not. The strength of TDD here is that if there is a change in the requirements, you can simply change one or more of your unit tests to reflect the changed requirements, and then update your code while being sure that you're not breaking other (unchanged) functionality.

I think that one thing that trips up a lot of people with TDD is the assumption that all tests need to be written ahead of time. I think it's more effective to use the rule of thumb that you never write any implementation code while all of your tests are passing; this simply ensures that all code is covered, while also ensuring that you're checking that all code does what you want it to do without worrying about writing all your tests up front.

like image 30
Gabriel Reid Avatar answered Sep 22 '22 20:09

Gabriel Reid


IMHO, your main problem is when you have to delete some code. This is waste and this is what shall be addressed first.

Perhaps you could prototype, or utilize "spike solutions" to validate the requirements and your ideas then apply TDD on the real code, once the requirements are stable.

The risk is to apply this and to have to ship the prototype.

Also you could test-drive the "sunny path" first and only implement the remaining such as error handling ... after the requirements have been pinned down. However the second phase of the implementation will be less motivating.

What development process are you using ? It sounds agile as you're having iterations, but not in an environment that fully supports it.

like image 23
philant Avatar answered Sep 19 '22 20:09

philant


TDD helps you to express the intent of your code. This means that writing the test, you have to say what you expect from your code. How your expectations are fulfilled is then secondary (this is the implementation). Ask yourself the question: "What is more important, the implementation, or what the provided functionality is?" If it is the implementation, then you don't have to write the tests. If it is the functionality provided then writing the tests first will help you with this.

Another valuable thing is that by TDD, you will not implement functionality that will not be needed. You only write code that needs to satisfy the intent. This is also called YAGNI (You aint gonna need it).

like image 42
paweloque Avatar answered Sep 19 '22 20:09

paweloque