• Like
  • Save
Refactoring
Upcoming SlideShare
Loading in...5
×
 

Refactoring

on

  • 3,015 views

Talk at the Orlando Ruby User Group on January 2010 about Refactoring.

Talk at the Orlando Ruby User Group on January 2010 about Refactoring.

Statistics

Views

Total Views
3,015
Views on SlideShare
1,592
Embed Views
1,423

Actions

Likes
4
Downloads
44
Comments
0

13 Embeds 1,423

http://campusvirtual.ull.es 1118
http://caikesouza.com 232
http://www.caikesouza.com 47
http://www.linkedin.com 6
http://www.slideshare.net 5
http://feeds2.feedburner.com 4
http://www.nobduf.org 3
https://www.linkedin.com 3
http://nobduf.org 1
http://www.docshut.com 1
http://feeds.feedburner.com 1
http://localhost 1
http://caike.github.com 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Refactoring Refactoring Presentation Transcript

    • Refactoring { :from => :mess, :to => :clean_code, :through => :patterns } @caike Orlando Ruby User Group http://caikesouza.com January 2010 Friday, January 15, 2010
    • Agile Friday, January 15, 2010
    • Testing Friday, January 15, 2010
    • Patterns Friday, January 15, 2010
    • Professionalism Friday, January 15, 2010
    • Software Craftsmanship Friday, January 15, 2010
    • ? Friday, January 15, 2010
    • http://www.flickr.com/photos/eurleif/255241547/ Friday, January 15, 2010
    • "Any fool can write code that a computer can understand. Good programmers write code that humans can understand" (Martin Fowler) Friday, January 15, 2010
    • “Free software only matters to those who can read” (Robert M. Lefkowitz) Friday, January 15, 2010
    • Friday, January 15, 2010
    • http://www.flickr.com/photos/dhammza/91435718/ Friday, January 15, 2010
    • Given messy code that works When I refactor Then it should be easier to understand And still work! Friday, January 15, 2010
    • Cleaning up the house Friday, January 15, 2010
    • http://www.flickr.com/photos/benfrantzdale/208672143/ http://www.flickr.com/photos/improveit/1574023621/ Friday, January 15, 2010
    • wiki, Fit, JUnit http://www.flickr.com/photos/benfrantzdale/208672143/ eXtreme Programming http://www.flickr.com/photos/improveit/1574023621/ Friday, January 15, 2010
    • Friday, January 15, 2010
    • http://www.flickr.com/photos/adewale_oshineye/2933030620 Friday, January 15, 2010
    • Some reasons... Friday, January 15, 2010
    • Design++ Friday, January 15, 2010
    • Money++ Friday, January 15, 2010
    • Respond to changes Money++ Friday, January 15, 2010
    • Respond to changes Money++ Release with confidence Friday, January 15, 2010
    • Effort-- Friday, January 15, 2010
    • Waterfall Jan Feb Mar Apr May Jun Jul Cost of Maintenance Extreme Programming Explained: Embrace Change Addison Wesley, 2000 Friday, January 15, 2010
    • XP Jan Feb Mar Apr May Jun Jul Cost of Maintenance Extreme Programming Explained: Embrace Change Addison Wesley, 2000 Friday, January 15, 2010
    • code LESS sleep MORE http://www.flickr.com/photos/x180/503574487/ Friday, January 15, 2010
    • http://www.flickr.com/photos/rockinrob/1485573200/ Friday, January 15, 2010
    • When ? Friday, January 15, 2010
    • When you add a function Friday, January 15, 2010
    • When you fix a bug Friday, January 15, 2010
    • Code Reviews Friday, January 15, 2010
    • http://www.flickr.com/photos/highwayoflife/2699887178/ Friday, January 15, 2010
    • Code Smells Friday, January 15, 2010
    • Duplicated Code Code Smells Friday, January 15, 2010
    • Duplicated Code Code Smells Long Method Friday, January 15, 2010
    • Duplicated Code Large Class Code Smells Long Method Friday, January 15, 2010
    • Duplicated Code Large Class Divergent Change Code Smells Long Method Friday, January 15, 2010
    • Duplicated Code Large Class Divergent Change Code Smells Long Parameter List Long Method Friday, January 15, 2010
    • Test Driven Development Friday, January 15, 2010
    • Red Green Refactor Friday, January 15, 2010
    • What if I Don’t ? Friday, January 15, 2010
    • Debt Metaphor Friday, January 15, 2010
    • http://www.flickr.com/photos/benfrantzdale/208672143/ Friday, January 15, 2010
    • http://www.flickr.com/photos/didmyself/3050138294/ Friday, January 15, 2010
    • Get ready for code! Friday, January 15, 2010
    • managers = [] employees.each do |e| managers << e if e.is_manager? end Saturday, January 16, 2010
    • managers = [] employees.each do |e| managers << e if e.is_manager? end Saturday, January 16, 2010
    • Replace Loop with Closure Method Saturday, January 16, 2010
    • managers = [] employees.each do |e| managers << e if e.is_manager? end Saturday, January 16, 2010
    • managers = employees. select { |e| e.is_manager? } Saturday, January 16, 2010
    • class Movie def initialize(stars) @stars = stars end def recommended? ((@stars > 5) ? 8 : 1) >= 8 end end Saturday, January 16, 2010
    • http://www.flickr.com/photos/anirudhkoul/3804552280/ Saturday, January 16, 2010
    • http://www.bartcop.com/marilyn-monroe001.jpg Saturday, January 16, 2010
    • http://thetorchonline.com/wp-content/uploads/2009/04/deathstar.jpg Saturday, January 16, 2010
    • class Movie def initialize(stars) @stars = stars end def recommended? ((@stars > 5) ? 8 : 1) >= 8 end end Saturday, January 16, 2010
    • Introduce Explaining Variable Saturday, January 16, 2010
    • class Movie def initialize(stars) @stars = stars end def recommended? ((@stars > 5) ? 8 : 1) >= 8 end end Saturday, January 16, 2010
    • class Movie def initialize(stars) @stars = stars end def recommended? rating = (@stars > 5) ? 8 : 1 rating >= 8 end end Saturday, January 16, 2010
    • class Movie def initialize(stars) @stars = stars end def recommended? rating = (@stars > 5) ? 8 : 1 rating >= 8 end end Saturday, January 16, 2010
    • Replace Temp With Query Saturday, January 16, 2010
    • class Movie def initialize(stars) @stars = stars end def recommended? rating = (@stars > 5) ? 8 : 1 rating >= 8 end end Saturday, January 16, 2010
    • class Movie def initialize(stars) @stars = stars end def recommended? rating >= 8 end def rating (@stars > 5) ? 8 : 1 end end Saturday, January 16, 2010
    • OMG, This is sooooo cooooool! Saturday, January 16, 2010
    • class Movie def recommended? rating >= 8 end def rating (@stars > 5) ? 8 : 1 end end Saturday, January 16, 2010
    • class Movie def recommended? rating >= 8 end def rating more_than_five_stars? ? 8 : 1 end def more_than_five_stars? @stars > 5 end end Saturday, January 16, 2010
    • Inline Method Saturday, January 16, 2010
    • class Movie def initialize...end def recommended? rating >= 8 end def rating more_than_five_stars? ? 8 : 1 end def more_than_five_stars? @stars > 5 end end Saturday, January 16, 2010
    • class Movie def initialize...end def recommended? rating >= 8 end def rating @stars > 5 ? 8 : 1 end end Saturday, January 16, 2010
    • mock = mock('user') expectation = mock.expects(:find) expectation.with("1") expectation.returns([]) Saturday, January 16, 2010
    • mock = mock('user') expectation = mock.expects(:find) expectation.with("1") expectation.returns([]) Saturday, January 16, 2010
    • Replace Temp With Chain Saturday, January 16, 2010
    • mock = mock('user') expectation = mock.expects(:find) expectation.with("1") expectation.returns([]) Saturday, January 16, 2010
    • mock = mock('user') mock.expects(:find).with("1"). returns([]) Saturday, January 16, 2010
    • def expects ... self end def with ... self end def returns ... self end Saturday, January 16, 2010
    • def charge(amount, ccnumber) begin conn = CC_Charger_Server.connect(...) conn.send(amount, ccnumber) rescue IOError => e Logger.log "Error: #{e}" return nil ensure conn.close end end Saturday, January 16, 2010
    • def charge(amount, ccnumber) begin conn = CC_Charger_Server.connect(...) conn.send(amount, ccnumber) rescue IOError => e Logger.log "Error: #{e}" return nil ensure conn.close end end Saturday, January 16, 2010
    • Extract Surrounding Method Saturday, January 16, 2010
    • def charge(amount, ccnumber) begin conn = CC_Charger_Server.connect(...) conn.send(amount, ccnumber) rescue IOError => e Logger.log "Error: #{e}" return nil ensure conn.close end end Saturday, January 16, 2010
    • def charge(amount, ccnumber) connect do |conn| conn.send(amount, ccnumber) end end Saturday, January 16, 2010
    • def connect begin conn = CC_Charger_Server.connect(...) yield conn rescue IOError => e Logger.log "Error: #{e}" return nil ensure conn.close end end Saturday, January 16, 2010
    • def body_fat_percentage(name, age, height, weight, metric_system) ... end Saturday, January 16, 2010
    • body_fat_percentage("fred", 30, 1.82, 90, 1) body_fat_percentage("joe", 32, 6, 220, 2) Saturday, January 16, 2010
    • body_fat_percentage("fred", 30, 1.82, 90, 1) body_fat_percentage("joe", 32, 6, 220, 2) Saturday, January 16, 2010
    • Introduce Named Parameter Saturday, January 16, 2010
    • def body_fat_percentage(name, age, height, weight, metric_system) ... end Saturday, January 16, 2010
    • def body_fat_percentage(name, params={}) # params[:age] # params[:height] # params[:weight] # params[:metric_system] end Saturday, January 16, 2010
    • body_fat_percentage("fred", 30, 1.82, 90, 1) body_fat_percentage("joe", 32, 6, 220, 2) Saturday, January 16, 2010
    • body_fat_percentage("fred", :age => 30, :height => 1.82, :weight => 90, MetricSystem::METERS_KG) body_fat_percentage("joe", :age => 32, :height => 6, :weight => 220, MetricSystem::FEET_LB) Saturday, January 16, 2010
    • user.posts.paginate(:page => params[:page], :per_page => params[:per_page] || 15) Saturday, January 16, 2010
    • user.posts.paginate(:page => params[:page], :per_page => params[:per_page] || 15) Saturday, January 16, 2010
    • Replace Magic Number with Symbolic Constant Saturday, January 16, 2010
    • user.posts.paginate(:page => params[:page], :per_page => params[:per_page] || 15) Saturday, January 16, 2010
    • CONTACTS_PER_PAGE = 15 user.posts.paginate(:page => params[:page], :per_page => params[:per_page] || CONTACTS_PER_PAGE) Saturday, January 16, 2010
    • class MountainBike def price ... end end MountainBike.new(:type => :rigid, ...) MountainBike.new(:type => :front_suspension, ...) MountainBike.new(:type => :full_suspension, ...) Saturday, January 16, 2010
    • def price if @type_code == :rigid (1 + @comission) * @base_price end if @type_code == :font_suspension (1 + @comission) * @base_price + @front_suspension_price end if @type_code == :full_suspension (1 + @comission) * @base_price+ @front_suspension_price + @rear_suspension_price end end Saturday, January 16, 2010
    • def price if @type_code == :rigid (1 + @comission) * @base_price end if @type_code == :font_suspension (1 + @comission) * @base_price + @front_suspension_price end if @type_code == :full_suspension (1 + @comission) * @base_price+ @front_suspension_price + @rear_suspension_price end if @type_code == :ultra_suspension ... end end Saturday, January 16, 2010
    • Saturday, January 16, 2010
    • Replace Conditional With Polymorphism Saturday, January 16, 2010
    • class MountainBike def price ... end end Saturday, January 16, 2010
    • module MountainBike def price ... end end Saturday, January 16, 2010
    • class RigidMountainBike include MountainBike end class FrontSuspensionMountainBike include MountainBike end class FullSuspensionMountainBike include MountainBike end Saturday, January 16, 2010
    • RigidMountainBike.new(:type => :rigid, ...) FrontSuspensionMountainBike.new(:type => :front_suspension, ...) FullSuspensionMountainBike.new(:type => :full_suspension, ...) Saturday, January 16, 2010
    • class RigidMountainBike include MountainBike def price (1 + @comission) * @base_price end end Saturday, January 16, 2010
    • def price if @type_code == :rigid raise "should not be called" end if @type_code == :font_suspension (1 + @comission) * @base_price + @front_suspension_price end if @type_code == :full_suspension (1 + @comission) * @base_price+ @front_suspension_price + @rear_suspension_price end end Saturday, January 16, 2010
    • class FrontSuspensionMountainBike include MountainBike def price (1 + @comission) * @base_price + @front_suspension_price end end class FullSuspensionMountainBike include MountainBike def price (1 + @comission) * @base_price + @front_suspension_price + @rear_suspension_price end end Saturday, January 16, 2010
    • def price if @type_code == :rigid raise "should not be called" end if @type_code == :font_suspension raise "should not be called" end if @type_code == :full_suspension raise "should not be called" end end Saturday, January 16, 2010
    • def price if @type_code == :rigid raise "should not be called" end if @type_code == :font_suspension raise "should not be called" end if @type_code == :full_suspension raise "should not be called" end end Saturday, January 16, 2010
    • class RigidMountainBike include MountainBike end class FrontSuspensionMountainBike include MountainBike end class FullSuspensionMountainBike include MountainBike end Saturday, January 16, 2010
    • Only the beginning... Saturday, January 16, 2010
    • Coding Dojo http://www.flickr.com/photos/el_ser_lomo/3267627038/ http://orlandodojo.wordpress.com/ http://groups.google.com/group/orlando-dojo/ Saturday, January 16, 2010
    • Saturday, January 16, 2010
    • Thank you! @caike http://caikesouza.com Saturday, January 16, 2010