ãªãã¡ã¯ã¿ãªã³ã°ã®åçåå
ãã³ã³ãã¥ã¼ã¿ãç解ã§ããã³ã¼ãã¯èª°ã§ãæ¸ãããåªããããã°ã©ããæ¸ãã®ã¯ã人éãç解ã§ããã³ã¼ãã ã
ãªãã¡ã¯ã¿ãªã³ã°ã¯ã³ã¼ãã®ä¿å®ãããä¸ã§ã¨ã£ã¦ã大åãªãã¨ã¯åãã£ã¦ãããã©ãåçååãæã£ã¦ããªããåçååã欲ããã®ã§ãæ¸ç±ãリファクタリング:Rubyエディションããèªãã§ãåçååãæã«å
¥ãããã¨ãã¦ãããã¾ã èªãã§ãéä¸ã ãã©ãã¢ã¦ãããããæ¸ãã¦ããã³ã¼ãã丸åãããã®ã§ã¯ãªããèªåãªãã«ã³ã¼ããæ¸ãç´ãäºã§ç解ããã
åçååã®åã«å¤§åãªãã¨
ãªãã¡ã¯ã¿ãªã³ã°ã§ã¯ããã¹ããå½ç¶±ã«ãªãããªãã¡ã¯ã¿ãªã³ã°ãæåãããã©ããã¯ããããã¹ããç¨æã§ãã¦ãããã©ããã«ãã£ã¦å·¦å³ãããããã¹ãã¯ãå¾ã§ããã°ã©ã ãæ¸ãæããªããã°ãªããªããªã£ãæã®å®å
¨å¼ã«ãªãã®ã§ãååã«æéãããã¦ä½ã£ã¦è¯ããããã¦ããã¹ãã¯èªå·±è¨ºæãã¹ãã§ãªããã°ãªããªãããªãã¡ã¯ã¿ãªã³ã°ã§ã¯ãããã°ã©ã ãå°ããã¤å¤æ´ãããã¨ãããããã°ãééãã¦ãç°¡åã«ãã°ãè¦ã¤ããããã
ã¡ã½ããã®æ½åºï¼Extract Methodï¼ï¼P31ï¼
æ°ããã¡ã½ããã«ã¹ã³ã¼ããéããã¦ããå¤æ°ãã¤ã¾ããã¼ã«ã«å¤æ°ã¨å¼æ°ã«ãªãã¹ããã®ãå ã®ã³ã¼ãããæ¢ãããã¨ãã°ãhuman 㨠total ãããã«å½ããã¨ãããããã®ãã¡ãhuman ã¯ã³ã¼ãå ã§å¤æ´ãããªãããtotal ã¯å¤æ´ããããå¤æ´ãããªãå¤æ°ã¯å¼æ°ã¨ãã¦æ¸¡ããããå¤æ´ãããå¤æ°ã¯ããå°ãæ éã«æ±ããªããã°ãªããªããããããå¤æ°ãï¼ã¤ã ããªããæ»ãå¤ã¨ãã¦è¿ãããä¸æå¤æ° total ã¯ãã«ã¼ãéå§æã« 0 ã«åæåããã caseæã«å ¥ãã¾ã§å¤æ´ãããªãã®ã§ãæ°ã¡ã½ããã®æ»ãå¤ãä»£å ¥åºæ¥ãã
ãªãã¡ã¯ã¿ãªã³ã°å
class Keiri def initialize @human = ["adult", "children"] end def fare_total result = "éè³åè¨ï¼ " total = 0 @human.each do | human | case human when "adult" total += 200 when "children" total += 100 end end # çµæãåºå result + "#{total}" end end keiri = Keiri.new puts keiri.fare_total
ãªãã¡ã¯ã¿ãªã³ã°å¾
class Keiri def initialize @human = ["adult", "children"] end def fare_total result = "éè³åè¨ï¼ " total = 0 @human.each do | human | total += fare(human) end # éè³åè¨ãåºå result + "#{total}" end # éè³ def fare(human_kind) case human_kind when "adult" result = 200 when "children" result = 100 end result end end keiri = Keiri.new puts keiri.fare_total
ã¡ã½ããã®ç§»åï¼Move Methodï¼ï¼P36ï¼
ã¡ã½ããã®æ½åºã®ç¶ããã¯ã©ã¹ Keiri 㯠fare ã¡ã½ãããæã£ã¦ããããå®ã¯éè³ã«ã¯è¤æ°ç¨®é¡ããããã¨ãçºè¦ããã®ãããã¯ã©ã¹ Fare ãä½æãã¦ãfareã¡ã½ããã移åãããã¨ã«ããã
ãªãã¡ã¯ã¿ãªã³ã°å
class Keiri def initialize @human = ["adult", "children"] end def fare_total result = "éè³åè¨ï¼ " total = 0 @human.each do | human | total += fare(human) end # éè³åè¨ãåºå result + "#{total}" end # éè³ def fare(human_kind) case human_kind when "adult" result = 200 when "children" result = 100 end result end end keiri = Keiri.new puts keiri.fare_total
ãªãã¡ã¯ã¿ãªã³ã°å¾
class Keiri def initialize @human = ["adult", "children"] end def bus_fare_total result = "éè³åè¨ï¼ " total = 0 @human.each do | human | total += Fare.bus(human) end # éè³åè¨ãåºå result + "#{total}" end end class Fare # ãã¹éè³ def self.bus(human_kind) case human_kind when "adult" result = 200 when "children" result = 100 end result end #é«éãã¹éè³ def self.expressway_bus(human_kind) case human_kind when "adult" result = 3000 when "children" result = 1500 end result end end keiri = Keiri.new puts keiri.bus_fare_total
ä¸æå¤æ°ããåãåããã¡ã½ããã¸ï¼Replace Temp with Queryï¼ï¼P41ï¼
以ä¸ã¯ã極端ãªä¾ã ãã»ã»ã»è¨ããããã¨ã¯åãã¯ãã
ãªãã¡ã¯ã¿ãªã³ã°å
def hoge total = 0 @human.each do |human| total = human.fare total += total end puts total end
ãªãã¡ã¯ã¿ãªã³ã°å¾
def hoge total = 0 @human.each do |human| total += human.fare end puts total end
ã«ã¼ãããã³ã¬ã¯ã·ã§ã³ã¯ãã¼ã¸ã£ã¡ã½ããã¸ï¼Replace Loop with Collection Closure Methodï¼ï¼P49ï¼
ãªãã¡ã¯ã¿ãªã³ã°å
class Hoge def total result = 0 [1, 2, 3, 4, 5].each do |n| result += n end result end
ãªãã¡ã¯ã¿ãªã³ã°å¾
class Hoge def total [1, 2, 3, 4, 5].inject(0) { |total, n| total + n } end end
æ¸ç±ç´¹ä»
ãªãã¡ã¯ã¿ãªã³ã°:Rubyã¨ãã£ã·ã§ã³
- ä½è : Jay Fields,Shane Harvie,Martin Fowler,Kent Beck,é·å°¾é«å¼
- åºç社/ã¡ã¼ã«ã¼: ã¢ã¹ãã¼ã»ã¡ãã£ã¢ã¯ã¼ã¯ã¹
- çºå£²æ¥: 2010/02/27
- ã¡ãã£ã¢: 大åæ¬
- è³¼å ¥: 9人 ã¯ãªãã¯: 321å
- ãã®ååãå«ãããã° (49件) ãè¦ã