Refactoring
-
Upload
caike-souza -
Category
Technology
-
view
2.482 -
download
0
description
Transcript of Refactoring
Refactoring{ :from => :mess, :to => :clean_code, :through => :patterns }
Orlando Ruby User GroupJanuary 2010
@caikehttp://caikesouza.com
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 worksWhen I refactor
Then it should be easier to understandAnd still work!
Friday, January 15, 2010
Cleaning up the house
Friday, January 15, 2010
http://www.flickr.com/photos/improveit/1574023621/
http://www.flickr.com/photos/benfrantzdale/208672143/
Friday, January 15, 2010
wiki, Fit, JUnit
eXtreme Programming
http://www.flickr.com/photos/improveit/1574023621/
http://www.flickr.com/photos/benfrantzdale/208672143/
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
Money++Respond to changes
Friday, January 15, 2010
Money++Respond to changes
Release with confidence
Friday, January 15, 2010
Effort--
Friday, January 15, 2010
Jan Feb Mar Apr May Jun Jul
Cost of Maintenance
Waterfall
Extreme Programming Explained: Embrace ChangeAddison Wesley, 2000
Friday, January 15, 2010
Jan Feb Mar Apr May Jun Jul
Cost of Maintenance Extreme Programming Explained: Embrace ChangeAddison Wesley, 2000
XP
Friday, January 15, 2010
http://www.flickr.com/photos/x180/503574487/
code LESSsleep MORE
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
Code Smells
Duplicated Code
Friday, January 15, 2010
Long Method
Duplicated Code
Code Smells
Friday, January 15, 2010
Duplicated Code
Long Method
Large ClassCode Smells
Friday, January 15, 2010
Duplicated Code
Long Method
Divergent ChangeLarge ClassCode Smells
Friday, January 15, 2010
Duplicated Code
Long Method
Divergent ChangeLarge Class
Long ParameterList
Code Smells
Friday, January 15, 2010
Test Driven Development
Friday, January 15, 2010
RedGreen
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 withClosure 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 endend
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 endend
Saturday, January 16, 2010
Introduce ExplainingVariable
Saturday, January 16, 2010
class Movie def initialize(stars) @stars = stars end def recommended? ((@stars > 5) ? 8 : 1) >= 8 endend
Saturday, January 16, 2010
class Movie def initialize(stars) @stars = stars end def recommended? rating = (@stars > 5) ? 8 : 1 rating >= 8 endend
Saturday, January 16, 2010
class Movie def initialize(stars) @stars = stars end def recommended? rating = (@stars > 5) ? 8 : 1 rating >= 8 endend
Saturday, January 16, 2010
Replace TempWith Query
Saturday, January 16, 2010
class Movie def initialize(stars) @stars = stars end def recommended? rating = (@stars > 5) ? 8 : 1 rating >= 8 endend
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
endSaturday, 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 TempWith 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 ... selfend
def with ... selfend
def returns ... selfend
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 endend
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 endend
Saturday, January 16, 2010
Extract SurroundingMethod
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 endend
Saturday, January 16, 2010
def charge(amount, ccnumber) connect do |conn| conn.send(amount, ccnumber) endend
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 endend
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 ... endend
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
endSaturday, 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_priceend 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 ConditionalWith Polymorphism
Saturday, January 16, 2010
class MountainBike def price ... endend
Saturday, January 16, 2010
module MountainBike def price ... endend
Saturday, January 16, 2010
class RigidMountainBike include MountainBikeend
class FrontSuspensionMountainBike include MountainBikeend
class FullSuspensionMountainBike include MountainBikeend
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 endend
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 endend
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 MountainBikeend
class FrontSuspensionMountainBike include MountainBikeend
class FullSuspensionMountainBike include MountainBikeend
Saturday, January 16, 2010
Only the beginning...Saturday, January 16, 2010
http://www.flickr.com/photos/el_ser_lomo/3267627038/
Coding Dojo
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