100% found this document useful (1 vote)
321 views57 pages

Refactoring Your Rails Application Presentation

This document summarizes a presentation on refactoring Rails applications. It discusses what refactoring is, why it's important, and how to refactor code, including having solid tests and taking short deliberate steps. It describes common code smells like duplicated code, long methods, and feature envy. Examples are given of refactoring techniques like extracting queries to models, moving view logic to presenters, and extracting operations into services. Refactoring is presented as an essential part of evolving code and managing technical debt over time.

Uploaded by

devon.y
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
321 views57 pages

Refactoring Your Rails Application Presentation

This document summarizes a presentation on refactoring Rails applications. It discusses what refactoring is, why it's important, and how to refactor code, including having solid tests and taking short deliberate steps. It describes common code smells like duplicated code, long methods, and feature envy. Examples are given of refactoring techniques like extracting queries to models, moving view logic to presenters, and extracting operations into services. Refactoring is presented as an essential part of evolving code and managing technical debt over time.

Uploaded by

devon.y
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 57

Refactoring Your Rails Application

RailsConf 2008

Zach Dennis
Mutually Human Software
www.mutuallyhuman.com

Drew Colthorp
Atomic Object
www.atomicobject.com
Example
Extract Query to Model
What is refactoring?
Why refactor?
Why refactor?
Program Evolution
Why refactor?
Technical Debt / Pain Management
How do you refactor?
How do you refactor?
Step #1 - have solid tests
How do you refactor?
Short deliberate steps
How do you refactor?
Wear two hats, but only one at a time.
Testing
Automated test suite preferred
Testing
When in doubt nothing beats a good manual
test plan
What do you refactor?
What do you refactor?
Code that smells
Common smells in Rails apps
Duplicated Code
Shotgun Surgery
Long Method
Inappropriate Intimacy
Divergent Change
Large Class
Feature Envy
Message Chains
Comments
Duplicated Code smell
def organizer_names
Organizer.find(:all).map(&:name)
end

def organizer_ids
Organizer.find(:all).map(&:id)
end
Shotgun Surgery smell
A change requires modifying many different
parts of your application.
Long Method smell
1: def create
2: @record = Record.find(params[:id])
3: @record.name = params[:name]
...
37: if @record.save
38: redirect_to records_path
39: else
40: render :action => “edit”
41: end
42:end
Inappropriate Intimacy smell
class PeopleController
def index
Person.find(:all,
:select => “name”,
:include => [:companies, :groups]
:order => :updated_at
)
end
end
Divergent Change smell
A class is commonly changing in different
ways for different reasons.
Large Class smell
A class is doing too much.

Common indicators:
- too many instance variables
- too much code
Feature Envy smell
A method is more interested in a class other
than the one it is actually on.
Many more smells exist
http://www.refactoring.com/sources.html
Deliberate code smells
Code smells have their tradeoffs
When do you refactor?
When do you refactor?
Adding a feature
When do you refactor?
Cleaning up the code base
When do you refactor?
Spikes, prototyping
Refactoring is essential
Red. Green. Refactor.
Common Rails Dilemma
MVC paralysis
Rails Refactorings
Example
Move Data From View Into Presenter
What is a Presenter?
An object responsible for view-related logic
and behavior.
Presenters are
decorators,
they wrap objects,
and delegate
Presenters can work for
individual resources /
models

ie: PersonPresenter
Presenters can work for
specific UI components

ie: ProjectWidgetPresenter
Example
Move Data From View Into Presenter
Example
Move View Logic From View Into Presenter
Example
Move Model Logic Into Model
Example
Extract Operations Into Service
What is a Service?
An operation that stands alone with no
encapsulated state.
class ProjectService

def create_project
generate new project logo automatically
create the project
send out email notifications
...
end

end
class AccountService

def transfer_funds(source, target, amount)


transaction do
source.withdraw(amount)
target.deposit(amount)
end
end

end
Example
Extract Operations Into Service
Example
Introduce Result Object
Example
Organize Large Model w/Mixins
Example
Extract RJS From Inline Update
Example
Extract RJS Into Renderer
Example
Replace Instance Variable w/Local Variable In Partial
Example
Extract Class from Long Method
More Rails Refactorings
- Extract Complex Creation Into Factory
- Move Before Filter and Friends Into App. Service
- Move View Data From Controller Into Presenter
- Move View Logic From Controller Into Presenter
- Move Extrinsic Callback Into Observer
- Move View Logic From Model Into Presenter
Questions?
References / Resources
Books
- Refactoring: Improving The Design Of Existing Code, Martin Fowler
- The Pragmatic Programmer, Dave Thomas and Andy Hunt
- Domain Driven Design:
Tackling The Heart Of Complexity In Software, Eric Evans

Online Resources
- http://www.refactoring.com
References / Resources
Example Application based on Strac
- Example Agile Project Management, Zach Dennis and Drew Colthorp
http://github.com/zdennis/strac/tree/refactoring

Strac
- Open Source Agile Project Management, Mark VanHolstyn and Zach Dennis
http://github.com/mvanholstyn/strac/tree/master
Thanks for attending!

You might also like