that may be commonly used but is ineffective and/or counterproductive in practice” (Wikipedia) God Object Sequential Coupling Object Orgy Wednesday, 11 January 12
=> e logger.error(e) raise end def handle_exceptions_properly tweets = find_all_my_tweets find_tweets_from_ddh(tweets) rescue Timeout::Error => e retry rescue Net::HTTPServerError => e wait and retry rescue Exception => e logger.error(e) raise end Wednesday, 11 January 12
"mm eating cake" end def method_missing(method, *args, &block) if method.to_s =~ /^cake/ puts "mmm cake" else super end end end puts Cake.new.respond_to? :eat # true puts Cake.new.respond_to? :cake # false Wednesday, 11 January 12
true if method =~ /^cake/ super end def method_missing(method, *args, &block) if method.to_s =~ /^cake/ puts "mmm cake" else super end end end puts Cake.new.respond_to? :cake # false Wednesday, 11 January 12
find_all_the_cakes(bakery_produce) cakes = [] bakery_produce.each do |potential_cake| cakes << potential_cake if is_cake?(potential_cake) end cakes end def find_a_cake(bakery_produce) bakery_produce.each do |potential_cake| return potential_cake if is_cake?(potential_cake) end end Wednesday, 11 January 12
find_all_the_cakes(bakery_produce) bakery_produce.select{|potential_cake| is_cake?(potential_cake)} end array[0] vs array.first def find_a_cake(bakery_produce) bakery_produce.detect{|potential_cake| is_cake?(potential_cake)} end Wednesday, 11 January 12
• You can play on your own. • You can play with your own toys (but you can’t take them apart). • You can play with toys that were given to you. • And you can play with toys you’ve made yourself. Wednesday, 11 January 12
/omnomnom/ content_tag(:h4, "Mmm Delicious") else content_tag(:h4, "Average") end end def cake_servings(cake) if cake.slices? content_tag(:h4, cake.slices ) else content_tag(:h4, "The cake has been eated!" ) end end <%= cake_deliciousness(@cake) %> => <h4> Mmm Delicious </h4> <%= cake_servings(@cake) %> => <h4> The cake has been eated! </h4> cake.html.erb cake_helper.rb a cake can only be tested by eating it! Wednesday, 11 January 12
Search.new(params) respond_with @search end end Keep it simple Break out complex logic into models One controller per resource Wednesday, 11 January 12
person.munch(self) until self.empty? end #Distribution methods def slices(degrees_per_slice) 360/degrees_per_slice end #Representation Methods def to_xml ... end def to_html ... end def as_json ... end end Wednesday, 11 January 12
def to_xml ... end def to_html ... end def as_json ... end end def CakeSlicer def initialize(cake) @cake = cake end def slices(degrees_per_slice) 360/degrees_per_slice end end Wednesday, 11 January 12