- Refactoring Under Green
- Lockdown Tests
- Extract Class
- Controller Refactoring
- OSS Contributions (if time permits)
Clone this repository, and then install the dependencies with:
$ bundle install
We will use the nursery rhyme The House That Jack Built to practice taking tiny steps and refactoring under green.
$ cd house
Run the tests with the following command:
$ ruby test/house_test.rb
We will use the classic song 99 Bottles of Beer on the Wall to practice writing lockdown tests.
$ cd bottles
Run the application with:
$ rackup
Then visit the app in your browser at localhost:9292.
We need to move the logic out of the beer song controller so that we can
implement the bin/bottles
command-line application.
$ bin/bottles
Refactoring by Martin Fowler contains a number of step-by-step recipes.
In all the recipes it is critical that you run the tests after each step.
If your tests goes red stop and back up. Undo the change, run your tests again to make sure it goes green, and then attempt the change again, or try to find a smaller step that you can take.
In the GET /
endpoint of the bottles app, there is some code that has to do with generating the song, and some that has to do with providing HTML for the view to render.
You will need to split that logic up so that the song-logic is in a method on its own.
- Create a new method, and name it for what it does (or returns), not for what the implementation will be.
- Copy (do not cut) the code you want to extract into the new method.
- Look for local variables in the extracted code.
- ~~ local variable steps skipped, there are none ~~
- Replace the extracted code in the source method with a call to the target method.
- Create a new, empty class.
- Instantiate the new class from within the old class.
- Move each method that belongs in the target class.
- Restrict the API by making methods private, if possible.
- Delete any code that is no longer used.
- Rename stuff, if you come up with better names.
- Examine all the features that a method uses, and decide if they also need to be moved into the new class.
- Define an empty method in the target class.
- Copy (do not cut) the body of the old method into the new method. Adjust the method to work in its new home.
- Replace the body of the source method with a call to the target method.
See the Tag Cloud Refactoring tutorial.
If there is time left at the end of the day, fork and clone the real tracks repository, and find some improvement that can be made.