Hacker News new | ask | show | jobs
by mpweiher 1251 days ago
> [R->G] But when writing new code?

Absolutely, and most prominently when writing new code. The red-green transition is absolutely essential for new code.

You should not write any production code except to make a red test green.

Think of the tests as the specification of your system.

If all tests are green, your system meets the specification. Thus there is no need to write production code.

So in order to make the system do something new, you need to first change the specification. So you add a test. When you add this test, it will almost certainly fail. After all, you haven't written the code to implement the feature.

Then you make the test green, and now the system once again matches the (now updated) specification. Commit/Refactor/Commit.

Having the test that is red also validates your tests. If your tests are always green, how do you know that you're actually testing something?

In fact, it sometimes happens that you write a test that you think should be red, because you haven't implemented the feature yet, but then it starts of as green. Meaning you inadvertently already built the feature. This can be very confusing... :-)