Member-only story
The three rules of TDD

According to Uncle bob, there are three rules that should be followed to get the best results from TDD. They are:
- You should write only enough tests that are sufficient to fail
- You should only write enough code that is sufficient to pass the written failing code
- Write production code only to pass the failing tests.
So did that sound a bit strange to you? Maybe you have been doing it all wrong since the start. For example, you might be writing all the tests ahead of time and then start writing the production code. Or you might be writing code earlier and write test later. That clearly violates the above three TDD principles and does not give the benefits of practicing the TDD approach. If you write all tests at first, here are something that can happen:
- You will be exhausted by a bunch of failing tests
- You might need to restructure the tests in the middle of writing production code,
- You violate the Red Green principle which states the red green cycle should be quick,
- You will accidently mix tested code and untested code. etc
The benefits that TDD brings are many. It does not gurantee you to write clean code but in most of the time it will. You might still write a bad code or a bad test but if done in a correct way it might save you hours of debugging in future.
If you like my article, don’t forget to follow me on Medium, or connect me on Linkedin, or follow me on twitter.