Some developers who attended our workshop at Ruby Hack Night were asking about refactoring in the context of the development processes that go on each day at Ryatta (and forward-thinking dev shops everywhere ;).
Refactoring is the simple task of reworking code for clarity and simplicity, and in this way it does not rely on a context to be a useful process. For instance, a developer using a language that doesn’t have a testing framework could still be said to be refactoring if they iteratively revise that code with the goal of clarity and simplicity. However, refactoring really shines in the context of a TDD process – one in which the act of refactoring is core to the devlopment process.
The above diagram will assist in discussing the relationship between TDD and refactoring.
1. A new requirement, which could also be, for example, a meta-requirement such as “Clean up the User class logged_in? method”, is inserted into your tracking system.
2. The next steps are the items in red: write a new test and ensure that the test fails. These steps may be iterated upon until the test provides the appropriate coverage and type of failure to accurately capture the requirement.
3. Next, the new code is implemented and the test run. Again this is iterated until the test passes. This code is intended to be the simplest implementation that makes the test pass.
Note: In the case of existing code, such as our User.logged_in? method, we write the test and prove that it passes as a contraction of these first steps.
4. Finally, we use very small iterations to improve the code for clarity and simplicity. As noted in our last workshop, this may mean creating more complicated code in order to find opportunities for refactoring. This is ok and is something we can learn by practice.
The process starts again at the top with a new requirement.
I hope this helps to clarify where and when refactoring is used. I welcome your comments and questions posted here – especially if you are a workshop attendee and want to provide a suggestion for future workshops.
[Click for more articles from our Development series]
[Click for more articles from our Workshop series]
[Click to learn more about Ryatta]