The Red Green Refactor Cycle

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:

1it 'returns false if there are available spaces on the board' do
2 board =[1, 2, 3, 'x', 5, 6, 7, 8, 9])
3 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:

1def complete?
2 available_squares = @squares.count { |square| square.is_a? Integer }
3 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:

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

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

1def 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.

Helpful resources:

Previous post:
What Is The Single Responsibility Principle?
Next post:
Testing Output With RSpec