Thursday, 6 November 2008

Make me Whole

This is one of my favourite refactoring patterns and one I find I use a great deal so I thought I'd share it. The basic premise of this refactoring is to encapsulate logic into its own class using small, easy steps. The trick is each step should always leave the code in good working order i.e. the tests should always pass.

To help explain I'm going to walk through the refactoring by encapsulating some simple procedural logic and by making the concept explicit by giving it it's own class. Here's our starting point:

  def say_hello()
if @friend == "Jeff" || @friend == "Robert" do
put "Hello my good friend #friend"
else
put "Hello #friend"
end
end

The first thing we notice is there is some behaviour which depends on the state of friend (is the value "Jeff" or "Robert"). This is a good sign that there's a concept called Friend which needs encapsulating. So here's how:

Step One: Extract method

The first step is easy: extract the logic into it's own method like so:

  def say_hello()
if is_good_friend do ...
end

def is_good_friend
@friend == "Jeff" || @friend == "Robert"
end

This is good, now the code is clearer to read and we've encapsulated the logic (albeit only in a method). That's already a big improvement.

Step Two: Introduce parameter

The problem is our method is dependent on its class for the @friend variable. We need to sever this connection so the method can stand on its own two feet. The simple way is to pass friend through as a parameter.

  def say_hello()
if is_good_friend(friend) do ...
end

def is_good_friend(friend)
friend == "Jeff" || friend == "Robert"
end

That's better: is_good_friend is decoupled from the class. Its free!

We still need to get that method onto a new class. The problem is it can't be done in one easy non-breaking step. So we're going to have to take a tiny intermediate step to get there:

Step Three: Make method static

By making the method static it severs all connections to the original class: it is explicitly non-dependant on any instance variables the original class has.

  def say_hello()
if self.is_good_friend(friend) do ...
end

def self.is_good_friend(friend) ...

Making the method static is also a good safety check to ensure that you've not got any references to the original class and it is properly isolated before moving it.

Step Four: Move method

Now the method is static move it to the new class:

  def say_hello()
if Friend.is_good_friend(friend) do ...
end

class Friend
def self.is_good_friend(friend) ...
end

Excellent, we're really getting somewhere! Though now we've got a static method sitting on a helper class. Yuk: let's get that sorted before someone copies us and that nasty anti-pattern starts to proliferate through our code base:

Step Five: Create instance

This is where the real magic starts to happen: we create an instance of our new class and use it to store the value of friend:

  class Friend
@value

def self.is_good_friend(old_friend)
friend = Friend.new(old_friend)
friend.value == "Jeff" || friend.value == "Robert"
end
end

Still, this is a bit ugly. What we really need to do is get the client to do the work of creating the instance.

Step Six: Introduce parameter

Rather than have our static method new up Friend get the client to do it and pass it across:

  def say_hello()
if(Friend.is_good_friend(@friend, Friend.new(@friend)) ...
end

class Friend
...
def self.is_good_friend(old_friend, friend)
friend.value == "Jeff" || friend.value == "Robert"
end
end

Step Seven: Remove parameter

The old_friend parameter is redundant and ugly: let's get rid of it!

  def say_hello()
if(Friend.is_good_friend(Friend.new(@friend)) ...
end

class Friend
...
def self.is_good_friend(friend) ...
end

Excellent, this is starting to look a lot better now. Though look at that repetition: the static method on Friend passes an instance of it's own class! Only one way to sort that out:

Step Eight: Make method non-static

Now the beauty of this is that if you are using a modern IDE it will work out that the method is being passed an instance of its own type and it will magically do this:

  def say_hello()
if(Friend.new(@friend).is_good_friend) ...
end

class Friend
def is_good_friend
@value == "Jeff" || @value== "Robert"
end
end

Brilliant: Friend is now nicely encapsulated and ready to grow into a responsible object with a bright future. From here we can start pushing more behaviour onto the object and eventually make @friend reference an instance of the Friend class.

Overall the pattern is very simple: extract the logic into its own method then work to detach it from the class by making in static, then simply move the static method and reattach it to the new class by returning it to an instance. It is also possible to handle more complex behaviour with several variables involved, simply pass them through as parameters and then push them onto the new instance.

Although this example is simple the pattern can be repeated again and again to build up classes into a number of methods (sometimes these classes can again be refactored into smaller classes still).

2 comments:

Anonymous said...

41961.....64896

Anonymous said...

[b]Здравствуйте Уважаемые автолюбители и профессионалы![/b]

Хотим предложить Вам сотрудничество в поставках пленки для автомобилей. Цены максимально занижены. Для дилеров цена обговаривается отдельно. По качеству пленка не уступает дорогим европейским и японским аналогам. Пленка производится под заказ и ввозится под нашим брендом с крупных мануфактур Китая что позволяет исключить кустарное производство.
Ассортимент в наличии:
- Карбон 3D (220 микрон с каналами выхода воздуха. Цвета Черный, Серебро, Белый и другие).
- Матовая пленка (220 микрон с каналами выхода воздуха. Цвета черный, Белый и другие).
- Защитная для фар(легкая тонирующая) и кузова автомобиля(прозрачная)(220 микрон с каналами выхода воздуха).
- Пленка под Хром (220 микрон с каналами выхода воздуха. Цвета Золото, Серебро).

Мы так же можем привезти под заказ любой материал. У наших поставщиков есть огромный ассортимент на любой вкус и цвет. Так же предоставляем услуги по оклейке пленкой всех автомобилей. Находимся в Москве.
Наши контактные данные:
[url=http://www.artifilm.ru]http://www.artifilm.ru[/url]
e-mail: carbon3d@russia.ru
Телефон-1: 8-967-163-50-40
Телефон-2: 8-909-940-41-03
Мы Вконтакте: [url=http://www.artifilm.ru]http://vkontakte.ru/carbon_auto [/url]

About Me

My photo
West Malling, Kent, United Kingdom
I am a ThoughtWorker and general Memeologist living in the UK. I have worked in IT since 2000 on many projects from public facing websites in media and e-commerce to rich-client banking applications and corporate intranets. I am passionate and committed to making IT a better world.