Therapeutic   RefactoringKatrina Owen@kytrinyxgithub.com/kytrinyx/therapeutic-refactoring
Illustration from Am Adult by Allie Broshhttp://hyperboleandahalf.blogspot.com/2010/03/am-adult.html
Photo: Johanna Owen 2011
Illustration from U B Hatin’ by Allie Broshhttp://hyperboleandahalf.blogspot.com/2010/03/u-b-hatin.html
I❤refactoring
Illustration from There’s a Lesson in Here Somewhere, I Think by Allie Broshhttp://hyperboleandahalf.blogspot.com/2010/01/...
Illustration from There’s a Lesson in Here Somewhere, I Think by Allie Broshhttp://hyperboleandahalf.blogspot.com/2010/01/...
refactoring   is notrehacktoring
A StoryInspired by true events
Illustration from The Scariest Story by Allie Broshhttp://hyperboleandahalf.blogspot.com/2011/02/scariest-story.html
module XYZService  def self.xyz_filename(target)    # File format:    # [day of month zero-padded][three-letter prefix]   ...
Illustration from The Scariest Story by Allie Broshhttp://hyperboleandahalf.blogspot.com/2011/02/scariest-story.html
Illustration from The Scariest Story by Allie Broshhttp://hyperboleandahalf.blogspot.com/2011/02/scariest-story.html
Illustration from The Scariest Story by Allie Broshhttp://hyperboleandahalf.blogspot.com/2011/02/scariest-story.html
module XYZService  def self.xyz_filename(target)    # File format:    # [day of month zero-padded][three-letter prefix]   ...
module XYZService  def self.xyz_filename(target)    # File format:    # [day of month zero-padded][three-letter prefix]   ...
module XYZService  def self.xyz_filename(target)    # File format:    # [day of month zero-padded][three-letter prefix]   ...
module XYZService  def self.xyz_filename(target)    # File format:    # [day of month zero-padded][three-letter prefix]   ...
module XYZService  def self.xyz_filename(target)    # File format:    # [day of month zero-padded][three-letter prefix]   ...
module XYZService  def self.xyz_filename(target)    # File format:    # [day of month zero-padded][three-letter prefix]   ...
module XYZService  def self.xyz_filename(target)    # File format:    # [day of month zero-padded][three-letter prefix]   ...
Tests(the first middle)
describe XYZService doend
describe XYZService do  it "works" do  endend
describe XYZService do  subject { XYZService.xyz_filename(stub) }  it "works" do  endend
describe XYZService do  subject { XYZService.xyz_filename(stub) }  it "works" do  endend
describe XYZService do  subject { XYZService.xyz_filename(stub) }  it "works" do  endend
describe XYZService do  subject { XYZService.xyz_filename(stub) }  it "works" do    subject.should eq("something")  endend
subject.should eq "something"
subject.should eq  "whatever"
subject.should eq   "freddy"
subject.should eq "something"
Failures: 1) XYZService works    Failure/Error:    subject.should eq("something")    Stub received unexpected message :pub...
Failures: 1) XYZService works    Failure/Error:    subject.should eq("something")    Stub received unexpected message :pub...
Stub received unexpected message:publish_on          with (no args)
Failures: 1) XYZService works    Failure/Error:    subject.should eq("something")    Stub received unexpected message :pub...
# ./xyz_service.rb:6
module XYZService  def self.xyz_filename(target)    # File format:    # [day of month zero-padded][three-letter prefix]   ...
target.publish_on.  strftime("%d")
target.publish_on.  strftime("%d")
target.publish_on.  strftime("%d")
describe XYZService do  subject { XYZService.xyz_filename(stub) }  it "works" do  endend
describe XYZService do  subject { XYZService.xyz_filename(target) }  it "works" do  endend
describe XYZService do  let(:target) do    stub(:target)  end  subject { XYZService.xyz_filename(target) }  it "works" do ...
describe XYZService do  let(:target) do    messages = {    }    stub(:target, messages)  end  subject { XYZService.xyz_fil...
describe XYZService do  let(:target) do    messages = {      :publish_on => ?    }    stub(:target, messages)  end  subjec...
describe XYZService do  let(:target) do    messages = {      :publish_on => Date.new(2012, 3, 14)    }    stub(:target, me...
Stub received unexpected message:xyz_category_prefix            with (no args)
...  :xyz_category_prefix => ?,...
...  :xyz_category_prefix => abc,...
Stub received unexpected message       :kind         with (no args)
...  :kind => ?...
...  :kind => unicorn...
Stub received unexpected message:personal?         with (no args)
...  :personal? => ?...
...  :personal? => false...
Stub received unexpected message          :id         with (no args)
...      :id => ?...
...      :id => 1337...
Stub received unexpected message     :title         with (no args)
...  :title => ?...
...  :title => magic & superglue...
Failures: 1) XYZService works    Failure/Error:    subject.should eq("something")      expected: "something"            go...
Failures: 1) XYZService works    Failure/Error:    subject.should eq("something")              #WIN      expected: "someth...
let(:target) do  messages = {    :publish_on => Date.new(2012, 3, 14),    :xyz_category_prefix => abc,    :kind => unicorn...
Failures: 1) XYZService works    Failure/Error:    subject.should eq("something")      expected: "something"            go...
Failures: 1) XYZService works    Failure/Error:    subject.should eq("something")      expected: "something"            go...
"14abcunicorn_1337_3f4894ca_magicsuper.jpg"
14abcunicorn_1337_3f4894ca_magicsuper.jpg
subject.should eq"14abcunicorn_1337_3f4894ca_magicsuper.jpg"
expected"14abcunicorn_1337_3f4894ca_magicsuper.jpg"                   got"14abcunicorn_1337_59a2b50d_magicsuper.jpg"
expected"14abcunicorn_1337_3f4894ca      _magicsuper.jpg"                        got"14abcunicorn_1337_59a2b50d      _magi...
subject.should eq"   14abcunicorn_1337_   3f4894ca   _magicsuper.jpg   "
subject.should match/   14abcunicorn_1337_   [0-9a-f]{8}   _magicsuper.jpg   /
.Finished in 0.00073 seconds1 example, 0 failures
.     #WINFinished in 0.00073 seconds1 example, 0 failures
def self.xyz_filename(target)  # File format:  # [day of month zero-padded][three-letter prefix]   # _[kind]_[age_if_kind_...
def self.xyz_filename(target)  # File format:  # [day of month zero-padded][three-letter prefix]   # _[kind]_[age_if_kind_...
Illustration from The Scariest Story by Allie Broshhttp://hyperboleandahalf.blogspot.com/2011/02/scariest-story.html
def self.xyz_filename(target)  # File format:  # [day of month zero-padded][three-letter prefix]   # _[kind]_[age_if_kind_...
target.publish_on.  strftime("%d")
:publish_on => Date.new(2012, 3, 14)
:publish_on => Date.new(2012, 2, 7)
def self.xyz_filename(target)  # File format:  # [day of month zero-padded][three-letter prefix]   # _[kind]_[age_if_kind_...
target.kind.gsub("_", "")
:kind => unicorn
:kind => magic_unicorn
def self.xyz_filename(target)  # File format:  # [day of month zero-padded][three-letter prefix]   # _[kind]_[age_if_kind_...
target.title.gsub(/[^[a-z]]/i, ).        downcase
:title => magic & superglue
:title => I <3 Sparkles!!1!
Illustration from Bycicle by Allie Broshhttp://hyperboleandahalf.blogspot.no/2010/07/bicycle.html
target.title.gsub(/[^[a-z]]/i, ).        downcase
target.title.gsub(/[^[a-z]]/i, ).        downcase
it "works" { ... }it "leaves square brackets???"
it "leaves square brackets???" do  target.stub(:title => i[sparkle]s)end
expected"07abcmagicunicorn_1337_f3b6d325_i[sparkle]     .jpg"                               to match/07abcmagicunicorn_133...
it "works"{ ... }it "leaves square brackets"{   ... }
def self.xyz_filename(target)  # File format:  # [day of month zero-padded][three-letter prefix]   # _[kind]_[age_if_kind_...
"_%03d" % (target.age || 0)if target.personal?
"_%03d" % (target.age || 0)if target.personal?
it "works" { ... }it "leaves square brackets"{   ... }it "personalizes"
it "personalizes" do  target.stub(:personal? => true)end
Stub received unexpected message        :age         with (no args)
it "personalizes" do  target.stub(:personal? => true)  target.stub(:age? => 42)end
"_%03d" % (target.age || 0)if target.personal?
"_%03d" % (target.age || 0)if target.personal?
it   "works"{ ... }it   "leaves square brackets"{   ... }it   "personalizes"{ ... }it   "handles nil age"
it "handles nil age" do  target.stub(:personal? => true)  target.stub(:age? => nil)end
def self.xyz_filename(target)  # File format:  # [day of month zero-padded][three-letter prefix]   # _[kind]_[age_if_kind_...
length > 9 ? 9 : length
:title => I <3 Sparkles!!1!
:title => I <3 SPARKLY Sparkles!!1!
it   "works" { ... }it   "leaves square brackets"    { ... }it   "personalizes" { ... }it   "handles nil age" { ... }it   ...
it "handles short titles" do  target.stub(:title => ‘O HAI’)end
.....Finished in 0.00386 seconds5 examples, 0 failures
.....        #WINFinished in 0.00386 seconds5 examples, 0 failures
Illustration from This is Why I’ll Never be an Adult by Allie Broshhttp://hyperboleandahalf.blogspot.com/2010/06/this-is-w...
Refactoring (the second middle)
Refactoring   replace method with method object
class XYZFileend
class XYZFile  def initialize(target)  endend
class XYZFile  attr_reader :target  def initialize(target)    @target = target  endend
class XYZFile  attr_reader :target  def initialize(target)    @target = target  end  def self.xyz_filename(target)    # Fi...
class XYZFile  ...  def self.xyz_filename(target)    ...  endend
class XYZFile  ...  def xyz_filename(target)    ...  endend
class XYZFile  ...  def name(target)    ...  endend
class XYZFile  ...  def name    ...  endend
module XYZService  def self.xyz_filename(target)    # File format:    # [day of month zero-padded][three-letter prefix]   ...
module XYZService  def self.xyz_filename(target)  endend
require_relative ./xyz_filemodule XYZService  def self.xyz_filename(target)  endend
require_relative ./xyz_filemodule XYZService  def self.xyz_filename(target)    XYZFile.new  endend
require_relative ./xyz_filemodule XYZService  def self.xyz_filename(target)    XYZFile.new(target)  endend
require_relative ./xyz_filemodule XYZService  def self.xyz_filename(target)    XYZFile.new(target)               (target)....
.....Finished in 0.00398 seconds5 examples, 0 failures
.....        #WINFinished in 0.00398 seconds5 examples, 0 failures
class XYZFile  ...  def name    # File format:    # [day of month zero-padded][three-letter prefix]     # _[kind]_[age_if_...
class XYZFile  ...  def name    # File format:    # [day of month zero-padded][three-letter prefix]     # _[kind]_[age_if_...
class XYZFile  ...  def name    filename = "#{target.publish_on.strftime("%d")}"    filename << "#{target.xyz_category_pre...
class XYZFile  ...  def name    filename = "#{target.publish_on.strftime("%d")}"    filename << "#{target.xyz_category_pre...
class XYZFile  ...  def name    ...    truncated_title = target.title.gsub(/[^[a-z]]/i, ).downcase    length = truncated_t...
class XYZFile  ...  def name    ...    truncated_title = target.title.gsub(/[^[a-z]]/i, ).downcase    length = truncated_t...
class XYZFile  ...  def name    ...    truncated_title = target.title.gsub(/[^[a-z]]/i, ).downcase    length = truncated_t...
class XYZFile  ...  def name    ...    truncated_title = target.title.gsub(/[^[a-z]]/i, ).downcase    length = truncated_t...
class XYZFile  ...  def name...  def truncated_title    truncated_title = target.title.gsub(/[^[a-z]]/i, ).downcase    len...
class XYZFile  ...  def name...  def truncated_title    truncated_title = target.title.gsub(/[^[a-z]]/i, ).downcase    len...
class XYZFile  ...  def name...  def truncated_title    truncated_title = target.title.gsub(/[^[a-z]]/i, ).downcase    len...
class XYZFile  ...  def name...  def truncated_title    truncated_title = target.title.gsub(/[^[a-z]]/i, ).downcase    len...
class XYZFile  ...  def name...  def truncated_title    truncated_title = target.title.gsub(/[^[a-z]]/i, ).downcase    len...
class XYZFile  ...  def name    ...    truncated_title = target.title.gsub(/[^[a-z]]/i, ).downcase    length = truncated_t...
class XYZFile  ...  def name    ...    truncated_title = target.title.gsub(/[^[a-z]]/i, ).downcase    length = truncated_t...
class XYZFile  ...  def name    ...    truncated_title = target.title.gsub(/[^[a-z]]/i, ).downcase    length = truncated_t...
class XYZFile  ...  def name    ...    filename << "_#{truncated_title}"    ...  end  def truncated_title...end
class XYZFile  ...  def name    filename = "#{target.publish_on.strftime("%d")}"    filename << "#{target.xyz_category_pre...
class XYZFile  ...  def truncated_title    truncated_title = target.title.gsub(/[^[a-z]]/i, ).downcase    length = truncat...
class XYZFile  ...  def truncated_title    truncated_title = target.title.gsub(/[^[a-z]]/i, ).downcase    length = truncat...
target.title.gsub(/[^[a-z]]/i, ).        downcase
target.title.gsub(/[^[a-z]]/i, ).        downcase
target.title.gsub(/[^[a-z]]/i, ).        downcase
target.title.downcasegsub(/[^[a-z]]/, )
truncated_title[0..(truncate_to)]
truncated_title[0..(truncate_to)]
truncated_title[0..truncate_to]
truncate_to = length > 9 ? 9 : length   truncated_title[0..truncate_to]
truncate_to = length > 9 ? 9 : lengthtruncated_title[0..9]
length = truncated_title.lengthtruncate_to = length > 9 ? 9 : lengthtruncated_title[0..9]
length = truncated_title.lengthtruncate_to = length > 9 ? 9 : lengthtruncated_title[0..9]
class XYZFile  ...  def truncated_title    truncated_title = target.title.downcase.gsub(/[^[a-z]]/, )    truncated_title[0...
class XYZFile  ...  def truncated_title    truncated_title = target.title.downcase.gsub(/[^[a-z]]/, )    truncated_title[0...
class XYZFile  ...  def truncated_title    target.title.downcase.    gsub(/[^[a-z]]/, )[0..9]  endend
class XYZFile  ...  def name    filename = "#{target.publish_on.strftime("%d")}"    filename << "#{target.xyz_category_pre...
class XYZFile  ...  def name    filename = "#{target.publish_on.strftime("%d")}"    filename << "#{target.xyz_category_pre...
class XYZFile  ...  def name    ...    filename << "_#{Digest::SHA1.hexdigest(rand(10000).to_s)[0,8]}"    ...  end  def tr...
class XYZFile  ...  def name    ...    filename << "_#{Digest::SHA1.hexdigest(rand(10000).to_s)[0,8]}"    ...  end  def no...
class XYZFile  ...  def name    ...    filename << "_#{Digest::SHA1.hexdigest(rand(10000).to_s)[0,8]}"    ...  end  def no...
class XYZFile  ...  def name    ...    filename << "_#{noise}"    ...  end  def noise    Digest::SHA1.hexdigest(rand(10000...
class XYZFile  ...  def name    filename = "#{target.publish_on.strftime("%d")}"    filename << "#{target.xyz_category_pre...
class XYZFile  ...  def name    filename = "#{target.publish_on.strftime("%d")}"    filename << "#{target.xyz_category_pre...
class XYZFile  ...  def name    filename = "#{target.publish_on.strftime("%d")}"    filename << "#{target.xyz_category_pre...
class XYZFile  ...  def name    filename = "#{target.publish_on.strftime("%d")}"    filename << "#{target.xyz_category_pre...
class XYZFile  ...  def name    filename = "#{target.publish_on.strftime("%d")}"    filename << "#{target.xyz_category_pre...
class XYZFile  ...  def name    filename = "#{publication_day}"    filename << "#{target.xyz_category_prefix}"    filename...
class XYZFile  ...  def name    filename = "#{publication_day}"    filename << "#{target.xyz_category_prefix}"    filename...
class XYZFile  ...  def name    filename = "#{publication_day}"    filename << "#{target.xyz_category_prefix}"    filename...
class XYZFile  ...  def name    filename = "#{publication_day}"    filename << "#{category}"    filename << "#{target.kind...
class XYZFile  ...  def name    filename = "#{publication_day}"    filename << "#{category}"    filename << "#{target.kind...
class XYZFile  ...  def name    filename = "#{publication_day}"    filename << "#{category}"    filename << "#{kind}"    f...
class XYZFile  ...  def name    filename = "#{publication_day}"    filename << "#{category}"    filename << "#{kind}"    f...
class XYZFile  ...  def name    filename = "#{publication_day}"    filename << "#{category}"    filename << "#{kind}"    f...
filename = "#{publication_day}"
filename = "#{publication_day}"
filename = publication_day
filename << "#{category}"
filename << category
filename << "#{kind}"
filename << kind
filename << "_#{target.id.to_s}"
filename << "_#{target.id}"
return filename
filename
class XYZFile  ...  def name    filename = publication_day    filename << category    filename << kind    filename << "_#{...
http://klesgaver.spreadshirt.no/finn-fem-feil-A8534272
class XYZFile  ...  def name    filename = publication_day    filename << category    filename << kind    filename << "_#{...
class XYZFile  ...  def name    filename = publication_day    filename << category    filename << kind    filename << "_#{...
class XYZFile  ...  def name    filename = publication_day    filename << category    filename << kind    filename << "_#{...
class XYZFile  ...  def name    filename = ""    filename << publication_day    filename << category    filename << kind  ...
class XYZFile  ...  def name    filename = ""    filename << publication_day    filename << category    filename << kind  ...
class XYZFile  ...  def name    filename = ""    filename << publication_day    filename << category    filename << kind  ...
class XYZFile  ...  def name    filename = ""    filename << publication_day    filename << category    filename << kind  ...
class XYZFile  ...  def name    filename = ""    filename << publication_day    filename << category    filename << kind  ...
class XYZFile  ...  def name    filename = ""    filename << publication_day    filename << category    filename << kind  ...
class XYZFile  ...  def name    filename = ""    filename << publication_day    filename << category    filename << kind  ...
class XYZFile  ...  def name    filename = ""    filename << publication_day    filename << category    filename << kind  ...
class XYZFile  ...  def name    ...    filename << publication_day    filename << category    filename << kind    ...  end...
class XYZFile  ...  def name    ...    filename << publication_day    filename << category    filename << kind    ...  end...
class XYZFile  ...  def name    ...    filename << prefix    ...  end  def prefix    [publication_day, category, kind].joi...
class XYZFile  ...  def name    filename = ""    filename << prefix    filename << "_#{age}" if target.personal?    filena...
class XYZFile  ...  def name    filename = ""    filename << prefix    filename << "_#{age}" if target.personal?    filena...
class XYZFile  ...  def name    filename = []    filename << prefix    filename << "_#{age}" if target.personal?    filena...
class XYZFile  ...  def name    filename = []    filename << prefix    filename << "_#{age}" if target.personal?    filena...
class XYZFile  ...  def name    filename = ""    filename << prefix    filename << age if target.personal?    filename << ...
class XYZFile  ...  def name    filename = []    filename << prefix    filename << age if target.personal?    filename << ...
class XYZFile  ...  def name    filename = []    filename << prefix    filename << age if target.personal?    filename << ...
class XYZFile  ...  def name    filename = []    filename << prefix    filename << age if target.personal?    filename << ...
class XYZFile  ...  def name    filename = []    filename << prefix    filename << age if target.personal?    filename << ...
class XYZFile  ...  def name    filename = []    filename << prefix    filename << age if target.personal?    filename << ...
Illustration from About by Allie Broshhttp://hyperboleandahalf.blogspot.com/p/about.html
module XYZService  def self.xyz_filename(target)    # File format:    # [day of month zero-padded][three-letter prefix]   ...
class XYZFile  ...  def name    filename = []    filename << prefix    filename << age if target.personal?    filename << ...
module XYZService  def self.xyz_filename(target)    # File format:    # [day of month zero-padded][three-letter prefix]   ...
class XYZFile  ...  def name    filename = []    filename << prefix    filename << age if target.personal?    filename << ...
module XYZService  def self.xyz_filename(target)    # File format:    # [day of month zero-padded][three-letter prefix]   ...
class XYZFile  ...  def name    filename = []    filename << prefix    filename << age if target.personal?    filename << ...
Codejunk (the ending)
Illustration from Chartjunk by Viveka Weiley          http://chartjunk.karmanaut.com/?p=8
Illustration from Chartjunk by Viveka Weileyhttp://chartjunk.karmanaut.com/test-pattern-retuned/
Top 10
#10Lousy Comments
module Codejunk  # States the obvious  def state_the_obvious    "whatever"  endend
module Codejunk  # Takes modulus 100  def say_it_again(number)    number % 100  endend
module Codejunk  # Subtracts  def incorrect_comment    1 + 1  endend
module Codejunk  # add  def fuzzy_comment    1 + 1  endend
module Codejunk  # ad too numbers  def bad_comment    1 + 2  endend
#9Unnecessary Explicit Return
module Codejunk  def spurious_return    return "i before e except after c"  endend
#8Trailing Whitespace
module Codejunk   ..........   def a_method .     Use your words ................   end ......................end
#7Commented-Out Code
module Codejunk  def tweaked_logic(wday)    # ((7 - wday) % 7) * 1.day    (7 - wday) % 7  endend
module Codejunk  def tweaked_logic(wday)    if false      ((7 - wday) % 7) * 1.day    end    (7 - wday) % 7  endend
#6Needless Parentheses
module Codejunk  def spurious_parentheses()    junk()  endend
module Codejunk  def more_spurious_parentheses    (0..(one_method_call)).to_a  end  def one_method_call    (10 - rand(10))...
#5Explicit Default Parameters
module Codejunk  def spurious_arguments    [1, 2, 3].join()  endend
#4Unnecessary Requires
require active_support/all# Nothing uses active supportmodule Codejunk  ...end
#3Stringifying Strings
module Codejunk  def spurious_string_interpolation    "#{thing}"  end  def thing    "a string"  endend
#2Too Much Hard Work
module Codejunk  def spurious_stringification    "I am #{age.to_s} years old"  end  def age    rand(25) + 15  endend
module Codejunk  def spurious_transformations    [1, 2, 3].map(&:to_s).join  endend
module Codejunk  def spurious_complexity    s = rand(2) == 0 ? "abcde" : "abc"    cutoff = s.length > 4 ? 4 : s.length    ...
module Codejunk  def spurious_hard_work    s = "I <3 Magic!"    s.gsub(/[^a-z]/i, ).downcase  endend
#1Duplicated Tests
describe Codejunk do  subject { stub.extend(Codejunk) }  it "gets rid of spaces" do    subject.sanitize(" o m g ").should ...
#0Combine All The Codejunk
module Codejunk  def junk    # Subtracts    s = rand(2) == 0 ? "abcde" : "abc"    cutoff = s.length > 4 ? 4 : s.length    ...
github.com/kytrinyx/therapeutic-refactoring
github.com/kytrinyx/therapeutic-refactoring
github.com/kytrinyx /therapeutic-refactoring@kytrinyxkytrinyx@gmail.com
Therapy (the moral)
I❤refactoring
Science!Working Memory
Science!Problem-Solving Strategies
Science! Worry & Panic
Refactoring makes you smarter!
Exobrain
Exobraincounteracts panic
Refactoring makes you happier!
Fast Tests
Fast Tests  are awesome
Happiness
Happinessleads to good design
Illustration from God of Cake by Allie Broshhttp://hyperboleandahalf.blogspot.no/2010/10/god-of-cake.html
KTHXBYEKatrina Owen@kytrinyxgithub.com/kytrinyx/therapeutic-refactoring
Therapeutic refactoring
Upcoming SlideShare
Loading in …5
×

Therapeutic refactoring

2,501 views

Published on

Slides for the Therapeutic Refactoring talk presented at NordicRuby on June 15th, 2012, and Scottish Ruby Conference on June 30th, 2012. The code samples are on github:

http://github.com/kytrinyx/therapeutic-refactoring

Published in: Technology, Art & Photos
0 Comments
8 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,501
On SlideShare
0
From Embeds
0
Number of Embeds
1,038
Actions
Shares
0
Downloads
26
Comments
0
Likes
8
Embeds 0
No embeds

No notes for slide

Therapeutic refactoring

  1. 1. Therapeutic RefactoringKatrina Owen@kytrinyxgithub.com/kytrinyx/therapeutic-refactoring
  2. 2. Illustration from Am Adult by Allie Broshhttp://hyperboleandahalf.blogspot.com/2010/03/am-adult.html
  3. 3. Photo: Johanna Owen 2011
  4. 4. Illustration from U B Hatin’ by Allie Broshhttp://hyperboleandahalf.blogspot.com/2010/03/u-b-hatin.html
  5. 5. I❤refactoring
  6. 6. Illustration from There’s a Lesson in Here Somewhere, I Think by Allie Broshhttp://hyperboleandahalf.blogspot.com/2010/01/theres-lesson-in-here-somewhere-i- think.html
  7. 7. Illustration from There’s a Lesson in Here Somewhere, I Think by Allie Broshhttp://hyperboleandahalf.blogspot.com/2010/01/theres-lesson-in-here-somewhere-i- think.html
  8. 8. refactoring is notrehacktoring
  9. 9. A StoryInspired by true events
  10. 10. Illustration from The Scariest Story by Allie Broshhttp://hyperboleandahalf.blogspot.com/2011/02/scariest-story.html
  11. 11. 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 endend
  12. 12. Illustration from The Scariest Story by Allie Broshhttp://hyperboleandahalf.blogspot.com/2011/02/scariest-story.html
  13. 13. Illustration from The Scariest Story by Allie Broshhttp://hyperboleandahalf.blogspot.com/2011/02/scariest-story.html
  14. 14. Illustration from The Scariest Story by Allie Broshhttp://hyperboleandahalf.blogspot.com/2011/02/scariest-story.html
  15. 15. 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 endend
  16. 16. 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 endend
  17. 17. 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 endend
  18. 18. 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 endend
  19. 19. 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 endend
  20. 20. 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 endend
  21. 21. 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 endend
  22. 22. Tests(the first middle)
  23. 23. describe XYZService doend
  24. 24. describe XYZService do it "works" do endend
  25. 25. describe XYZService do subject { XYZService.xyz_filename(stub) } it "works" do endend
  26. 26. describe XYZService do subject { XYZService.xyz_filename(stub) } it "works" do endend
  27. 27. describe XYZService do subject { XYZService.xyz_filename(stub) } it "works" do endend
  28. 28. describe XYZService do subject { XYZService.xyz_filename(stub) } it "works" do subject.should eq("something") endend
  29. 29. subject.should eq "something"
  30. 30. subject.should eq "whatever"
  31. 31. subject.should eq "freddy"
  32. 32. subject.should eq "something"
  33. 33. 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
  34. 34. 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
  35. 35. Stub received unexpected message:publish_on with (no args)
  36. 36. 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
  37. 37. # ./xyz_service.rb:6
  38. 38. 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 endend
  39. 39. target.publish_on. strftime("%d")
  40. 40. target.publish_on. strftime("%d")
  41. 41. target.publish_on. strftime("%d")
  42. 42. describe XYZService do subject { XYZService.xyz_filename(stub) } it "works" do endend
  43. 43. describe XYZService do subject { XYZService.xyz_filename(target) } it "works" do endend
  44. 44. describe XYZService do let(:target) do stub(:target) end subject { XYZService.xyz_filename(target) } it "works" do subject.should eq("something") endend
  45. 45. describe XYZService do let(:target) do messages = { } stub(:target, messages) end subject { XYZService.xyz_filename(target) } it "works" do subject.should eq("something") endend
  46. 46. 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") endend
  47. 47. 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") endend
  48. 48. Stub received unexpected message:xyz_category_prefix with (no args)
  49. 49. ... :xyz_category_prefix => ?,...
  50. 50. ... :xyz_category_prefix => abc,...
  51. 51. Stub received unexpected message :kind with (no args)
  52. 52. ... :kind => ?...
  53. 53. ... :kind => unicorn...
  54. 54. Stub received unexpected message:personal? with (no args)
  55. 55. ... :personal? => ?...
  56. 56. ... :personal? => false...
  57. 57. Stub received unexpected message :id with (no args)
  58. 58. ... :id => ?...
  59. 59. ... :id => 1337...
  60. 60. Stub received unexpected message :title with (no args)
  61. 61. ... :title => ?...
  62. 62. ... :title => magic & superglue...
  63. 63. 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)>
  64. 64. Failures: 1) XYZService works Failure/Error: subject.should eq("something") #WIN expected: "something" got: "14abcunicorn_42_3f4894ca_magicsuper.jpg" (compared using ==) # ./xyz_file_spec.rb:18:in `block (2 levels) in <top (required)>
  65. 65. 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
  66. 66. 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)>
  67. 67. 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)>
  68. 68. "14abcunicorn_1337_3f4894ca_magicsuper.jpg"
  69. 69. 14abcunicorn_1337_3f4894ca_magicsuper.jpg
  70. 70. subject.should eq"14abcunicorn_1337_3f4894ca_magicsuper.jpg"
  71. 71. expected"14abcunicorn_1337_3f4894ca_magicsuper.jpg" got"14abcunicorn_1337_59a2b50d_magicsuper.jpg"
  72. 72. expected"14abcunicorn_1337_3f4894ca _magicsuper.jpg" got"14abcunicorn_1337_59a2b50d _magicsuper.jpg"
  73. 73. subject.should eq" 14abcunicorn_1337_ 3f4894ca _magicsuper.jpg "
  74. 74. subject.should match/ 14abcunicorn_1337_ [0-9a-f]{8} _magicsuper.jpg /
  75. 75. .Finished in 0.00073 seconds1 example, 0 failures
  76. 76. . #WINFinished in 0.00073 seconds1 example, 0 failures
  77. 77. 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 filenameend
  78. 78. 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 filenameend
  79. 79. Illustration from The Scariest Story by Allie Broshhttp://hyperboleandahalf.blogspot.com/2011/02/scariest-story.html
  80. 80. 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 filenameend
  81. 81. target.publish_on. strftime("%d")
  82. 82. :publish_on => Date.new(2012, 3, 14)
  83. 83. :publish_on => Date.new(2012, 2, 7)
  84. 84. 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 filenameend
  85. 85. target.kind.gsub("_", "")
  86. 86. :kind => unicorn
  87. 87. :kind => magic_unicorn
  88. 88. 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 filenameend
  89. 89. target.title.gsub(/[^[a-z]]/i, ). downcase
  90. 90. :title => magic & superglue
  91. 91. :title => I <3 Sparkles!!1!
  92. 92. Illustration from Bycicle by Allie Broshhttp://hyperboleandahalf.blogspot.no/2010/07/bicycle.html
  93. 93. target.title.gsub(/[^[a-z]]/i, ). downcase
  94. 94. target.title.gsub(/[^[a-z]]/i, ). downcase
  95. 95. it "works" { ... }it "leaves square brackets???"
  96. 96. it "leaves square brackets???" do target.stub(:title => i[sparkle]s)end
  97. 97. expected"07abcmagicunicorn_1337_f3b6d325_i[sparkle] .jpg" to match/07abcmagicunicorn_1337_[0-9a-z]{8}_isparkles.jpg/
  98. 98. it "works"{ ... }it "leaves square brackets"{ ... }
  99. 99. 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 filenameend
  100. 100. "_%03d" % (target.age || 0)if target.personal?
  101. 101. "_%03d" % (target.age || 0)if target.personal?
  102. 102. it "works" { ... }it "leaves square brackets"{ ... }it "personalizes"
  103. 103. it "personalizes" do target.stub(:personal? => true)end
  104. 104. Stub received unexpected message :age with (no args)
  105. 105. it "personalizes" do target.stub(:personal? => true) target.stub(:age? => 42)end
  106. 106. "_%03d" % (target.age || 0)if target.personal?
  107. 107. "_%03d" % (target.age || 0)if target.personal?
  108. 108. it "works"{ ... }it "leaves square brackets"{ ... }it "personalizes"{ ... }it "handles nil age"
  109. 109. it "handles nil age" do target.stub(:personal? => true) target.stub(:age? => nil)end
  110. 110. 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 filenameend
  111. 111. length > 9 ? 9 : length
  112. 112. :title => I <3 Sparkles!!1!
  113. 113. :title => I <3 SPARKLY Sparkles!!1!
  114. 114. it "works" { ... }it "leaves square brackets" { ... }it "personalizes" { ... }it "handles nil age" { ... }it "handles short titles"
  115. 115. it "handles short titles" do target.stub(:title => ‘O HAI’)end
  116. 116. .....Finished in 0.00386 seconds5 examples, 0 failures
  117. 117. ..... #WINFinished in 0.00386 seconds5 examples, 0 failures
  118. 118. Illustration from This is Why I’ll Never be an Adult by Allie Broshhttp://hyperboleandahalf.blogspot.com/2010/06/this-is-why-ill-never-be-adult.html
  119. 119. Refactoring (the second middle)
  120. 120. Refactoring replace method with method object
  121. 121. class XYZFileend
  122. 122. class XYZFile def initialize(target) endend
  123. 123. class XYZFile attr_reader :target def initialize(target) @target = target endend
  124. 124. 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
  125. 125. class XYZFile ... def self.xyz_filename(target) ... endend
  126. 126. class XYZFile ... def xyz_filename(target) ... endend
  127. 127. class XYZFile ... def name(target) ... endend
  128. 128. class XYZFile ... def name ... endend
  129. 129. 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 endend
  130. 130. module XYZService def self.xyz_filename(target) endend
  131. 131. require_relative ./xyz_filemodule XYZService def self.xyz_filename(target) endend
  132. 132. require_relative ./xyz_filemodule XYZService def self.xyz_filename(target) XYZFile.new endend
  133. 133. require_relative ./xyz_filemodule XYZService def self.xyz_filename(target) XYZFile.new(target) endend
  134. 134. require_relative ./xyz_filemodule XYZService def self.xyz_filename(target) XYZFile.new(target) (target).name endend
  135. 135. .....Finished in 0.00398 seconds5 examples, 0 failures
  136. 136. ..... #WINFinished in 0.00398 seconds5 examples, 0 failures
  137. 137. 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 endend
  138. 138. 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 endend
  139. 139. 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 endend
  140. 140. 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 endend
  141. 141. 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)]}" ... endend
  142. 142. 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)]}" ... endend
  143. 143. 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 endend
  144. 144. 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)]}" endend
  145. 145. 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)]}" endend
  146. 146. 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)]}" endend
  147. 147. 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)]}" endend
  148. 148. 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)] endend
  149. 149. 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)] endend
  150. 150. 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
  151. 151. 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
  152. 152. 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
  153. 153. class XYZFile ... def name ... filename << "_#{truncated_title}" ... end def truncated_title...end
  154. 154. 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
  155. 155. 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)] endend
  156. 156. 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)] endend
  157. 157. target.title.gsub(/[^[a-z]]/i, ). downcase
  158. 158. target.title.gsub(/[^[a-z]]/i, ). downcase
  159. 159. target.title.gsub(/[^[a-z]]/i, ). downcase
  160. 160. target.title.downcasegsub(/[^[a-z]]/, )
  161. 161. truncated_title[0..(truncate_to)]
  162. 162. truncated_title[0..(truncate_to)]
  163. 163. truncated_title[0..truncate_to]
  164. 164. truncate_to = length > 9 ? 9 : length truncated_title[0..truncate_to]
  165. 165. truncate_to = length > 9 ? 9 : lengthtruncated_title[0..9]
  166. 166. length = truncated_title.lengthtruncate_to = length > 9 ? 9 : lengthtruncated_title[0..9]
  167. 167. length = truncated_title.lengthtruncate_to = length > 9 ? 9 : lengthtruncated_title[0..9]
  168. 168. class XYZFile ... def truncated_title truncated_title = target.title.downcase.gsub(/[^[a-z]]/, ) truncated_title[0..truncate_to] endend
  169. 169. class XYZFile ... def truncated_title truncated_title = target.title.downcase.gsub(/[^[a-z]]/, ) truncated_title[0..truncate_to] endend
  170. 170. class XYZFile ... def truncated_title target.title.downcase. gsub(/[^[a-z]]/, )[0..9] endend
  171. 171. 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
  172. 172. 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
  173. 173. class XYZFile ... def name ... filename << "_#{Digest::SHA1.hexdigest(rand(10000).to_s)[0,8]}" ... end def truncated_title...end
  174. 174. class XYZFile ... def name ... filename << "_#{Digest::SHA1.hexdigest(rand(10000).to_s)[0,8]}" ... end def noise end def truncated_title...end
  175. 175. 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
  176. 176. class XYZFile ... def name ... filename << "_#{noise}" ... end def noise Digest::SHA1.hexdigest(rand(10000).to_s)[0,8] end def truncated_title...end
  177. 177. 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
  178. 178. 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
  179. 179. 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
  180. 180. 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
  181. 181. 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
  182. 182. 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
  183. 183. 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
  184. 184. 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
  185. 185. 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...
  186. 186. 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
  187. 187. 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...
  188. 188. 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
  189. 189. 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
  190. 190. filename = "#{publication_day}"
  191. 191. filename = "#{publication_day}"
  192. 192. filename = publication_day
  193. 193. filename << "#{category}"
  194. 194. filename << category
  195. 195. filename << "#{kind}"
  196. 196. filename << kind
  197. 197. filename << "_#{target.id.to_s}"
  198. 198. filename << "_#{target.id}"
  199. 199. return filename
  200. 200. filename
  201. 201. 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
  202. 202. http://klesgaver.spreadshirt.no/finn-fem-feil-A8534272
  203. 203. 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
  204. 204. 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
  205. 205. 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
  206. 206. 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
  207. 207. 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
  208. 208. 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
  209. 209. 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
  210. 210. 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
  211. 211. 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
  212. 212. 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
  213. 213. 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
  214. 214. class XYZFile ... def name ... filename << publication_day filename << category filename << kind ... end ...end
  215. 215. class XYZFile ... def name ... filename << publication_day filename << category filename << kind ... end def prefix [publication_day, category, kind].join end ...end
  216. 216. class XYZFile ... def name ... filename << prefix ... end def prefix [publication_day, category, kind].join end ...end
  217. 217. class XYZFile ... def name filename = "" filename << prefix filename << "_#{age}" if target.personal? filename << "_#{target.id}" filename << "_#{noise}" filename << "_#{truncated_title}" "#{filename}.jpg" end ...end
  218. 218. class XYZFile ... def name filename = "" filename << prefix filename << "_#{age}" if target.personal? filename << "_#{target.id}" filename << "_#{noise}" filename << "_#{truncated_title}" "#{filename}.jpg" end ...end
  219. 219. class XYZFile ... def name filename = [] filename << prefix filename << "_#{age}" if target.personal? filename << "_#{target.id}" filename << "_#{noise}" filename << "_#{truncated_title}" "#{filename}.jpg" end ...end
  220. 220. class XYZFile ... def name filename = [] filename << prefix filename << "_#{age}" if target.personal? filename << "_#{target.id}" filename << "_#{noise}" filename << "_#{truncated_title}" "#{filename}.jpg" end ...end
  221. 221. class XYZFile ... def name filename = "" filename << prefix filename << age if target.personal? filename << "_#{target.id}" filename << "_#{noise}" filename << "_#{truncated_title}" "#{filename}.jpg" end ...end
  222. 222. class XYZFile ... def name filename = [] filename << prefix filename << age if target.personal? filename << target.id filename << "_#{noise}" filename << "_#{truncated_title}" "#{filename}.jpg" end ...end
  223. 223. class XYZFile ... def name filename = [] filename << prefix filename << age if target.personal? filename << target.id filename << noise filename << "_#{truncated_title}" "#{filename}.jpg" end ...end
  224. 224. class XYZFile ... def name filename = [] filename << prefix filename << age if target.personal? filename << target.id filename << noise filename << truncated_title "#{filename}.jpg" end ...end
  225. 225. 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
  226. 226. 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
  227. 227. Illustration from About by Allie Broshhttp://hyperboleandahalf.blogspot.com/p/about.html
  228. 228. 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 endend
  229. 229. 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
  230. 230. 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 endend
  231. 231. 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
  232. 232. 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 endend
  233. 233. 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
  234. 234. Codejunk (the ending)
  235. 235. Illustration from Chartjunk by Viveka Weiley http://chartjunk.karmanaut.com/?p=8
  236. 236. Illustration from Chartjunk by Viveka Weileyhttp://chartjunk.karmanaut.com/test-pattern-retuned/
  237. 237. Top 10
  238. 238. #10Lousy Comments
  239. 239. module Codejunk # States the obvious def state_the_obvious "whatever" endend
  240. 240. module Codejunk # Takes modulus 100 def say_it_again(number) number % 100 endend
  241. 241. module Codejunk # Subtracts def incorrect_comment 1 + 1 endend
  242. 242. module Codejunk # add def fuzzy_comment 1 + 1 endend
  243. 243. module Codejunk # ad too numbers def bad_comment 1 + 2 endend
  244. 244. #9Unnecessary Explicit Return
  245. 245. module Codejunk def spurious_return return "i before e except after c" endend
  246. 246. #8Trailing Whitespace
  247. 247. module Codejunk .......... def a_method . Use your words ................ end ......................end
  248. 248. #7Commented-Out Code
  249. 249. module Codejunk def tweaked_logic(wday) # ((7 - wday) % 7) * 1.day (7 - wday) % 7 endend
  250. 250. module Codejunk def tweaked_logic(wday) if false ((7 - wday) % 7) * 1.day end (7 - wday) % 7 endend
  251. 251. #6Needless Parentheses
  252. 252. module Codejunk def spurious_parentheses() junk() endend
  253. 253. module Codejunk def more_spurious_parentheses (0..(one_method_call)).to_a end def one_method_call (10 - rand(10)) endend
  254. 254. #5Explicit Default Parameters
  255. 255. module Codejunk def spurious_arguments [1, 2, 3].join() endend
  256. 256. #4Unnecessary Requires
  257. 257. require active_support/all# Nothing uses active supportmodule Codejunk ...end
  258. 258. #3Stringifying Strings
  259. 259. module Codejunk def spurious_string_interpolation "#{thing}" end def thing "a string" endend
  260. 260. #2Too Much Hard Work
  261. 261. module Codejunk def spurious_stringification "I am #{age.to_s} years old" end def age rand(25) + 15 endend
  262. 262. module Codejunk def spurious_transformations [1, 2, 3].map(&:to_s).join endend
  263. 263. module Codejunk def spurious_complexity s = rand(2) == 0 ? "abcde" : "abc" cutoff = s.length > 4 ? 4 : s.length s[0..cutoff] endend
  264. 264. module Codejunk def spurious_hard_work s = "I <3 Magic!" s.gsub(/[^a-z]/i, ).downcase endend
  265. 265. #1Duplicated Tests
  266. 266. 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) endend
  267. 267. #0Combine All The Codejunk
  268. 268. 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 endend
  269. 269. github.com/kytrinyx/therapeutic-refactoring
  270. 270. github.com/kytrinyx/therapeutic-refactoring
  271. 271. github.com/kytrinyx /therapeutic-refactoring@kytrinyxkytrinyx@gmail.com
  272. 272. Therapy (the moral)
  273. 273. I❤refactoring
  274. 274. Science!Working Memory
  275. 275. Science!Problem-Solving Strategies
  276. 276. Science! Worry & Panic
  277. 277. Refactoring makes you smarter!
  278. 278. Exobrain
  279. 279. Exobraincounteracts panic
  280. 280. Refactoring makes you happier!
  281. 281. Fast Tests
  282. 282. Fast Tests are awesome
  283. 283. Happiness
  284. 284. Happinessleads to good design
  285. 285. Illustration from God of Cake by Allie Broshhttp://hyperboleandahalf.blogspot.no/2010/10/god-of-cake.html
  286. 286. KTHXBYEKatrina Owen@kytrinyxgithub.com/kytrinyx/therapeutic-refactoring

×