마찬가지로 rails refactoring 에 관한 자료입니다.
URL
EMBED
Page 0:
Page 1: Refactoring Your Rails Application
RailsConf 2008 Zach Dennis
Mutually Human Software www.mutuallyhuman.com
Drew Colthorp
Atomic Object www.atomicobject.com
Page 2: Example
Extract Query to Model
Page 3: What is refactoring?
Page 4: Why refactor?
Page 5: Why refactor?
Program Evolution
Page 6: Why refactor?
Technical Debt / Pain Management
Page 7: How do you refactor?
Page 8: How do you refactor?
Step #1 - have solid tests
Page 9: How do you refactor?
Short deliberate steps
Page 10: How do you refactor?
Wear two hats, but only one at a time.
Page 11: Testing
Automated test suite preferred
Page 12: Testing
When in doubt nothing beats a good manual test plan
Page 13: What do you refactor?
Page 14: What do you refactor?
Code that smells
Page 15: Common smells in Rails apps
Duplicated Code Shotgun Surgery Long Method Inappropriate Intimacy Divergent Change Large Class Feature Envy Message Chains Comments
Page 16: Duplicated Code smell
def organizer_names Organizer.find(:all).map(&:name) end def organizer_ids Organizer.find(:all).map(&:id) end
Page 17: Shotgun Surgery smell
A change requires modifying many different parts of your application.
Page 18: 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
Page 19: Inappropriate Intimacy smell
class PeopleController def index Person.find(:all, :select => “name”, :include => [:companies, :groups] :order => :updated_at ) end end
Page 20: Divergent Change smell
A class is commonly changing in different ways for different reasons.
Page 21: Large Class smell
A class is doing too much. Common indicators: - too many instance variables - too much code
Page 22: Feature Envy smell
A method is more interested in a class other than the one it is actually on.
Page 23: Many more smells exist
http://www.refactoring.com/sources.html
Page 24: Deliberate code smells
Code smells have their tradeoffs
Page 25: When do you refactor?
Page 26: When do you refactor?
Adding a feature
Page 27: When do you refactor?
Cleaning up the code base
Page 28: When do you refactor?
Spikes, prototyping
Page 29: Refactoring is essential
Red. Green. Refactor.
Page 30: Common Rails Dilemma
MVC paralysis
Page 31:
Page 32:
Page 33: Rails Refactorings
Page 34: Example
Move Data From View Into Presenter
Page 35: What is a Presenter?
An object responsible for view-related logic and behavior.
Page 36: Presenters are decorators, they wrap objects, and delegate
Page 37: Presenters can work for individual resources / models ie: PersonPresenter
Page 38: Presenters can work for specific UI components ie: ProjectWidgetPresenter
Page 39: Example
Move Data From View Into Presenter
Page 40: Example
Move View Logic From View Into Presenter
Page 41: Example
Move Model Logic Into Model
Page 42: Example
Extract Operations Into Service
Page 43: What is a Service?
An operation that stands alone with no encapsulated state.
Page 44: class ProjectService def create_project generate new project logo automatically create the project send out email notifications ... end end
Page 45: class AccountService def transfer_funds(source, target, amount) transaction do source.withdraw(amount) target.deposit(amount) end end end
Page 46: Example
Extract Operations Into Service
Page 47: Example
Introduce Result Object
Page 48: Example
Organize Large Model w/Mixins
Page 49: Example
Extract RJS From Inline Update
Page 50: Example
Extract RJS Into Renderer
Page 51: Example
Replace Instance Variable w/Local Variable In Partial
Page 52: Example
Extract Class from Long Method
Page 53: 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
Page 54: Questions?
Page 55: 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
Page 56: 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
Page 57: Thanks for attending!
Page 58: