Blog

Discover Our Blog

What is Refactoring in Ruby on Rails?

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

end

 

<%= @invoice.user.name %>

<%= @invoice.user.address %>

Refactor:  

class Invoice < ActiveRecord::Base

belongs_to :user

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

end

 

<%= @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

Comments

Leave a comment: