Code Smells @ YouTube

MountainWest RubyConf 2013 Code Smells: Your Refactoring Cheat Codes by John Pignata

https://github.com/jpignata/mwrc

It’s great presentation which talks about refactoring the code by showing examples. The actual refactoring part is 20-minutes or something, but it has a lot of nice viewpoints. Defining the roles of modules and their interactions are the key factor of designing maintainable codes. Always, actual example-code works well understanding the concept.

One concern point (which I felt) is some of the classes are cross-communicating each other, making the class relationship a little complex.

(e.g.)

  1. PushDaemon <-> UDPServer
  2. UDPServer <-> Client <-> Job
  3. Worker <-> Job

These classes are arbitrarily calling the other one’s method. As ruby’s duck typing doesn’t define the specific interface, it’s a little difficult to identify the method call-graph. Maybe, at least for the 1st item, instead of calling pre-fixed method each other, apply observer pattern (subscribe/notify) from one side makes it clear about their relationship?

require "observer"

class Server
  include Observable

  def start
    sleep 1
    changed
    notify_observers("sample response")
  end
end

class Daemon
  def initialize
    @server = Server.new
    @server.add_observer(self, :call)
  end

  def run
    @server.start
  end

  def call(message)
    puts "called by listener with message: " + message
  end
end
Advertisements

Posted on July 11, 2013, in Ruby, Software Design, Web. Bookmark the permalink. 1 Comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: