SlideShare a Scribd company logo
S O Y O U WA N T T O
S TA R T R E FA C T O R I N G ?
J I L L I A N F O L E Y • R A I L S C O N F 2 0 1 5
A B O U T M E
•Software engineer at
Contactually
•Learned coding on-the-job
•Pretty much only worked in
legacy code
S O W H AT I S R E FA C T O R I N G ?
– M A R T I N F O W L E R
“Refactoring is a disciplined technique for
restructuring an existing body of code, altering its
internal structure without changing its external
behavior."
T H E I D E A I S
• No change to your users
• Improvement for
developers
B U T … W H Y ?
– Y O U R M A N A G E R
“Refactoring? No way. We need new features!”
• Maintainability
• Scalability
• Flexibility
• Spring cleaning
H O W W I L L I K N O W W H E N I T ’ S T I M E
T O R E FA C T O R ?
O V E R W H E L M E D B Y
B U G S
D E C R E A S E D
P E R F O R M A N C E
N 0 0 B S AY S W T F
TA K E A L O O K
WINTER IS COMING
TESTS ARE HARD TO
WRITE
BLOCKING NEW
PROGRESS
PRODUCTION CRASHES
D O I T Y E S T E R D AY !
S O H O W D O Y O U D O I T ?
P R E PA R E
• Beef up your test
coverage
• Review functionality x 100
• Brush up on code style
guides and preferences
M I S E E N P L A C E , F O R C O D E
A N N O TAT E
• Read through and comment
anything that looks weird,
bad, wrong, or confusing
• Consider copying
dependencies temporarily
• Rename variables and
methods
M A R K U P S M E L LY C O D E
I N V E S T I G AT E
•What looks redundant?
•What looks too complex?
•What parts of the code took more
than 1-2 reads to understand?
•Do you see any methods that
look bloated?
•Does any of it violate code style
standards?
L O O K F O R C O D E M E S S E S
C L E A N U P
• Focus on one method at a time
• DRY it up
• Pick better names
• Fix style issues
• Simplify conditionals
• Break down complex methods
• Move code where it truly belongs
G I V E Y O U R C O D E A M A K E O V E R
A V E RY B R I E F C A S E S T U D Y
!
email = params[:signup].delete(:email).to_s.gsub(/s/, '').downcase[0..100]
!
!
unless u = User.find_by_email(email)
if a = Account.email.find_by_username(email)
!
u = a
end
end
!
!
if u
if email != "demo@contactually.com"
flash[:notice] = "Looks like you already have an account."
redirect_to new_user_session_path(:exists => true)
return true
!
else
sign_in(u)
redirect_to signup_accounts_path
return true
end
end
# Does this assignment need to be so long? Why is the param key :signup ?
email = params[:signup].delete(:email).to_s.gsub(/s/, '').downcase[0..100]
!
# This nested unless/if makes my brain hurt.
unless u = User.find_by_email(email)
if a = Account.email.find_by_username(email)
# Is there some way to consolidate these two u assignments?
u = a # wtf are these single-letter variables
end
end
!
# Ew another nested if
if u
if email != "demo@contactually.com"
flash[:notice] = "Looks like you already have an account."
redirect_to new_user_session_path(:exists => true)
return true
# Using the negative in the first if makes this 'else' confusing
else
sign_in(u)
redirect_to signup_accounts_path
return true
end
end
email = params[:user][:email]
!
# Try to find an existing user by email or account
existing_user = User.find_by_email(email) ||
Account.email.find_by_username(email).try(&:user)
!
# Push demo user through to demo signup
if existing_user && email == 'demo@contactually.com'
sign_in(existing_user)
redirect_to signup_accounts_path
return true
end
!
if existing_user
flash[:notice] = "Looks like you already have an account with
Contactually."
redirect_to new_user_session_path(:exists => true)
return true
end
O T H E R T I P S
• Test frequently, both manually and automated.
• Don’t change tests while refactoring!
• Commit frequently.
• Use tools like Rubocop or Reek for help when you’re
overwhelmed or stuck
• Consider pausing after a logical group of changes to
do more testing, code review, and deploy . . .
OMG I WANT
ALL THE
CHANGES!!!11
“BIG BANG”
SLOW AND
STEADY WINS
THE RACE?
“INCREMENTAL”
T H I N G S T O R E M E M B E R
• Refactoring isn’t scary
• It’s a good way to learn new design patterns
• It’s a good way to learn your codebase
• Tests can be refactored too!
• Go back and refactor old projects for practice
G O F O R T H A N D R E FA C T O R !
Q U E S T I O N S ?
J I L L I A N @ C O N TA C T U A L LY. C O M
@ J 3 F O L E Y
A P P E N D I X : F U R T H E R R E A D I N G
!
• Martin Fowler's site to accompany his Refactoring book
• The Ruby version of above Refactoring book, by Jay
Fields, Shane Harvie, and Martin Fowler
• Ginny Hendry's notes from the Ruby version of the
Refactoring book
• Codecademy Ruby refactoring exercise

More Related Content

Similar to So You Want to Start Refactoring?

Good code, Bad Code
Good code, Bad CodeGood code, Bad Code
Good code, Bad Codejosedasilva
 
Surviving a heavy deadline
Surviving a heavy deadlineSurviving a heavy deadline
Surviving a heavy deadlineFranz Dumfart
 
From Content Strategy to Drupal Site Building - Connecting the dots
From Content Strategy to Drupal Site Building - Connecting the dotsFrom Content Strategy to Drupal Site Building - Connecting the dots
From Content Strategy to Drupal Site Building - Connecting the dotsRonald Ashri
 
From Content Strategy to Drupal Site Building - Connecting the Dots
From Content Strategy to Drupal Site Building - Connecting the DotsFrom Content Strategy to Drupal Site Building - Connecting the Dots
From Content Strategy to Drupal Site Building - Connecting the DotsRonald Ashri
 
XPDS16: Patch review for non-maintainers - George Dunlap, Citrix Systems R&D...
 XPDS16: Patch review for non-maintainers - George Dunlap, Citrix Systems R&D... XPDS16: Patch review for non-maintainers - George Dunlap, Citrix Systems R&D...
XPDS16: Patch review for non-maintainers - George Dunlap, Citrix Systems R&D...The Linux Foundation
 
How I Learned to Stop Worrying and Love Legacy Code.....
How I Learned to Stop Worrying and Love Legacy Code.....How I Learned to Stop Worrying and Love Legacy Code.....
How I Learned to Stop Worrying and Love Legacy Code.....Mike Harris
 
Another pair of eyes
Another pair of eyesAnother pair of eyes
Another pair of eyesAdam Dangoor
 
Test Driven Development with Laravel
Test Driven Development with LaravelTest Driven Development with Laravel
Test Driven Development with LaravelTyler Johnston
 
code review, style guides and tools for pythin
code review, style guides and tools for pythincode review, style guides and tools for pythin
code review, style guides and tools for pythinuniversité d'el jadida
 
50 Shades of Fail KScope16
50 Shades of Fail KScope1650 Shades of Fail KScope16
50 Shades of Fail KScope16Christian Berg
 
Saleforce For Domino Dogs
Saleforce For Domino DogsSaleforce For Domino Dogs
Saleforce For Domino DogsMark Myers
 

Similar to So You Want to Start Refactoring? (20)

Deployments in one click!
Deployments in one click!Deployments in one click!
Deployments in one click!
 
Good code, Bad Code
Good code, Bad CodeGood code, Bad Code
Good code, Bad Code
 
Surviving a heavy deadline
Surviving a heavy deadlineSurviving a heavy deadline
Surviving a heavy deadline
 
From Content Strategy to Drupal Site Building - Connecting the dots
From Content Strategy to Drupal Site Building - Connecting the dotsFrom Content Strategy to Drupal Site Building - Connecting the dots
From Content Strategy to Drupal Site Building - Connecting the dots
 
From Content Strategy to Drupal Site Building - Connecting the Dots
From Content Strategy to Drupal Site Building - Connecting the DotsFrom Content Strategy to Drupal Site Building - Connecting the Dots
From Content Strategy to Drupal Site Building - Connecting the Dots
 
Pair Programming
Pair ProgrammingPair Programming
Pair Programming
 
XPDS16: Patch review for non-maintainers - George Dunlap, Citrix Systems R&D...
 XPDS16: Patch review for non-maintainers - George Dunlap, Citrix Systems R&D... XPDS16: Patch review for non-maintainers - George Dunlap, Citrix Systems R&D...
XPDS16: Patch review for non-maintainers - George Dunlap, Citrix Systems R&D...
 
How I Learned to Stop Worrying and Love Legacy Code.....
How I Learned to Stop Worrying and Love Legacy Code.....How I Learned to Stop Worrying and Love Legacy Code.....
How I Learned to Stop Worrying and Love Legacy Code.....
 
Another pair of eyes
Another pair of eyesAnother pair of eyes
Another pair of eyes
 
New Android Languages
New Android LanguagesNew Android Languages
New Android Languages
 
Test Driven Development with Laravel
Test Driven Development with LaravelTest Driven Development with Laravel
Test Driven Development with Laravel
 
ITB2016 - ColdBox 4 Modules
ITB2016 - ColdBox 4 ModulesITB2016 - ColdBox 4 Modules
ITB2016 - ColdBox 4 Modules
 
Clean code
Clean codeClean code
Clean code
 
code review, style guides and tools for pythin
code review, style guides and tools for pythincode review, style guides and tools for pythin
code review, style guides and tools for pythin
 
Make a better with clean code
Make a better with clean codeMake a better with clean code
Make a better with clean code
 
Development tools
Development toolsDevelopment tools
Development tools
 
50 Shades of Fail KScope16
50 Shades of Fail KScope1650 Shades of Fail KScope16
50 Shades of Fail KScope16
 
Saleforce For Domino Dogs
Saleforce For Domino DogsSaleforce For Domino Dogs
Saleforce For Domino Dogs
 
Module
ModuleModule
Module
 
Smoke tests and mirrors
Smoke tests and mirrorsSmoke tests and mirrors
Smoke tests and mirrors
 

Recently uploaded

Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfOrtus Solutions, Corp
 
AI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in MichelangeloAI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in MichelangeloAlluxio, Inc.
 
Agnieszka Andrzejewska - BIM School Course in Kraków
Agnieszka Andrzejewska - BIM School Course in KrakówAgnieszka Andrzejewska - BIM School Course in Kraków
Agnieszka Andrzejewska - BIM School Course in Krakówbim.edu.pl
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTier1 app
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar
 
GraphAware - Transforming policing with graph-based intelligence analysis
GraphAware - Transforming policing with graph-based intelligence analysisGraphAware - Transforming policing with graph-based intelligence analysis
GraphAware - Transforming policing with graph-based intelligence analysisNeo4j
 
A Comprehensive Appium Guide for Hybrid App Automation Testing.pdf
A Comprehensive Appium Guide for Hybrid App Automation Testing.pdfA Comprehensive Appium Guide for Hybrid App Automation Testing.pdf
A Comprehensive Appium Guide for Hybrid App Automation Testing.pdfkalichargn70th171
 
iGaming Platform & Lottery Solutions by Skilrock
iGaming Platform & Lottery Solutions by SkilrockiGaming Platform & Lottery Solutions by Skilrock
iGaming Platform & Lottery Solutions by SkilrockSkilrock Technologies
 
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...Alluxio, Inc.
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILNatan Silnitsky
 
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...Abortion Clinic
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessWSO2
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Shahin Sheidaei
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandIES VE
 
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?XfilesPro
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Anthony Dahanne
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...informapgpstrackings
 
Studiovity film pre-production and screenwriting software
Studiovity film pre-production and screenwriting softwareStudiovity film pre-production and screenwriting software
Studiovity film pre-production and screenwriting softwareinfo611746
 
Breaking the Code : A Guide to WhatsApp Business API.pdf
Breaking the Code : A Guide to WhatsApp Business API.pdfBreaking the Code : A Guide to WhatsApp Business API.pdf
Breaking the Code : A Guide to WhatsApp Business API.pdfMeon Technology
 

Recently uploaded (20)

Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
 
AI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in MichelangeloAI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in Michelangelo
 
Agnieszka Andrzejewska - BIM School Course in Kraków
Agnieszka Andrzejewska - BIM School Course in KrakówAgnieszka Andrzejewska - BIM School Course in Kraków
Agnieszka Andrzejewska - BIM School Course in Kraków
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
 
GraphAware - Transforming policing with graph-based intelligence analysis
GraphAware - Transforming policing with graph-based intelligence analysisGraphAware - Transforming policing with graph-based intelligence analysis
GraphAware - Transforming policing with graph-based intelligence analysis
 
A Comprehensive Appium Guide for Hybrid App Automation Testing.pdf
A Comprehensive Appium Guide for Hybrid App Automation Testing.pdfA Comprehensive Appium Guide for Hybrid App Automation Testing.pdf
A Comprehensive Appium Guide for Hybrid App Automation Testing.pdf
 
iGaming Platform & Lottery Solutions by Skilrock
iGaming Platform & Lottery Solutions by SkilrockiGaming Platform & Lottery Solutions by Skilrock
iGaming Platform & Lottery Solutions by Skilrock
 
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
 
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
 
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
 
Studiovity film pre-production and screenwriting software
Studiovity film pre-production and screenwriting softwareStudiovity film pre-production and screenwriting software
Studiovity film pre-production and screenwriting software
 
Breaking the Code : A Guide to WhatsApp Business API.pdf
Breaking the Code : A Guide to WhatsApp Business API.pdfBreaking the Code : A Guide to WhatsApp Business API.pdf
Breaking the Code : A Guide to WhatsApp Business API.pdf
 

So You Want to Start Refactoring?

  • 1. S O Y O U WA N T T O S TA R T R E FA C T O R I N G ? J I L L I A N F O L E Y • R A I L S C O N F 2 0 1 5
  • 2. A B O U T M E •Software engineer at Contactually •Learned coding on-the-job •Pretty much only worked in legacy code
  • 3.
  • 4. S O W H AT I S R E FA C T O R I N G ?
  • 5. – M A R T I N F O W L E R “Refactoring is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior."
  • 6. T H E I D E A I S • No change to your users • Improvement for developers
  • 7.
  • 8. B U T … W H Y ?
  • 9. – Y O U R M A N A G E R “Refactoring? No way. We need new features!”
  • 10. • Maintainability • Scalability • Flexibility • Spring cleaning
  • 11. H O W W I L L I K N O W W H E N I T ’ S T I M E T O R E FA C T O R ?
  • 12. O V E R W H E L M E D B Y B U G S D E C R E A S E D P E R F O R M A N C E N 0 0 B S AY S W T F TA K E A L O O K WINTER IS COMING
  • 13. TESTS ARE HARD TO WRITE BLOCKING NEW PROGRESS PRODUCTION CRASHES D O I T Y E S T E R D AY !
  • 14. S O H O W D O Y O U D O I T ?
  • 15. P R E PA R E • Beef up your test coverage • Review functionality x 100 • Brush up on code style guides and preferences M I S E E N P L A C E , F O R C O D E
  • 16. A N N O TAT E • Read through and comment anything that looks weird, bad, wrong, or confusing • Consider copying dependencies temporarily • Rename variables and methods M A R K U P S M E L LY C O D E
  • 17. I N V E S T I G AT E •What looks redundant? •What looks too complex? •What parts of the code took more than 1-2 reads to understand? •Do you see any methods that look bloated? •Does any of it violate code style standards? L O O K F O R C O D E M E S S E S
  • 18. C L E A N U P • Focus on one method at a time • DRY it up • Pick better names • Fix style issues • Simplify conditionals • Break down complex methods • Move code where it truly belongs G I V E Y O U R C O D E A M A K E O V E R
  • 19. A V E RY B R I E F C A S E S T U D Y
  • 20. ! email = params[:signup].delete(:email).to_s.gsub(/s/, '').downcase[0..100] ! ! unless u = User.find_by_email(email) if a = Account.email.find_by_username(email) ! u = a end end ! ! if u if email != "demo@contactually.com" flash[:notice] = "Looks like you already have an account." redirect_to new_user_session_path(:exists => true) return true ! else sign_in(u) redirect_to signup_accounts_path return true end end
  • 21. # Does this assignment need to be so long? Why is the param key :signup ? email = params[:signup].delete(:email).to_s.gsub(/s/, '').downcase[0..100] ! # This nested unless/if makes my brain hurt. unless u = User.find_by_email(email) if a = Account.email.find_by_username(email) # Is there some way to consolidate these two u assignments? u = a # wtf are these single-letter variables end end ! # Ew another nested if if u if email != "demo@contactually.com" flash[:notice] = "Looks like you already have an account." redirect_to new_user_session_path(:exists => true) return true # Using the negative in the first if makes this 'else' confusing else sign_in(u) redirect_to signup_accounts_path return true end end
  • 22. email = params[:user][:email] ! # Try to find an existing user by email or account existing_user = User.find_by_email(email) || Account.email.find_by_username(email).try(&:user) ! # Push demo user through to demo signup if existing_user && email == 'demo@contactually.com' sign_in(existing_user) redirect_to signup_accounts_path return true end ! if existing_user flash[:notice] = "Looks like you already have an account with Contactually." redirect_to new_user_session_path(:exists => true) return true end
  • 23. O T H E R T I P S • Test frequently, both manually and automated. • Don’t change tests while refactoring! • Commit frequently. • Use tools like Rubocop or Reek for help when you’re overwhelmed or stuck • Consider pausing after a logical group of changes to do more testing, code review, and deploy . . .
  • 24. OMG I WANT ALL THE CHANGES!!!11 “BIG BANG” SLOW AND STEADY WINS THE RACE? “INCREMENTAL”
  • 25. T H I N G S T O R E M E M B E R • Refactoring isn’t scary • It’s a good way to learn new design patterns • It’s a good way to learn your codebase • Tests can be refactored too! • Go back and refactor old projects for practice
  • 26. G O F O R T H A N D R E FA C T O R !
  • 27. Q U E S T I O N S ? J I L L I A N @ C O N TA C T U A L LY. C O M @ J 3 F O L E Y
  • 28. A P P E N D I X : F U R T H E R R E A D I N G ! • Martin Fowler's site to accompany his Refactoring book • The Ruby version of above Refactoring book, by Jay Fields, Shane Harvie, and Martin Fowler • Ginny Hendry's notes from the Ruby version of the Refactoring book • Codecademy Ruby refactoring exercise