This document discusses refactoring code to improve its structure and design without changing its external behavior. It provides examples of refactoring techniques like extracting methods, replacing conditionals with polymorphism, introducing named parameters, and replacing magic numbers with symbolic constants. The goal of refactoring is to make code easier to understand and cheaper to modify over time.
4. Refatorar é...
realizar mudanças internas em um
programa funcional com o objetivo torná-lo
mais fácil de ser compreendido e mais
barato de ser modificado - sem que haja
mudanças no comportamento externo;
31. “The great thing about programming is
pretty much everyone's code is shit.
Writing software is hard, so it's no problem
finding dogs in someone's stuff”
(Zed Shaw)
59. 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
60. class Movie
def initialize...end
def recommended?
rating >= 8
end
def rating
@stars > 5 ? 8 : 1
end
end
87. class MountainBike
def price
...
end
end
MountainBike.new(:type => :rigid, ...)
MountainBike.new(:type => :front_suspension, ...)
MountainBike.new(:type => :full_suspension, ...)
88. def price
if @type == :rigid
(1 + @comission) * @base_price
end
if @type == :font_suspension
(1 + @comission) * @base_price +
@front_suspension_price
end
if @type == :full_suspension
(1 + @comission) * @base_price+
@front_suspension_price +
@rear_suspension_price
end
end
89. def price
if @type == :rigid
(1 + @comission) * @base_price
end
if @type == :font_suspension
(1 + @comission) * @base_price +
@front_suspension_price
end
if @type == :full_suspension
(1 + @comission) * @base_price+
@front_suspension_price +
@rear_suspension_price
end
if @type == :ultra_suspension
...
end
end
95. class RigidMountainBike
include MountainBike
end
class FrontSuspensionMountainBike
include MountainBike
end
class FullSuspensionMountainBike
include MountainBike
end
97. class RigidMountainBike
include MountainBike
def price
(1 + @comission) * @base_price
end
end
98. def price
if @type == :rigid
raise "should not be called"
end
if @type == :font_suspension
(1 + @comission) * @base_price +
@front_suspension_price
end
if @type == :full_suspension
(1 + @comission) * @base_price+
@front_suspension_price +
@rear_suspension_price
end
end
99. 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
100. def price
if @type == :rigid
raise "should not be called"
end
if @type == :font_suspension
raise "should not be called"
end
if @type == :full_suspension
raise "should not be called"
end
end
101. def price
if @type == :rigid
raise "should not be called"
end
if @type == :font_suspension
raise "should not be called"
end
if @type == :full_suspension
raise "should not be called"
end
end
102. class RigidMountainBike
include MountainBike
end
class FrontSuspensionMountainBike
include MountainBike
end
class FullSuspensionMountainBike
include MountainBike
end
108. Refactoring
.... porque apenas fazer
funcionar não é o suficiente.
http://www.flickr.com/photos/eurleif/255241547/
http://www.flickr.com/photos/dhammza/91435718/
http://www.flickr.com/photos/krash0387
http://www.flickr.com/photos/benfrantzdale/208672143/
http://www.flickr.com/photos/improveit/1574023621/ @caike
http://www.flickr.com/photos/aaroncoyle/972403508
http://www.flickr.com/photos/talios/3726484920
http://www.flickr.com/photos/moow/3412079622
caikesouza.com
http://www.flickr.com/photos/highwayoflife/2699887178/