Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for What Is The Red Green Refactor Cycle
Elle Hallal 👩🏽‍💻
Elle Hallal 👩🏽‍💻

Posted on • Originally published atellehallal.dev on

What Is The Red Green Refactor Cycle

Originally posted atellehallal.dev👩🏽‍💻


This week, I have continued creating a human vs. humanTic 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.

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

Red

Thered 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'returnsfalseifthereareavailablespacesontheboard'doboard=Board.new([1,2,3,'x',5,6,7,8,9])expect(board.complete?).tobefalseend
Enter fullscreen modeExit fullscreen mode

Green

Thegreen 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:

defcomplete?available_squares=@squares.count{|square|square.is_a?Integer}available_squares==0end
Enter fullscreen modeExit fullscreen mode

Refactor

Therefactor stage refers to writing a cleaner version of the code and still pass the test. In Sandi Metz’s presentation onSOLID 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:

//testit'returnstheamountofavailablesquares'doboard=Board.new(['x',2,'o',4,5,6,7,8,9])expect(board.available_squares).toeq(7)end//methoddefavailable_squares@squares.count{|square|square.is_a?Integer}end
Enter fullscreen modeExit fullscreen mode

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

defcomplete?available_squares.zero?end
Enter fullscreen modeExit fullscreen mode

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:

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Senior Software Engineer. Leadership Team Member & Mentor at Coding Black Females 👩🏽‍💻
  • Location
    London, UK
  • Work
    Senior Software Engineer at 8th Light
  • Joined

More fromElle Hallal 👩🏽‍💻

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp