Refactoring Rails Applications: A Workshop to Remember

Refactoring is a technique I often use but know I still have a lot to learn, especially when I see a presentation like Therapeutic Refactoring. So I was very excited when the organizers of the Barcelona Ruby Conference announced the pre-conference workshop “Refactoring Rails Applications” by JumpstartLab. It may sound strange to attend a refactoring workshop in such a beautiful city like Barcelona, but you should give it a try.

In the workshop Katrina Owen showed us how we can use refactoring with a real Rails application – not the small little examples we always see at conferences. To exercise and improve our skills we used the Tracks application. This Application is old (started with Rails 0.8), widely used and has a measurably bad quality – what makes it perfect for the workshop.

For me the day was well spent. I could close some gaps in my understanding of cucumber and selenium, find out a lot on how to pick the starting point for a refactoring session and saw how helpful tools like Code Climate can be. And I could experience how helpful small classes and even smaller methods can be. I never believed how fundamental this simple change improves the readability of code. But seeing now code that so far was small I immediately want to split it in smaller parts.

Lock Down the Output

A great new technique I learned at the workshop is to lock down the output of a complex method. When you write your code test first you will not need that. But in the real world you find often code that was not written with tests in mind. You can go and isolate the method you want to change – but that can take hours or even days.

Or you take a shortcut and dump the output of the method to disk. With this reference point you can check the current output of the method with the one before you started refactoring it. As long as it is unchanged you are on the right track. If it changes you did something wrong and get an immediate feedback through a failing test. And yes, you can call this a hack. But I prefer a dirty safety net over doing the full checks manually over and over again.

Depending on your application it may be as easy as to open your Rails application in the browser, save the output to a file and check it against the safe version. But most likely it will need more work than that, especially if the output changes depending on time. Luckily there are many gems around to help (like TimeCop) and if not, you can always ask your questions on Stack Overflow.

Try it

You can try the refactoring workshop on your own. All you need is a Rails environment and the tutorial from JumpstartLab. You won’t have the guidance and help of Katrina and the discussion with the other participants – for that you need to go to the workshop. But beside that you have all the things you need.

Conclusion

In the real world you don’t get far with the simple examples on refactoring you always see at conferences. Here you need more. The tutorial from JumpstartLab shows what you will be up to and how you can refactor even a mess like Tracks. And try to attend a workshop with Katrina Owen. She knows how to refactor and can teach you a lot!

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.