What is Red-Green-Refactor?

Red-Green-Refactor is a software development technique used in Test-Driven Development (TDD), a methodology focused on writing tests before writing the actual code. The process is divided into three distinct phases:

  1. Red: Write a failing test. The developer writes a test for a specific functionality that does not yet exist or is not yet implemented in the code. The test is expected to fail when run, which is represented by the color red. This failing test ensures that the test itself is valid and checks for the intended functionality.
  2. Green: Make the test pass. The developer writes the minimum amount of code necessary to satisfy the test requirements, making the test pass. When the test passes, it is represented by the color green. This stage focuses on making the code functional without worrying too much about optimization or best practices.
  3. Refactor: Improve the code. Once the test is passing, the developer reviews the code and refactors it to improve readability, maintainability, and performance, while ensuring that the test still passes. This stage is about cleaning up the code and applying best practices without changing the functionality.

The Red-Green-Refactor cycle is repeated for each new functionality or feature, which helps to ensure that the code is well-tested and follows good design principles. The TDD approach promotes writing code that is easier to maintain, debug, and extend, while reducing the likelihood of introducing bugs during the development process.

Here is a simple example!

Let’s consider a simple example where we want to implement a class named “Calculator” with an “add” method that takes two integers as input and returns their sum. Here’s a step-by-step illustration of the Red-Green-Refactor process in Java:

  1. Red: Write a failing test. First, we write a test for the “add” method, which doesn’t exist yet.
import org.junit.Test;
import static org.junit.Assert.assertEquals;

public class CalculatorTest {
    @Test
    public void testAdd() {
        Calculator calculator = new Calculator();
        int sum = calculator.add(2, 3);
        assertEquals(5, sum);
    }
}

When we run this test, it will fail because the Calculator class and its “add” method haven’t been implemented yet.

  1. Green: Make the test pass. Now, we’ll write a simple implementation for the Calculator class and the “add” method to make the test pass.
public class Calculator {
    public int add(int a, int b) {
        return a + b;
    }
}

Now, when we run the test, it should pass (turn green) since the “add” method is correctly implemented.

  1. Refactor: Improve the code. In this example, the code is quite simple, and there isn’t much to refactor. However, imagine that we had more complex logic or multiple methods in the Calculator class. During the refactor phase, we could look for opportunities to improve the code’s readability, maintainability, and performance without changing its functionality.

For example, if we had a more complex method, we might extract parts of the code into smaller, more focused methods or look for ways to optimise the code for better performance. We would also ensure that the test still passes after refactoring.

Final Note

In summary, the Red-Green-Refactor process in Test-Driven Development involves writing a failing test, making it pass with the minimal code required, and then refining the code for better quality while ensuring the test still passes.

📚 Further Reading & Related Topics

If you’re exploring the Red-Green-Refactor cycle in test-driven development (TDD), these related articles will provide deeper insights:

• Mastering Unit Testing in Spring Boot: Best Practices and Coverage Goals – Learn how to effectively implement TDD in Spring Boot projects using the Red-Green-Refactor cycle to ensure high-quality code and proper test coverage.

• Understanding Test Coverage and Its Importance in Software Development – Explore the role of test coverage in TDD and how Red-Green-Refactor helps achieve optimal coverage while developing robust software systems.

Leave a comment

I’m Sean

Welcome to the Scalable Human blog. Just a software engineer writing about algo trading, AI, and books. I learn in public, use AI tools extensively, and share what works. Educational purposes only – not financial advice.

Let’s connect