What is Refactoring in Ruby on Rails?

24 Jun 2016
What is Refactoring in Ruby on Rails?

Refactoring modifies software to improve its readability, maintainability, and extensibility without changing what it actually does. Its external behavior does NOT change but its internal structure is improved.

Benefits of Refactoring:

  • Code size is often reduced
  • Confusing code is restructured into simpler code
  • Refactoring is technique to help you discover problems in design and implementation

Code Examples

class Invoice < ActiveRecord::Base

belongs_to :user



<%= @invoice.user.name %>

<%= @invoice.user.address %>


class Invoice < ActiveRecord::Base

belongs_to :user

delegate :name, :address, :cellphone, :to => :user, :prefix => true



<%= @invoice.user_name %> <%= @invoice.user_address %>

When do I refactor?

  • When you add functionality
  • When you need to fix a bug
  • When you do a peer review

Code Optimization Tools:

  • Traceroute
  • Rack-Mini-Profiler
  • Bullet
  • Brakeman
  • Deadweight
  • Rails Best Practices
  • Rubocop
  • Rubycritic

Leave a comment: