Basic Rules of Refactoring – code cast
Blog post series
This is a code cast in Java.
We start by checking what can be extracted to another method. The Basic Rules of Refactoring describes the detailed steps I use.
To ensure that we have a safety net, let us start with covering the code with some system tests and then write tests on the newly created method. These are also characterization tests, but they are often simpler than the system tests we wrote before. When the new method have been covered by tests, call it from the initial place where the code was copied from. Run all the system tests. At the end do some manual testing to see that everything works fine.
This technique is very useful to have small and safe steps while refactoring. Remember to never cut and paste. Always duplicate the implementation, cover it with tests and then reroute the call to the new code.
Many thanks to Thomas Sundberg for proofreading this post.