Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Therapeutic Refactoring

Therapeutic Refactoring

Enter deadline center stage, exit best practices, quietly, rear stage left.

The results are rarely pretty.

Refactoring can pry panic’s fingers away from your poor, overburdened adrenal glands and restore your sanity. Not that it went missing, of course. Never that!

This talk will cover the two reasons why refactoring works as well as (or better than) whiskey, sky diving, and massages as therapy, explore a handful of effective strategies to ensure that the rubber meets the road, and contains gory before shots and slick after shots of ruby code that has served therapeutic purpose.

Katrina Owen

June 15, 2012
Tweet

More Decks by Katrina Owen

Other Decks in Programming

Transcript

  1. Illustration from There’s a Lesson in Here Somewhere, I Think

    by Allie Brosh http://hyperboleandahalf.blogspot.com/2010/01/theres-lesson-in-here-somewhere-i- think.html
  2. Illustration from There’s a Lesson in Here Somewhere, I Think

    by Allie Brosh http://hyperboleandahalf.blogspot.com/2010/01/theres-lesson-in-here-somewhere-i- think.html
  3. module XYZService def self.xyz_filename(target) # File format: # [day of

    month zero-padded][three-letter prefix] \ # _[kind]_[age_if_kind_personal]_[target.id] \ # _[8 random chars]_[10 first chars of title].jpg filename = "#{target.publish_on.strftime("%d")}" filename << "#{target.xyz_category_prefix}" filename << "#{target.kind.gsub("_", "")}" filename << "_%03d" % (target.age || 0) if target.personal? filename << "_#{target.id.to_s}" filename << "_#{Digest::SHA1.hexdigest(rand(10000).to_s)[0,8]}" truncated_title = target.title.gsub(/[^\[a-z\]]/i, '').downcase length = truncated_title.length truncate_to = length > 9 ? 9 : length filename << "_#{truncated_title[0..(truncate_to)]}" filename << ".jpg" return filename end end
  4. module XYZService def self.xyz_filename(target) # File format: # [day of

    month zero-padded][three-letter prefix] \ # _[kind]_[age_if_kind_personal]_[target.id] \ # _[8 random chars]_[10 first chars of title].jpg filename = "#{target.publish_on.strftime("%d")}" filename << "#{target.xyz_category_prefix}" filename << "#{target.kind.gsub("_", "")}" filename << "_%03d" % (target.age || 0) if target.personal? filename << "_#{target.id.to_s}" filename << "_#{Digest::SHA1.hexdigest(rand(10000).to_s)[0,8]}" truncated_title = target.title.gsub(/[^\[a-z\]]/i, '').downcase length = truncated_title.length truncate_to = length > 9 ? 9 : length filename << "_#{truncated_title[0..(truncate_to)]}" filename << ".jpg" return filename end end
  5. module XYZService def self.xyz_filename(target) # File format: # [day of

    month zero-padded][three-letter prefix] \ # _[kind]_[age_if_kind_personal]_[target.id] \ # _[8 random chars]_[10 first chars of title].jpg filename = "#{target.publish_on.strftime("%d")}" filename << "#{target.xyz_category_prefix}" filename << "#{target.kind.gsub("_", "")}" filename << "_%03d" % (target.age || 0) if target.personal? filename << "_#{target.id.to_s}" filename << "_#{Digest::SHA1.hexdigest(rand(10000).to_s)[0,8]}" truncated_title = target.title.gsub(/[^\[a-z\]]/i, '').downcase length = truncated_title.length truncate_to = length > 9 ? 9 : length filename << "_#{truncated_title[0..(truncate_to)]}" filename << ".jpg" return filename end end
  6. module XYZService def self.xyz_filename(target) # File format: # [day of

    month zero-padded][three-letter prefix] \ # _[kind]_[age_if_kind_personal]_[target.id] \ # _[8 random chars]_[10 first chars of title].jpg filename = "#{target.publish_on.strftime("%d")}" filename << "#{target.xyz_category_prefix}" filename << "#{target.kind.gsub("_", "")}" filename << "_%03d" % (target.age || 0) if target.personal? filename << "_#{target.id.to_s}" filename << "_#{Digest::SHA1.hexdigest(rand(10000).to_s)[0,8]}" truncated_title = target.title.gsub(/[^\[a-z\]]/i, '').downcase length = truncated_title.length truncate_to = length > 9 ? 9 : length filename << "_#{truncated_title[0..(truncate_to)]}" filename << ".jpg" return filename end end
  7. module XYZService def self.xyz_filename(target) # File format: # [day of

    month zero-padded][three-letter prefix] \ # _[kind]_[age_if_kind_personal]_[target.id] \ # _[8 random chars]_[10 first chars of title].jpg filename = "#{target.publish_on.strftime("%d")}" filename << "#{target.xyz_category_prefix}" filename << "#{target.kind.gsub("_", "")}" filename << "_%03d" % (target.age || 0) if target.personal? filename << "_#{target.id.to_s}" filename << "_#{Digest::SHA1.hexdigest(rand(10000).to_s)[0,8]}" truncated_title = target.title.gsub(/[^\[a-z\]]/i, '').downcase length = truncated_title.length truncate_to = length > 9 ? 9 : length filename << "_#{truncated_title[0..(truncate_to)]}" filename << ".jpg" return filename end end
  8. module XYZService def self.xyz_filename(target) # File format: # [day of

    month zero-padded][three-letter prefix] \ # _[kind]_[age_if_kind_personal]_[target.id] \ # _[8 random chars]_[10 first chars of title].jpg filename = "#{target.publish_on.strftime("%d")}" filename << "#{target.xyz_category_prefix}" filename << "#{target.kind.gsub("_", "")}" filename << "_%03d" % (target.age || 0) if target.personal? filename << "_#{target.id.to_s}" filename << "_#{Digest::SHA1.hexdigest(rand(10000).to_s)[0,8]}" truncated_title = target.title.gsub(/[^\[a-z\]]/i, '').downcase length = truncated_title.length truncate_to = length > 9 ? 9 : length filename << "_#{truncated_title[0..(truncate_to)]}" filename << ".jpg" return filename end end
  9. module XYZService def self.xyz_filename(target) # File format: # [day of

    month zero-padded][three-letter prefix] \ # _[kind]_[age_if_kind_personal]_[target.id] \ # _[8 random chars]_[10 first chars of title].jpg filename = "#{target.publish_on.strftime("%d")}" filename << "#{target.xyz_category_prefix}" filename << "#{target.kind.gsub("_", "")}" filename << "_%03d" % (target.age || 0) if target.personal? filename << "_#{target.id.to_s}" filename << "_#{Digest::SHA1.hexdigest(rand(10000).to_s)[0,8]}" truncated_title = target.title.gsub(/[^\[a-z\]]/i, '').downcase length = truncated_title.length truncate_to = length > 9 ? 9 : length filename << "_#{truncated_title[0..(truncate_to)]}" filename << ".jpg" return filename end end
  10. module XYZService def self.xyz_filename(target) # File format: # [day of

    month zero-padded][three-letter prefix] \ # _[kind]_[age_if_kind_personal]_[target.id] \ # _[8 random chars]_[10 first chars of title].jpg filename = "#{target.publish_on.strftime("%d")}" filename << "#{target.xyz_category_prefix}" filename << "#{target.kind.gsub("_", "")}" filename << "_%03d" % (target.age || 0) if target.personal? filename << "_#{target.id.to_s}" filename << "_#{Digest::SHA1.hexdigest(rand(10000).to_s)[0,8]}" truncated_title = target.title.gsub(/[^\[a-z\]]/i, '').downcase length = truncated_title.length truncate_to = length > 9 ? 9 : length filename << "_#{truncated_title[0..(truncate_to)]}" filename << ".jpg" return filename end end
  11. Failures: 1) XYZService works Failure/Error: subject.should eq("something") Stub received unexpected

    message :publish_on with (no args) # ./xyz_service.rb:6:in `xyz_filename'
  12. Failures: 1) XYZService works Failure/Error: subject.should eq("something") Stub received unexpected

    message :publish_on with (no args) # ./xyz_service.rb:6:in `xyz_filename'
  13. Failures: 1) XYZService works Failure/Error: subject.should eq("something") Stub received unexpected

    message :publish_on with (no args) # ./xyz_service.rb:6:in `xyz_filename'
  14. module XYZService def self.xyz_filename(target) # File format: # [day of

    month zero-padded][three-letter prefix] \ # _[kind]_[age_if_kind_personal]_[target.id] \ # _[8 random chars]_[10 first chars of title].jpg filename = "#{target.publish_on.strftime("%d")}" filename << "#{target.xyz_category_prefix}" filename << "#{target.kind.gsub("_", "")}" filename << "_%03d" % (target.age || 0) if target.personal? filename << "_#{target.id.to_s}" filename << "_#{Digest::SHA1.hexdigest(rand(10000).to_s)[0,8]}" truncated_title = target.title.gsub(/[^\[a-z\]]/i, '').downcase length = truncated_title.length truncate_to = length > 9 ? 9 : length filename << "_#{truncated_title[0..(truncate_to)]}" filename << ".jpg" return filename end end
  15. describe XYZService do let(:target) do messages = { } stub(:target,

    messages) end subject { XYZService.xyz_filename(target) } it "works" do subject.should eq("something") end end
  16. describe XYZService do let(:target) do messages = { :publish_on =>

    ? } stub(:target, messages) end subject { XYZService.xyz_filename(target) } it "works" do subject.should eq("something") end end
  17. describe XYZService do let(:target) do messages = { :publish_on =>

    Date.new(2012, 3, 14) } stub(:target, messages) end subject { XYZService.xyz_filename(target) } it "works" do subject.should eq("something") end end
  18. Failures: 1) XYZService works Failure/Error: subject.should eq("something") expected: "something" got:

    "14abcunicorn_1337_3f4894ca_magicsuper.jpg" (compared using ==) # ./xyz_file_spec.rb:18:in `block (2 levels) in <top (required)>
  19. Failures: 1) XYZService works Failure/Error: subject.should eq("something") expected: "something" got:

    "14abcunicorn_42_3f4894ca_magicsuper.jpg" (compared using ==) # ./xyz_file_spec.rb:18:in `block (2 levels) in <top (required)> #WIN
  20. let(:target) do messages = { :publish_on => Date.new(2012, 3, 14),

    :xyz_category_prefix => 'abc', :kind => 'unicorn', :personal? => false, :id => 1337, :title => 'magic & superglue' } stub(:target, messages) end
  21. Failures: 1) XYZService works Failure/Error: subject.should eq("something") expected: "something" got:

    "14abcunicorn_1337_3f4894ca_magicsuper.jpg" (compared using ==) # ./xyz_file_spec.rb:18:in `block (2 levels) in <top (required)>
  22. Failures: 1) XYZService works Failure/Error: subject.should eq("something") expected: "something" got:

    "14abcunicorn_1337_3f4894ca_magicsuper.jpg" (compared using ==) # ./xyz_file_spec.rb:18:in `block (2 levels) in <top (required)>
  23. def self.xyz_filename(target) # File format: # [day of month zero-padded][three-letter

    prefix] \ # _[kind]_[age_if_kind_personal]_[target.id] \ # _[8 random chars]_[10 first chars of title].jpg filename = "#{target.publish_on.strftime("%d")}" filename << "#{target.xyz_category_prefix}" filename << "#{target.kind.gsub("_", "")}" filename << "_%03d" % (target.age || 0) if target.personal? filename << "_#{target.id.to_s}" filename << "_#{Digest::SHA1.hexdigest(rand(10000).to_s)[0,8]}" truncated_title = target.title.gsub(/[^\[a-z\]]/i, '').downcase length = truncated_title.length truncate_to = length > 9 ? 9 : length filename << "_#{truncated_title[0..(truncate_to)]}" filename << ".jpg" return filename end
  24. def self.xyz_filename(target) # File format: # [day of month zero-padded][three-letter

    prefix] \ # _[kind]_[age_if_kind_personal]_[target.id] \ # _[8 random chars]_[10 first chars of title].jpg filename = "#{target.publish_on.strftime("%d")}" filename << "#{target.xyz_category_prefix}" filename << "#{target.kind.gsub("_", "")}" filename << "_%03d" % (target.age || 0) if target.personal? filename << "_#{target.id.to_s}" filename << "_#{Digest::SHA1.hexdigest(rand(10000).to_s)[0,8]}" truncated_title = target.title.gsub(/[^\[a-z\]]/i, '').downcase length = truncated_title.length truncate_to = length > 9 ? 9 : length filename << "_#{truncated_title[0..(truncate_to)]}" filename << ".jpg" return filename end
  25. def self.xyz_filename(target) # File format: # [day of month zero-padded][three-letter

    prefix] \ # _[kind]_[age_if_kind_personal]_[target.id] \ # _[8 random chars]_[10 first chars of title].jpg filename = "#{target.publish_on.strftime("%d")}" filename << "#{target.xyz_category_prefix}" filename << "#{target.kind.gsub("_", "")}" filename << "_%03d" % (target.age || 0) if target.personal? filename << "_#{target.id.to_s}" filename << "_#{Digest::SHA1.hexdigest(rand(10000).to_s)[0,8]}" truncated_title = target.title.gsub(/[^\[a-z\]]/i, '').downcase length = truncated_title.length truncate_to = length > 9 ? 9 : length filename << "_#{truncated_title[0..(truncate_to)]}" filename << ".jpg" return filename end
  26. def self.xyz_filename(target) # File format: # [day of month zero-padded][three-letter

    prefix] \ # _[kind]_[age_if_kind_personal]_[target.id] \ # _[8 random chars]_[10 first chars of title].jpg filename = "#{target.publish_on.strftime("%d")}" filename << "#{target.xyz_category_prefix}" filename << "#{target.kind.gsub("_", "")}" filename << "_%03d" % (target.age || 0) if target.personal? filename << "_#{target.id.to_s}" filename << "_#{Digest::SHA1.hexdigest(rand(10000).to_s)[0,8]}" truncated_title = target.title.gsub(/[^\[a-z\]]/i, '').downcase length = truncated_title.length truncate_to = length > 9 ? 9 : length filename << "_#{truncated_title[0..(truncate_to)]}" filename << ".jpg" return filename end
  27. def self.xyz_filename(target) # File format: # [day of month zero-padded][three-letter

    prefix] \ # _[kind]_[age_if_kind_personal]_[target.id] \ # _[8 random chars]_[10 first chars of title].jpg filename = "#{target.publish_on.strftime("%d")}" filename << "#{target.xyz_category_prefix}" filename << "#{target.kind.gsub("_", "")}" filename << "_%03d" % (target.age || 0) if target.personal? filename << "_#{target.id.to_s}" filename << "_#{Digest::SHA1.hexdigest(rand(10000).to_s)[0,8]}" truncated_title = target.title.gsub(/[^\[a-z\]]/i, '').downcase length = truncated_title.length truncate_to = length > 9 ? 9 : length filename << "_#{truncated_title[0..(truncate_to)]}" filename << ".jpg" return filename end
  28. def self.xyz_filename(target) # File format: # [day of month zero-padded][three-letter

    prefix] \ # _[kind]_[age_if_kind_personal]_[target.id] \ # _[8 random chars]_[10 first chars of title].jpg filename = "#{target.publish_on.strftime("%d")}" filename << "#{target.xyz_category_prefix}" filename << "#{target.kind.gsub("_", "")}" filename << "_%03d" % (target.age || 0) if target.personal? filename << "_#{target.id.to_s}" filename << "_#{Digest::SHA1.hexdigest(rand(10000).to_s)[0,8]}" truncated_title = target.title.gsub(/[^\[a-z\]]/i, '').downcase length = truncated_title.length truncate_to = length > 9 ? 9 : length filename << "_#{truncated_title[0..(truncate_to)]}" filename << ".jpg" return filename end
  29. it "works"{ ... } it "leaves square brackets"{ ... }

    it "personalizes"{ ... } it "handles nil age"
  30. def self.xyz_filename(target) # File format: # [day of month zero-padded][three-letter

    prefix] \ # _[kind]_[age_if_kind_personal]_[target.id] \ # _[8 random chars]_[10 first chars of title].jpg filename = "#{target.publish_on.strftime("%d")}" filename << "#{target.xyz_category_prefix}" filename << "#{target.kind.gsub("_", "")}" filename << "_%03d" % (target.age || 0) if target.personal? filename << "_#{target.id.to_s}" filename << "_#{Digest::SHA1.hexdigest(rand(10000).to_s)[0,8]}" truncated_title = target.title.gsub(/[^\[a-z\]]/i, '').downcase length = truncated_title.length truncate_to = length > 9 ? 9 : length filename << "_#{truncated_title[0..(truncate_to)]}" filename << ".jpg" return filename end
  31. it "works" { ... } it "leaves square brackets" {

    ... } it "personalizes" { ... } it "handles nil age" { ... } it "handles short titles"
  32. Illustration from This is Why I’ll Never be an Adult

    by Allie Brosh http://hyperboleandahalf.blogspot.com/2010/06/this-is-why-ill-never-be-adult.html
  33. class XYZFile attr_reader :target def initialize(target) @target = target end

    def self.xyz_filename(target) # File format: # [day of month zero-padded][three-letter prefix] \ # _[kind]_[age_if_kind_personal]_[target.id] \ # _[8 random chars]_[10 first chars of title].jpg filename = "#{target.publish_on.strftime("%d")}" filename << "#{target.xyz_category_prefix}" filename << "#{target.kind.gsub("_", "")}" filename << "_%03d" % (target.age || 0) if target.personal? filename << "_#{target.id.to_s}" filename << "_#{Digest::SHA1.hexdigest(rand(10000).to_s)[0,8]}" truncated_title = target.title.gsub(/[^\[a-z\]]/i, '').downcase length = truncated_title.length truncate_to = length > 9 ? 9 : length filename << "_#{truncated_title[0..(truncate_to)]}" filename << ".jpg" return filename end
  34. module XYZService def self.xyz_filename(target) # File format: # [day of

    month zero-padded][three-letter prefix] \ # _[kind]_[age_if_kind_personal]_[target.id] \ # _[8 random chars]_[10 first chars of title].jpg filename = "#{target.publish_on.strftime("%d")}" filename << "#{target.xyz_category_prefix}" filename << "#{target.kind.gsub("_", "")}" filename << "_%03d" % (target.age || 0) if target.personal? filename << "_#{target.id.to_s}" filename << "_#{Digest::SHA1.hexdigest(rand(10000).to_s)[0,8]}" truncated_title = target.title.gsub(/[^\[a-z\]]/i, '').downcase length = truncated_title.length truncate_to = length > 9 ? 9 : length filename << "_#{truncated_title[0..(truncate_to)]}" filename << ".jpg" return filename end end
  35. class XYZFile ... def name # File format: # [day

    of month zero-padded][three-letter prefix] \ # _[kind]_[age_if_kind_personal]_[target.id] \ # _[8 random chars]_[10 first chars of title].jpg filename = "#{target.publish_on.strftime("%d")}" filename << "#{target.xyz_category_prefix}" filename << "#{target.kind.gsub("_", "")}" filename << "_%03d" % (target.age || 0) if target.personal? filename << "_#{target.id.to_s}" filename << "_#{Digest::SHA1.hexdigest(rand(10000).to_s)[0,8]}" truncated_title = target.title.gsub(/[^\[a-z\]]/i, '').downcase length = truncated_title.length truncate_to = length > 9 ? 9 : length filename << "_#{truncated_title[0..(truncate_to)]}" filename << ".jpg" return filename end end
  36. class XYZFile ... def name # File format: # [day

    of month zero-padded][three-letter prefix] \ # _[kind]_[age_if_kind_personal]_[target.id] \ # _[8 random chars]_[10 first chars of title].jpg filename = "#{target.publish_on.strftime("%d")}" filename << "#{target.xyz_category_prefix}" filename << "#{target.kind.gsub("_", "")}" filename << "_%03d" % (target.age || 0) if target.personal? filename << "_#{target.id.to_s}" filename << "_#{Digest::SHA1.hexdigest(rand(10000).to_s)[0,8]}" truncated_title = target.title.gsub(/[^\[a-z\]]/i, '').downcase length = truncated_title.length truncate_to = length > 9 ? 9 : length filename << "_#{truncated_title[0..(truncate_to)]}" filename << ".jpg" return filename end end
  37. class XYZFile ... def name filename = "#{target.publish_on.strftime("%d")}" filename <<

    "#{target.xyz_category_prefix}" filename << "#{target.kind.gsub("_", "")}" filename << "_%03d" % (target.age || 0) if target.personal? filename << "_#{target.id.to_s}" filename << "_#{Digest::SHA1.hexdigest(rand(10000).to_s)[0,8]}" truncated_title = target.title.gsub(/[^\[a-z\]]/i, '').downcase length = truncated_title.length truncate_to = length > 9 ? 9 : length filename << "_#{truncated_title[0..(truncate_to)]}" filename << ".jpg" return filename end end
  38. class XYZFile ... def name filename = "#{target.publish_on.strftime("%d")}" filename <<

    "#{target.xyz_category_prefix}" filename << "#{target.kind.gsub("_", "")}" filename << "_%03d" % (target.age || 0) if target.personal? filename << "_#{target.id.to_s}" filename << "_#{Digest::SHA1.hexdigest(rand(10000).to_s)[0,8]}" truncated_title = target.title.gsub(/[^\[a-z\]]/i, '').downcase length = truncated_title.length truncate_to = length > 9 ? 9 : length filename << "_#{truncated_title[0..(truncate_to)]}" filename << ".jpg" return filename end end
  39. class XYZFile ... def name ... truncated_title = target.title.gsub(/[^\[a-z\]]/i, '').downcase

    length = truncated_title.length truncate_to = length > 9 ? 9 : length filename << "_#{truncated_title[0..(truncate_to)]}" ... end end
  40. class XYZFile ... def name ... truncated_title = target.title.gsub(/[^\[a-z\]]/i, '').downcase

    length = truncated_title.length truncate_to = length > 9 ? 9 : length filename << "_#{truncated_title[0..(truncate_to)]}" ... end end
  41. class XYZFile ... def name ... truncated_title = target.title.gsub(/[^\[a-z\]]/i, '').downcase

    length = truncated_title.length truncate_to = length > 9 ? 9 : length filename << "_#{truncated_title[0..(truncate_to)]}" ... end def truncated_title end end
  42. class XYZFile ... def name ... truncated_title = target.title.gsub(/[^\[a-z\]]/i, '').downcase

    length = truncated_title.length truncate_to = length > 9 ? 9 : length filename << "_#{truncated_title[0..(truncate_to)]}" ... end def truncated_title truncated_title = target.title.gsub(/[^\[a-z\]]/i, '').downcase length = truncated_title.length truncate_to = length > 9 ? 9 : length filename << "_#{truncated_title[0..(truncate_to)]}" end end
  43. class XYZFile ... def name... def truncated_title truncated_title = target.title.gsub(/[^\[a-z\]]/i,

    '').downcase length = truncated_title.length truncate_to = length > 9 ? 9 : length filename << "_#{truncated_title[0..(truncate_to)]}" end end
  44. class XYZFile ... def name... def truncated_title truncated_title = target.title.gsub(/[^\[a-z\]]/i,

    '').downcase length = truncated_title.length truncate_to = length > 9 ? 9 : length filename << "_#{truncated_title[0..(truncate_to)]}" end end
  45. class XYZFile ... def name... def truncated_title truncated_title = target.title.gsub(/[^\[a-z\]]/i,

    '').downcase length = truncated_title.length truncate_to = length > 9 ? 9 : length filename << "_#{truncated_title[0..(truncate_to)]}" end end
  46. class XYZFile ... def name... def truncated_title truncated_title = target.title.gsub(/[^\[a-z\]]/i,

    '').downcase length = truncated_title.length truncate_to = length > 9 ? 9 : length truncated_title[0..(truncate_to)] end end
  47. class XYZFile ... def name... def truncated_title truncated_title = target.title.gsub(/[^\[a-z\]]/i,

    '').downcase length = truncated_title.length truncate_to = length > 9 ? 9 : length truncated_title[0..(truncate_to)] end end
  48. class XYZFile ... def name ... truncated_title = target.title.gsub(/[^\[a-z\]]/i, '').downcase

    length = truncated_title.length truncate_to = length > 9 ? 9 : length filename << "_#{truncated_title[0..(truncate_to)]}" ... end def truncated_title... end
  49. class XYZFile ... def name ... truncated_title = target.title.gsub(/[^\[a-z\]]/i, '').downcase

    length = truncated_title.length truncate_to = length > 9 ? 9 : length filename << "_#{truncated_title}" ... end def truncated_title... end
  50. class XYZFile ... def name ... truncated_title = target.title.gsub(/[^\[a-z\]]/i, '').downcase

    length = truncated_title.length truncate_to = length > 9 ? 9 : length filename << "_#{truncated_title}" ... end def truncated_title... end
  51. class XYZFile ... def name filename = "#{target.publish_on.strftime("%d")}" filename <<

    "#{target.xyz_category_prefix}" filename << "#{target.kind.gsub("_", "")}" filename << "_%03d" % (target.age || 0) if target.personal? filename << "_#{target.id.to_s}" filename << "_#{Digest::SHA1.hexdigest(rand(10000).to_s)[0,8]}" filename << "_#{truncated_title}" filename << ".jpg" return filename end def truncated_title... end
  52. class XYZFile ... def truncated_title truncated_title = target.title.gsub(/[^\[a-z\]]/i, '').downcase length

    = truncated_title.length truncate_to = length > 9 ? 9 : length truncated_title[0..(truncate_to)] end end
  53. class XYZFile ... def truncated_title truncated_title = target.title.gsub(/[^\[a-z\]]/i, '').downcase length

    = truncated_title.length truncate_to = length > 9 ? 9 : length truncated_title[0..(truncate_to)] end end
  54. class XYZFile ... def name filename = "#{target.publish_on.strftime("%d")}" filename <<

    "#{target.xyz_category_prefix}" filename << "#{target.kind.gsub("_", "")}" filename << "_%03d" % (target.age || 0) if target.personal? filename << "_#{target.id.to_s}" filename << "_#{Digest::SHA1.hexdigest(rand(10000).to_s)[0,8]}" filename << "_#{truncated_title}" filename << ".jpg" return filename end def truncated_title... end
  55. class XYZFile ... def name filename = "#{target.publish_on.strftime("%d")}" filename <<

    "#{target.xyz_category_prefix}" filename << "#{target.kind.gsub("_", "")}" filename << "_%03d" % (target.age || 0) if target.personal? filename << "_#{target.id.to_s}" filename << "_#{Digest::SHA1.hexdigest(rand(10000).to_s)[0,8]}" filename << "_#{truncated_title}" filename << ".jpg" return filename end def truncated_title... end
  56. class XYZFile ... def name ... filename << "_#{Digest::SHA1.hexdigest(rand(10000).to_s)[0,8]}" ...

    end def noise Digest::SHA1.hexdigest(rand(10000).to_s)[0,8] end def truncated_title... end
  57. class XYZFile ... def name ... filename << "_#{noise}" ...

    end def noise Digest::SHA1.hexdigest(rand(10000).to_s)[0,8] end def truncated_title... end
  58. class XYZFile ... def name filename = "#{target.publish_on.strftime("%d")}" filename <<

    "#{target.xyz_category_prefix}" filename << "#{target.kind.gsub("_", "")}" filename << "_%03d" % (target.age || 0) if target.personal? filename << "_#{target.id.to_s}" filename << "_#{noise}" filename << "_#{truncated_title}" filename << ".jpg" return filename end def noise... def truncated_title... end
  59. class XYZFile ... def name filename = "#{target.publish_on.strftime("%d")}" filename <<

    "#{target.xyz_category_prefix}" filename << "#{target.kind.gsub("_", "")}" filename << "_%03d" % (target.age || 0) if target.personal? filename << "_#{target.id}" filename << "_#{noise}" filename << "_#{truncated_title}" filename << ".jpg" return filename end def noise... def truncated_title... end
  60. class XYZFile ... def name filename = "#{target.publish_on.strftime("%d")}" filename <<

    "#{target.xyz_category_prefix}" filename << "#{target.kind.gsub("_", "")}" filename << "_#{age}" if target.personal? filename << "_#{target.id.to_s}" filename << "_#{noise}" filename << "_#{truncated_title}" filename << ".jpg" return filename end def age "%03d" % (target.age || 0) end def noise... def truncated_title... end
  61. class XYZFile ... def name filename = "#{target.publish_on.strftime("%d")}" filename <<

    "#{target.xyz_category_prefix}" filename << "#{target.kind.gsub("_", "")}" filename << "_#{age}" if target.personal? filename << "_#{target.id.to_s}" filename << "_#{noise}" filename << "_#{truncated_title}" filename << ".jpg" return filename end def age... def noise... def truncated_title... end
  62. class XYZFile ... def name filename = "#{target.publish_on.strftime("%d")}" filename <<

    "#{target.xyz_category_prefix}" filename << "#{target.kind.gsub("_", "")}" filename << "_#{age}" if target.personal? filename << "_#{target.id.to_s}" filename << "_#{noise}" filename << "_#{truncated_title}" filename << ".jpg" return filename end def age... def noise... def truncated_title... end
  63. class XYZFile ... def name filename = "#{publication_day}" filename <<

    "#{target.xyz_category_prefix}" filename << "#{target.kind.gsub("_", "")}" filename << "_#{age}" if target.personal? filename << "_#{target.id.to_s}" filename << "_#{noise}" filename << "_#{truncated_title}" filename << ".jpg" return filename end def publication_day target.publish_on.strftime("%d") end def age... def noise... def truncated_title... end
  64. class XYZFile ... def name filename = "#{publication_day}" filename <<

    "#{target.xyz_category_prefix}" filename << "#{target.kind.gsub("_", "")}" filename << "_#{age}" if target.personal? filename << "_#{target.id.to_s}" filename << "_#{noise}" filename << "_#{truncated_title}" filename << ".jpg" return filename end def publication_day... def age... def noise... def truncated_title... end
  65. class XYZFile ... def name filename = "#{publication_day}" filename <<

    "#{target.xyz_category_prefix}" filename << "#{target.kind.gsub("_", "")}" filename << "_#{age}" if target.personal? filename << "_#{target.id.to_s}" filename << "_#{noise}" filename << "_#{truncated_title}" filename << ".jpg" return filename end def publication_day... def age... def noise... def truncated_title... end
  66. class XYZFile ... def name filename = "#{publication_day}" filename <<

    "#{category}" filename << "#{target.kind.gsub("_", "")}" filename << "_#{age}" if target.personal? filename << "_#{target.id.to_s}" filename << "_#{noise}" filename << "_#{truncated_title}" filename << ".jpg" return filename end def category target.xyz_category_prefix end def publication_day... def age... def noise... def truncated_title...
  67. class XYZFile ... def name filename = "#{publication_day}" filename <<

    "#{category}" filename << "#{target.kind.gsub("_", "")}" filename << "_#{age}" if target.personal? filename << "_#{target.id.to_s}" filename << "_#{noise}" filename << "_#{truncated_title}" filename << ".jpg" return filename end def publication_day... def category... def age... def noise... def truncated_title... end
  68. class XYZFile ... def name filename = "#{publication_day}" filename <<

    "#{category}" filename << "#{kind}" filename << "_#{age}" if target.personal? filename << "_#{target.id.to_s}" filename << "_#{noise}" filename << "_#{truncated_title}" filename << ".jpg" return filename end def kind target.kind.gsub("_", "") end def publication_day... def category... def age... def noise...
  69. class XYZFile ... def name filename = "#{publication_day}" filename <<

    "#{category}" filename << "#{kind}" filename << "_#{age}" if target.personal? filename << "_#{target.id.to_s}" filename << "_#{noise}" filename << "_#{truncated_title}" filename << ".jpg" return filename end def publication_day... def category... def kind... def age... def noise... def truncated_title... end
  70. class XYZFile ... def name filename = "#{publication_day}" filename <<

    "#{category}" filename << "#{kind}" filename << "_#{age}" if target.personal? filename << "_#{target.id.to_s}" filename << "_#{noise}" filename << "_#{truncated_title}" filename << ".jpg" return filename end def publication_day... def category... def kind... def age... def noise... def truncated_title... end
  71. class XYZFile ... def name filename = publication_day filename <<

    category filename << kind filename << "_#{age}" if target.personal? filename << "_#{target.id}" filename << "_#{noise}" filename << "_#{truncated_title}" filename << ".jpg" filename end ... end
  72. class XYZFile ... def name filename = publication_day filename <<

    category filename << kind filename << "_#{age}" if target.personal? filename << "_#{target.id}" filename << "_#{noise}" filename << "_#{truncated_title}" filename << ".jpg" filename end ... end
  73. class XYZFile ... def name filename = publication_day filename <<

    category filename << kind filename << "_#{age}" if target.personal? filename << "_#{target.id}" filename << "_#{noise}" filename << "_#{truncated_title}" filename << ".jpg" filename end ... end
  74. class XYZFile ... def name filename = "" filename <<

    publication_day filename << category filename << kind filename << "_#{age}" if target.personal? filename << "_#{target.id}" filename << "_#{noise}" filename << "_#{truncated_title}" filename << ".jpg" filename end ... end
  75. class XYZFile ... def name filename = "" filename <<

    publication_day filename << category filename << kind filename << "_#{age}" if target.personal? filename << "_#{target.id}" filename << "_#{noise}" filename << "_#{truncated_title}" filename << ".jpg" filename end ... end
  76. class XYZFile ... def name filename = "" filename <<

    publication_day filename << category filename << kind filename << "_#{age}" if target.personal? filename << "_#{target.id}" filename << "_#{noise}" filename << "_#{truncated_title}" filename << ".jpg" filename end ... end
  77. class XYZFile ... def name filename = "" filename <<

    publication_day filename << category filename << kind filename << "_#{age}" if target.personal? filename << "_#{target.id}" filename << "_#{noise}" filename << "_#{truncated_title}" filename << ".jpg" filename end ... end
  78. class XYZFile ... def name filename = "" filename <<

    publication_day filename << category filename << kind filename << "_#{age}" if target.personal? filename << "_#{target.id}" filename << "_#{noise}" filename << "_#{truncated_title}" "#{filename}.jpg" end ... end
  79. class XYZFile ... def name filename = "" filename <<

    publication_day filename << category filename << kind filename << "_#{age}" if target.personal? filename << "_#{target.id}" filename << "_#{noise}" filename << "_#{truncated_title}" "#{filename}.jpg" end ... end
  80. class XYZFile ... def name filename = "" filename <<

    publication_day filename << category filename << kind filename << "_#{age}" if target.personal? filename << "_#{target.id}" filename << "_#{noise}" filename << "_#{truncated_title}" "#{filename}.jpg" end ... end
  81. class XYZFile ... def name filename = "" filename <<

    publication_day filename << category filename << kind filename << "_#{age}" if target.personal? filename << "_#{target.id}" filename << "_#{noise}" filename << "_#{truncated_title}" "#{filename}.jpg" end ... end
  82. class XYZFile ... def name ... filename << publication_day filename

    << category filename << kind ... end ... end
  83. class XYZFile ... def name ... filename << publication_day filename

    << category filename << kind ... end def prefix [publication_day, category, kind].join end ... end
  84. class XYZFile ... def name ... filename << prefix ...

    end def prefix [publication_day, category, kind].join end ... end
  85. class XYZFile ... def name filename = "" filename <<

    prefix filename << "_#{age}" if target.personal? filename << "_#{target.id}" filename << "_#{noise}" filename << "_#{truncated_title}" "#{filename}.jpg" end ... end
  86. class XYZFile ... def name filename = "" filename <<

    prefix filename << "_#{age}" if target.personal? filename << "_#{target.id}" filename << "_#{noise}" filename << "_#{truncated_title}" "#{filename}.jpg" end ... end
  87. class XYZFile ... def name filename = [] filename <<

    prefix filename << "_#{age}" if target.personal? filename << "_#{target.id}" filename << "_#{noise}" filename << "_#{truncated_title}" "#{filename}.jpg" end ... end
  88. class XYZFile ... def name filename = [] filename <<

    prefix filename << "_#{age}" if target.personal? filename << "_#{target.id}" filename << "_#{noise}" filename << "_#{truncated_title}" "#{filename}.jpg" end ... end
  89. class XYZFile ... def name filename = "" filename <<

    prefix filename << age if target.personal? filename << "_#{target.id}" filename << "_#{noise}" filename << "_#{truncated_title}" "#{filename}.jpg" end ... end
  90. class XYZFile ... def name filename = [] filename <<

    prefix filename << age if target.personal? filename << target.id filename << "_#{noise}" filename << "_#{truncated_title}" "#{filename}.jpg" end ... end
  91. class XYZFile ... def name filename = [] filename <<

    prefix filename << age if target.personal? filename << target.id filename << noise filename << "_#{truncated_title}" "#{filename}.jpg" end ... end
  92. class XYZFile ... def name filename = [] filename <<

    prefix filename << age if target.personal? filename << target.id filename << noise filename << truncated_title "#{filename}.jpg" end ... end
  93. class XYZFile ... def name filename = [] filename <<

    prefix filename << age if target.personal? filename << target.id filename << noise filename << truncated_title "#{filename.join("_")}.jpg" end ... end
  94. class XYZFile ... def name filename = [] filename <<

    prefix filename << age if target.personal? filename << target.id filename << noise filename << truncated_title "#{filename.join("_")}.jpg" end def prefix... def publication_day... def category... def kind... def age... def noise... def truncated_title... ... end
  95. module XYZService def self.xyz_filename(target) # File format: # [day of

    month zero-padded][three-letter prefix] \ # _[kind]_[age_if_kind_personal]_[target.id] \ # _[8 random chars]_[10 first chars of title].jpg filename = "#{target.publish_on.strftime("%d")}" filename << "#{target.xyz_category_prefix}" filename << "#{target.kind.gsub("_", "")}" filename << "_%03d" % (target.age || 0) if target.personal? filename << "_#{target.id.to_s}" filename << "_#{Digest::SHA1.hexdigest(rand(10000).to_s)[0,8]}" truncated_title = target.title.gsub(/[^\[a-z\]]/i, '').downcase length = truncated_title.length truncate_to = length > 9 ? 9 : length filename << "_#{truncated_title[0..(truncate_to)]}" filename << ".jpg" return filename end end
  96. class XYZFile ... def name filename = [] filename <<

    prefix filename << age if target.personal? filename << target.id filename << noise filename << truncated_title "#{filename.join("_")}.jpg" end def prefix... def publication_day... def category... def kind... def age... def noise... def truncated_title... ... end
  97. class XYZFile ... def prefix [publication_day, category, kind].join end def

    publication_day target.publish_on.strftime("%d") end def category target.xyz_category_prefix end def kind target.kind.gsub("_", "") end def age "%03d" % (target.age || 0) end def noise Digest::SHA1.hexdigest(rand(10000).to_s)[0,8] end def truncated_title target.title.downcase.gsub(/[^\[a-z\]]/, '')[0..9] end end
  98. module XYZService def self.xyz_filename(target) # File format: # [day of

    month zero-padded][three-letter prefix] \ # _[kind]_[age_if_kind_personal]_[target.id] \ # _[8 random chars]_[10 first chars of title].jpg filename = "#{target.publish_on.strftime("%d")}" filename << "#{target.xyz_category_prefix}" filename << "#{target.kind.gsub("_", "")}" filename << "_%03d" % (target.age || 0) if target.personal? filename << "_#{target.id.to_s}" filename << "_#{Digest::SHA1.hexdigest(rand(10000).to_s)[0,8]}" truncated_title = target.title.gsub(/[^\[a-z\]]/i, '').downcase length = truncated_title.length truncate_to = length > 9 ? 9 : length filename << "_#{truncated_title[0..(truncate_to)]}" filename << ".jpg" return filename end end
  99. class XYZFile ... def name filename = [] filename <<

    prefix filename << age if target.personal? filename << target.id filename << noise filename << truncated_title "#{filename.join("_")}.jpg" end def prefix... def publication_day... def category... def kind... def age... def noise... def truncated_title... ... end
  100. module XYZService def self.xyz_filename(target) # File format: # [day of

    month zero-padded][three-letter prefix] \ # _[kind]_[age_if_kind_personal]_[target.id] \ # _[8 random chars]_[10 first chars of title].jpg filename = "#{target.publish_on.strftime("%d")}" filename << "#{target.xyz_category_prefix}" filename << "#{target.kind.gsub("_", "")}" filename << "_%03d" % (target.age || 0) if target.personal? filename << "_#{target.id.to_s}" filename << "_#{Digest::SHA1.hexdigest(rand(10000).to_s)[0,8]}" truncated_title = target.title.gsub(/[^\[a-z\]]/i, '').downcase length = truncated_title.length truncate_to = length > 9 ? 9 : length filename << "_#{truncated_title[0..(truncate_to)]}" filename << ".jpg" return filename end end
  101. class XYZFile ... def name filename = [] filename <<

    prefix filename << age if target.personal? filename << target.id filename << noise filename << truncated_title "#{filename.join("_")}.jpg" end def prefix... def publication_day... def category... def kind... def age... def noise... def truncated_title... ... end
  102. module Codejunk def spurious_complexity s = rand(2) == 0 ?

    "abcde" : "abc" cutoff = s.length > 4 ? 4 : s.length s[0..cutoff] end end
  103. module Codejunk def add(n1, n2) n1 + n2 end end

    describe Codejunk do subject { stub.extend(Codejunk) } it "adds" do subject.add(1, 1).should eq(2) end it "adds twin primes" do subject.add(29, 31).should eq(60) end it "adds big numbers" do subject.add(9238498, 2039238).should eq(11277736) end it "adds negative numbers" do subject.add(100, -390).should eq(-290) end end
  104. describe Codejunk do subject { stub.extend(Codejunk) } it "gets rid

    of spaces" do subject.sanitize(" o m g ").should eq('omg') end it "gets rid of funky characters" do subject.sanitize("omg^%#=}{?_").should eq('omg') end it "gets rid of numbers" do subject.sanitize("omg123").should eq('omg') end it "downcases everything" do subject.sanitize("OMG").should eq('omg') end end
  105. module Codejunk def junk # Subtracts s = rand(2) ==

    0 ? "abcde" : "abc" cutoff = s.length > 4 ? 4 : s.length s = s[0..cutoff] s << "#{spurious_string_interpolation()}" s << "I am #{age.to_s} years old, and " s << "I <3 Magic!".gsub(/[^a-z]/i, '').downcase s << "#{(0..(one_method_call)).map(&:to_s).join('')}" return s end end
  106. Refactoring by Martin Fowler et al. http://www.goodreads.com/book/show/44936.Refactoring Refactoring, Ruby Edition

    by Jay Fields et al. http://www.goodreads.com/book/show/11560939-refactoring
  107. Working Memory Science! * Beilock & DeCaro, Department of Psychology,

    University of Chicago http://www.ncbi.nlm.nih.gov/pubmed/17983308