Refactoring is the craft of improving the design of an existing code without changing its external behavior. Just like with everything else, you get better at it with practice and continuous learning. Check out a couple of talks given by some of the best Ruby developers that point out common mistakes and how to deal with them.

I've curated five of my favorite talks on code refactoring taking into consideration the information presented, the quality of examples and the quality of the presentation itself. Some of the talks are closely tied to Ruby on Rails, but most refactoring techniques are often language agnostic, so even if you're not in the Ruby world, don't hesitate to watch them.

Sandi Metz - Nothing is something

Sandi Metz gave this talk at multiple conferences this year and it gained a lot of traction - and for a reason - it's awesome! The first half of the talk is more oriented on her animosity towards conditionals, the dirtiness of nil checks and advocating the use of the Null Object pattern.

The second part is a real life example of the misleading usage of inheritance (used for shared behavior instead of specialization) and a step-by-step explanation on how to refactor the given code using composition with dependency injection.

If you like this talk, be sure to check out Sandi's All the little things from Railsconf 2014, where Sandi explains on a real world example how to thoroughly refactor a stale and procedural code.

Bryan Helmkamp - Refactoring Fat Models with Patterns

Bryan Helmkamp, the CEO of Code Climate, gave this talk at the LA Ruby Conference in 2013, where he explained how to run from the methodology of Thin Controllers, Fat Models and presented other options such as Form Objects, Service Objects, Decorators, etc. There is also an associated blogpost if you're not up for watching the video.

There are some gems that Bryan doesn't mention that can come quite in handy when implementing these patterns in Rails:

Form Objects - Reform

Service Objects - Interactor

Decorators - Draper

Ben Orenstein - Refactoring from Good to Great

Thoughtbot's Ben Orenstein kicked ass with this talk at the Aloha Ruby Conf in 2012. If you want to know the basics of patterns such as the single responsibility principle, the tell don't ask principle, the null object pattern or anti-patterns like Shotgun surgery, this is the talk for you.

Avdi Grimm - Confident Code

This is a talk that Avdi Grimm gave back in 2011 at Ruby Midwest. It's an excellent talk on how to construct methods and separate them into 4 parts:

gather input,

perform work,

deliver results,

handle failures.

There are examples of each part of a method, common mistakes that are usually made and how to fix them: embracing duck typing instead of using class checks, refactoring case equality statements with type coercion, using Hash#fetch for easier debugging, the Black Hole NilObject Pattern, etc.

If you want to find out more about the stuff he's been talking about beyond this video, check out his book called Confident Ruby.

Aja Hammerly - N design patterns you might actually use

Knowing your design patterns is the mother of all refactoring. Aja talks about design patterns you might actually use in Ruby. She goes fast through the examples in a sarcastic manner and wastes no time covering the following patterns: template, strategy, composite, command and decorators. For each, she gives real world examples and hands out code snippets.

She mentions how the Gang Of Four book is always lying around her office and recommends Rubyists to read a more applicable book on design patterns by Russ Olsen. Doug Yun from Dockyard covered most of them well in a series of blog posts on design patterns.