Understanding the Red, Green, Refactor Cycle

Elle Hallal
Mar 29, 2019 · 3 min read

This week, I have continued creating a human vs. human Tic Tac Toe game in Ruby, using Test Driven Development. I have also been trying to get familiar with a number of principles and rules, such as SOLID principles and the Four Rules of Simple Design.

Following last week’s blog, it turns out although I have a fair understanding of the Single Responsibility Principle. However, implementing it correctly as the Tic Tac Toe game increases in size is more challenging.

When creating Tic Tac Toe, Test Driven Development is helping me to break down the game into small chunks. I still have a way to go, but the red, green refactor cycle is helping me to write cleaner code.

Red, Green, Refactor cycle


The red stage means that a test should be written and it should fail. This means not writing any code related to the test yet. In addition, it’s important to run the test to ensure it fails. Here’s an example of a test in my Tic Tac Toe game:

it 'returns false if there are available spaces on the board' do
board =[1, 2, 3, 'x', 5, 6, 7, 8, 9])
expect(board.complete?).to be false


The green stage means code should be written to pass the test. At this stage, the code doesn’t need to be the cleanest, just enough to make the test pass.

The simplest way of passing the above test would be to create a method namedcomplete?, which simply returns false. Here’s my initial attempt, which passed the test:

def complete?
available_squares = @squares.count { |square| square.is_a? Integer }
available_squares == 0


The refactor stage refers to writing a cleaner version of the code and still pass the test. In Sandi Metz’s presentation on SOLID Object-Oriented Design, there are a number of factors to consider when refactoring:

  • Is it DRY?
  • Does it have a single responsibility?
  • Does everything in it change at the same rate?
  • Does it depend on things that change less often that it does?

Looking at the method above, it appears to be DRY. However, it does have more than one responsibility.

  • Counting the amount of available squares
  • Returning true or false depending on the amount of available squares

My understanding is there should be two separate methods. With this in mind, another test was created to test a method counting the amount of available squares. The related code in complete? was extracted and added to the new method:

it 'returns the amount of available squares' do
board =['x', 2, 'o', 4, 5, 6, 7, 8, 9])
expect(board.available_squares).to eq(7)
def available_squares
@squares.count { |square| square.is_a? Integer }

The refactoredcomplete? method is now doing one thing: returning true or false depending on the amount of available squares:

def complete?

I still have a way to go to adhere to the Single Responsibility Principle throughout the Tic Tac Toe game. Following the red, green, refactor cycle and the points on refactoring are helping to apply this principle.

Elle Hallal

Written by

Apprentice at 8th Light. Github:

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade