A presentation on code refactoring that I gave at Seattle.rb in October, 2012. There is also a behavioral driven development example from my work on the CapGun http://capgun.io/ service.
Example Logic defining the state of the object is contained outside of it package.data.size == 0 package.data.size < 12 package.data.size == 12 Tuesday, January 8, 13
package.data.size == 12 || package.data.size == 13 Example • Hard to find all places of code implementing outside logic • Object doesn’t control it’s own logical state Tuesday, January 8, 13
Query? Method Pattern •Object tells you it’s state •Method is intention revealing •Is human readable •Examples: obj.nil? ... num.zero? Tuesday, January 8, 13
Example Write tests first it "can be empty" do @package.empty?.must_equal true end it "can't be partial" do @package.partial?.must_equal false end it "can't be complete" do @package.complete?.must_equal false end Tuesday, January 8, 13
Example Implementation, State Is In A Single Location def empty? data.size.zero? end def partial? !empty? && data.size < 12 end def complete? data.size == 12 || data.size == 13 end Tuesday, January 8, 13
Example Mock or override #data method in specs - clears away distractions before do @package = Package.new @package.extend(SpecEmpty) end it "can be empty" do @package.empty?.must_equal true end Tuesday, January 8, 13
Simple Mistakes Compound Upon Themselves • logic was external to object • external logic is hard to maintain/find • we had no tests • external data implementation had to be addressed in testing Tuesday, January 8, 13
Pro-Tips • Refactoring Favors Behavior Rather Than Logic • Writing code that can be unit tested changes your writing style • If a method can’t be unit tested it must be refactored • Factor out interaction with frameworks from code that is tested Tuesday, January 8, 13
Start Somewhere Feature: Order Estimates As an API client In order to list information about orders I want to be able to query the order API Background: Given I am a valid API user And I send and accept JSON Scenario: getting an estimate for a base screen capping job When I send a POST request to "/api/v1/orders/estimate" with the following body: |{ "url": "http://example.com/cucumber#test" }| Then the response should be "200" And the JSON response should not have "order/job" And the JSON response should have an estimate of 1 credits Tuesday, January 8, 13
Start Somewhere describe "Estimate" do describe "json" do should "estimate capping a URL for 1 credit" do post '/estimate.json', '{"url":"http://www.seattlerb.org/"}' JSON.parse(last_response.body).should == { 'order' { 'id' '4fd20a1288f560177600000a' 'url' 'http://www.seattle.rb/' 'cost' 1 } } end end end Tuesday, January 8, 13
Write Code That Is Used Immediately, Do Not Future Proof It class Order def cost end end describe Order do describe "defaults" do it "should cost 1 credit" do subject.cost.should == 1 end end end Tuesday, January 8, 13
Image Attribution @monde Gavatar by Mike Mondragon https://secure.gravatar.com/avatar/74c036cfbef785b139d06ccad4687b44?s=420&d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-user-420.png Phil Hagelberg Seattle.rb shirt by veganstraightedge http://www.flickr.com/photos/veganstraightedge/7843002706/in/set-72157631203475428 Amazon cover of “Refactoring” book http://www.amazon.com/Refactoring-Improving-Design-Existing-Code/dp/0201485672 Wallace the cat cradled like a baby - Mike Mondragon Xzibit head shot - The Internet Wallace - THE INTERNETS is watching - Mike Mondragon on icanhazcheezburger Green field @ wallpaperstock.net http://wallpaperstock.net/green-field_wallpapers_8739_1024x768_1.html Palouse wheat field - unknown Palouse wheat field - unknown Tilled field - unknown CapGun logo - Mike Mondragon Skitch screen cap of CapGun panel - Mike Mondragon Tilled field - unknown Kitten asleep in food dish http://catlovingcare.com/2042/blog/loosing-weight-tips-for-fat-cats/2009/12/ Skitch screen cap of http://tublr.capgun.io/ - Mike Mondragon CapGun.io result for Seattlerb.org - Mike Mondragon Wallace the cat profile - Mike Mondragon Tuesday, January 8, 13