Skip to content

Instantly share code, notes, and snippets.

@paulghaddad
Last active August 29, 2015 14:08
Show Gist options
  • Save paulghaddad/6197d0ebd8a55fa569aa to your computer and use it in GitHub Desktop.
Save paulghaddad/6197d0ebd8a55fa569aa to your computer and use it in GitHub Desktop.
Level Up 3: Understands Red -> Green -> Refactor
Explain how the red / green / refactor cycle works, and how it creates better code over time.
The red / green / refactor cycle explains the process of Test-Driven Development. The Red part of the cycle denotes a failing test. This failing test is written before any code. After the test is written, it should be run. Since code hasn't yet been written, the test should fail. Then you move on to the next step in the cycle: green.
In the green step, the simplest code is written to make the test pass. Perfection is not the goal of this step; making the test pass is. Once the test passes, you can be confident the test is actually testing the code in question since it initially failed, and then passed. Then it is on to the final step: refactoring.
During this step, which is only performed when the code is green, the goal is to refactor the code to make it better. Some of the things to focus on include: ensuring there is no duplication in the system; making the code expresses your intent, clearly and concisely; and minimizing classes and methods; among many others. Because you have a test to support the code, you can be confident in making changes. Simply run the tests, and make sure they pass. If the tests happen to go back to red, make sure you immediately go back to green. This cycle is repeated over and over when creating the program, incrementally adding additional tests, and making them pass.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment