Refactoring: the good, the bad and the ugly.

REFACTORING
THE GOOD, THE BAD AND THE UGLY
Jeroen Thora | @jeroenthora acrobat
JEROEN THORA
▸ Backend developer @ Kunstmaan | Accenture interactive
▸ Opensource contributor and maintainer
▸ Knplabs/php-github-api, Bernard, Bootstrap-
datepicker, ...
WHAT WE WILL TALK ABOUT
▸ Introduction to legacy code
▸ Steps to refactor
▸ Code smells (and solutions)
▸ Tools & resources
▸ Summary
LEGACY CODE
HOW YOU FEEL
TECHNICAL DEBT
▸ bit by bit the code is corrupted
▸ Accumulation of technical debts
▸ Any healthy project will collect some technical debt
▸ Forced to pay the debt
▸ Paying this debt is called refactoring
REFACTORING
"It's a process of restructuring existing
code, altering it's internal structure without
changing it's external behaviour"
BENEFITS - CODE
▸ Will look clean
▸ Easy to understand
▸ Easy to change / maintain
BENEFITS - YOU
▸ Will code faster
▸ Spot bugs easier
▸ More confident to develop the code
CONVINCING YOUR CLIENT/BOSS
▸ Explain what technical debt means
▸ Use actual examples like cost to on-board team
members, development of features, ...
▸ Integrate cost in the offer
▸ Refactor with clear short-term goals in mind
▸ Point out that you want the best for your customer
ONCE OWNER CONVINCED

FREEDOM TO MOVE THE PROJECT IN THE RIGHT
DIRECTION.
WARNINGS (DON'TS)
▸ The big red button switch (Green field approach)
▸ Migration of data
▸ Mistakes old and new
▸ Keeping up with the business
GENERAL ADVICE

DON'T START OVER. IMPROVE WHAT YOU HAVE! 
HOW TO START
▸ Make problems visible
▸ Monitor your environment
▸ Visualise errors
▸ Sentry / grafana dashboard / ...
*insert screenshot sentry + grafana dashboard*
TEAM
▸ The boyscout rule - Robert C. Martin (Uncle Bob)
▸ "Leaving the camping ground cleaner than you found it"
▸ You have to care
▸ The whole team has to care
▸ Discipline
▸ Keep taking small steps
▸ Small victories generate momentum
BALL OF MUD
▸ Isolate the messy part
▸ Push dependencies out from the outside to the core
▸ Controllers -> services -> libraries -> ...
▸ Making code  self-contained
▸ Thus making it testable
BALL OF MUD
▸ Small iterations
▸ Easy to detect regressions
▸ Almost risk free refactoring
▸ Together with tests
Refactoring: the good, the bad and the ugly.
BUILD CONFIDENCE
▸ High level tests of business critical logic
▸ Checkout flow, member registration, ....
▸ Unit tests of small components
▸ Clear input and a clear output.
▸ Make sure that bit of code keeps behaving as expected.
GENERAL ADVICE

DON'T (TRY TO) TEST EVERYTHING.
YOU (OR MY REFLECTIONS)
▸ Becoming a better developer
▸ Learning to recognise bad code
▸ Back to basics
▸ People are generating shiny new legacy constantly
▸ Fun and rewarding
CODE SMELLS
▸ Bloaters
▸ Unneeded stuff
▸ Change preventers
▸ Complex if statements
▸ Bad naming
IF IT STINKS, CHANGE IT!
An indication of a deeper problem in the system
BLOATERS
Long method Long parameter list Large class
Image source: refactoring.guru
BLOATERS
Long method Long parameter list Large class
▸ Move logic to
private methods
▸ Group common
logic
▸ Keep class
hierarchy small
▸ Avoid classes with
same behaviour
▸ Enforce single
responsibility
principle
▸ Group in object
▸ Remove method
call result params
UNNEEDED STUFF
Dead code Useless comments
Future code 

not in use
Image source: refactoring.guru
CHANGE PREVENTERS
Shotgun Surgery
Image source: refactoring.guru
CHANGE PREVENTERS
▸ Avoid classes with same behaviour
▸ Move duplicated code
▸ Don't over engineer
Shotgun Surgery
COMPLEX IF-STATEMENTS
BAD NAMING
RESOURCES / TOOLS
▸ Phpmd
▸ Phpcpd
▸ Scrutinizer
▸ Php-cs-fixer
▸ Php Inspections EA (ultimate)
RESOURCES / TOOLS
https://refactoring.guru
SUMMARY
▸ Live by the boyscout rule
▸ Get your boss/client in the refactoring mindset
▸ Small steps at a time
▸ Monitor your application
▸ To improve yourself, improve your code
THANKS!
QUESTIONS?
@jeroenthora acrobat
1 of 31

Recommended

Devops (start walking in the same direction) by ops by
Devops (start walking in the same direction) by opsDevops (start walking in the same direction) by ops
Devops (start walking in the same direction) by opsDemis Rizzotto
189 views44 slides
Intro to DevOps by
Intro to DevOpsIntro to DevOps
Intro to DevOpsErnest Mueller
25.3K views45 slides
Modern day jvm controversies by
Modern day jvm controversiesModern day jvm controversies
Modern day jvm controversiesVictorSzoltysek
77 views35 slides
The Best Feature of Go – A 5 Year Retrospective by
The Best Feature of Go – A 5 Year RetrospectiveThe Best Feature of Go – A 5 Year Retrospective
The Best Feature of Go – A 5 Year RetrospectiveTahir Hashmi
1.9K views24 slides
Beyond Technical Debt: Unconventional techniques to uncover technical and soc... by
Beyond Technical Debt: Unconventional techniques to uncover technical and soc...Beyond Technical Debt: Unconventional techniques to uncover technical and soc...
Beyond Technical Debt: Unconventional techniques to uncover technical and soc...Juraj Martinka
52 views39 slides
DevOps Security - Is It Really So Difficult? - Reuven Harrison - DevOpsDays T... by
DevOps Security - Is It Really So Difficult? - Reuven Harrison - DevOpsDays T...DevOps Security - Is It Really So Difficult? - Reuven Harrison - DevOpsDays T...
DevOps Security - Is It Really So Difficult? - Reuven Harrison - DevOpsDays T...DevOpsDays Tel Aviv
70 views27 slides

More Related Content

Similar to Refactoring: the good, the bad and the ugly.

Developer-friendly taskqueues: What you should ask yourself before choosing one by
Developer-friendly taskqueues: What you should ask yourself before choosing oneDeveloper-friendly taskqueues: What you should ask yourself before choosing one
Developer-friendly taskqueues: What you should ask yourself before choosing oneSylvain Zimmer
684 views30 slides
Continuous Testing With React Storybook & WebdriverIO by
Continuous Testing With React Storybook & WebdriverIOContinuous Testing With React Storybook & WebdriverIO
Continuous Testing With React Storybook & WebdriverIOJosh Cypher
701 views55 slides
Real-World Application Observability - 11 Practical Developer Focused Tips by
Real-World Application Observability - 11 Practical Developer Focused TipsReal-World Application Observability - 11 Practical Developer Focused Tips
Real-World Application Observability - 11 Practical Developer Focused TipsVictorSzoltysek
73 views19 slides
Big code refactoring with agility by
Big code refactoring with agilityBig code refactoring with agility
Big code refactoring with agilityLuca Merolla
1.6K views49 slides
Real-World DevOps — 20 Practical Developers Tips for Tightening Your Operatio... by
Real-World DevOps — 20 Practical Developers Tips for Tightening Your Operatio...Real-World DevOps — 20 Practical Developers Tips for Tightening Your Operatio...
Real-World DevOps — 20 Practical Developers Tips for Tightening Your Operatio...VictorSzoltysek
80 views53 slides
Becoming the Docker Champion: Bringing Docker Back to Work by
Becoming the Docker Champion: Bringing Docker Back to WorkBecoming the Docker Champion: Bringing Docker Back to Work
Becoming the Docker Champion: Bringing Docker Back to WorkDocker, Inc.
678 views33 slides

Similar to Refactoring: the good, the bad and the ugly.(20)

Developer-friendly taskqueues: What you should ask yourself before choosing one by Sylvain Zimmer
Developer-friendly taskqueues: What you should ask yourself before choosing oneDeveloper-friendly taskqueues: What you should ask yourself before choosing one
Developer-friendly taskqueues: What you should ask yourself before choosing one
Sylvain Zimmer684 views
Continuous Testing With React Storybook & WebdriverIO by Josh Cypher
Continuous Testing With React Storybook & WebdriverIOContinuous Testing With React Storybook & WebdriverIO
Continuous Testing With React Storybook & WebdriverIO
Josh Cypher701 views
Real-World Application Observability - 11 Practical Developer Focused Tips by VictorSzoltysek
Real-World Application Observability - 11 Practical Developer Focused TipsReal-World Application Observability - 11 Practical Developer Focused Tips
Real-World Application Observability - 11 Practical Developer Focused Tips
VictorSzoltysek73 views
Big code refactoring with agility by Luca Merolla
Big code refactoring with agilityBig code refactoring with agility
Big code refactoring with agility
Luca Merolla1.6K views
Real-World DevOps — 20 Practical Developers Tips for Tightening Your Operatio... by VictorSzoltysek
Real-World DevOps — 20 Practical Developers Tips for Tightening Your Operatio...Real-World DevOps — 20 Practical Developers Tips for Tightening Your Operatio...
Real-World DevOps — 20 Practical Developers Tips for Tightening Your Operatio...
VictorSzoltysek80 views
Becoming the Docker Champion: Bringing Docker Back to Work by Docker, Inc.
Becoming the Docker Champion: Bringing Docker Back to WorkBecoming the Docker Champion: Bringing Docker Back to Work
Becoming the Docker Champion: Bringing Docker Back to Work
Docker, Inc.678 views
A Journey of Android Engineer in Start-up Culture by Fatima Azzahro
A Journey of Android Engineer in Start-up CultureA Journey of Android Engineer in Start-up Culture
A Journey of Android Engineer in Start-up Culture
Fatima Azzahro207 views
React Fundamentals - Jakarta JS, Apr 2016 by Simon Sturmer
React Fundamentals - Jakarta JS, Apr 2016React Fundamentals - Jakarta JS, Apr 2016
React Fundamentals - Jakarta JS, Apr 2016
Simon Sturmer814 views
2017-10-24 All Day DevOps - Disposable Development Environments by Boyd Hemphill
2017-10-24 All Day DevOps - Disposable Development Environments2017-10-24 All Day DevOps - Disposable Development Environments
2017-10-24 All Day DevOps - Disposable Development Environments
Boyd Hemphill487 views
TDD and Simple Design Workshop - Session 1 - March 2019 by Paulo Clavijo
TDD and Simple Design Workshop - Session 1 - March 2019TDD and Simple Design Workshop - Session 1 - March 2019
TDD and Simple Design Workshop - Session 1 - March 2019
Paulo Clavijo811 views
Why Developers Dig DevOps by BMC_DSM
Why Developers Dig DevOpsWhy Developers Dig DevOps
Why Developers Dig DevOps
BMC_DSM1.6K views
Test-Driven Design - ¿Porqué? by David Alfaro
Test-Driven Design - ¿Porqué?Test-Driven Design - ¿Porqué?
Test-Driven Design - ¿Porqué?
David Alfaro405 views
Automated Testing in Angular Slides by Jim Lynch
Automated Testing in Angular SlidesAutomated Testing in Angular Slides
Automated Testing in Angular Slides
Jim Lynch503 views
Practices of agile developers by DUONG Trong Tan
Practices of agile developersPractices of agile developers
Practices of agile developers
DUONG Trong Tan1.2K views
OSDC 2014: Fernando Hönig - New Data Center Service Model: Cloud + DevOps by NETWAYS
OSDC 2014:  Fernando Hönig - New Data Center Service Model: Cloud + DevOpsOSDC 2014:  Fernando Hönig - New Data Center Service Model: Cloud + DevOps
OSDC 2014: Fernando Hönig - New Data Center Service Model: Cloud + DevOps
NETWAYS514 views
You only have to change one thing to make DevOps work, Everything by Ken Mugrage
You only have to change one thing to make DevOps work, EverythingYou only have to change one thing to make DevOps work, Everything
You only have to change one thing to make DevOps work, Everything
Ken Mugrage322 views
It's all about behaviour, also in php - phpspec by Giulio De Donato
It's all about behaviour, also in php - phpspecIt's all about behaviour, also in php - phpspec
It's all about behaviour, also in php - phpspec
Giulio De Donato5K views
Improving your CFML code quality by Kai Koenig
Improving your CFML code qualityImproving your CFML code quality
Improving your CFML code quality
Kai Koenig855 views

Recently uploaded

BushraDBR: An Automatic Approach to Retrieving Duplicate Bug Reports by
BushraDBR: An Automatic Approach to Retrieving Duplicate Bug ReportsBushraDBR: An Automatic Approach to Retrieving Duplicate Bug Reports
BushraDBR: An Automatic Approach to Retrieving Duplicate Bug ReportsRa'Fat Al-Msie'deen
8 views49 slides
SAP FOR CONTRACT MANUFACTURING.pdf by
SAP FOR CONTRACT MANUFACTURING.pdfSAP FOR CONTRACT MANUFACTURING.pdf
SAP FOR CONTRACT MANUFACTURING.pdfVirendra Rai, PMP
13 views2 slides
SUGCON ANZ Presentation V2.1 Final.pptx by
SUGCON ANZ Presentation V2.1 Final.pptxSUGCON ANZ Presentation V2.1 Final.pptx
SUGCON ANZ Presentation V2.1 Final.pptxJack Spektor
22 views34 slides
DSD-INT 2023 European Digital Twin Ocean and Delft3D FM - Dols by
DSD-INT 2023 European Digital Twin Ocean and Delft3D FM - DolsDSD-INT 2023 European Digital Twin Ocean and Delft3D FM - Dols
DSD-INT 2023 European Digital Twin Ocean and Delft3D FM - DolsDeltares
7 views23 slides
Airline Booking Software by
Airline Booking SoftwareAirline Booking Software
Airline Booking SoftwareSharmiMehta
6 views26 slides
360 graden fabriek by
360 graden fabriek360 graden fabriek
360 graden fabriekinfo33492
38 views25 slides

Recently uploaded(20)

BushraDBR: An Automatic Approach to Retrieving Duplicate Bug Reports by Ra'Fat Al-Msie'deen
BushraDBR: An Automatic Approach to Retrieving Duplicate Bug ReportsBushraDBR: An Automatic Approach to Retrieving Duplicate Bug Reports
BushraDBR: An Automatic Approach to Retrieving Duplicate Bug Reports
SUGCON ANZ Presentation V2.1 Final.pptx by Jack Spektor
SUGCON ANZ Presentation V2.1 Final.pptxSUGCON ANZ Presentation V2.1 Final.pptx
SUGCON ANZ Presentation V2.1 Final.pptx
Jack Spektor22 views
DSD-INT 2023 European Digital Twin Ocean and Delft3D FM - Dols by Deltares
DSD-INT 2023 European Digital Twin Ocean and Delft3D FM - DolsDSD-INT 2023 European Digital Twin Ocean and Delft3D FM - Dols
DSD-INT 2023 European Digital Twin Ocean and Delft3D FM - Dols
Deltares7 views
Airline Booking Software by SharmiMehta
Airline Booking SoftwareAirline Booking Software
Airline Booking Software
SharmiMehta6 views
360 graden fabriek by info33492
360 graden fabriek360 graden fabriek
360 graden fabriek
info3349238 views
DSD-INT 2023 Process-based modelling of salt marsh development coupling Delft... by Deltares
DSD-INT 2023 Process-based modelling of salt marsh development coupling Delft...DSD-INT 2023 Process-based modelling of salt marsh development coupling Delft...
DSD-INT 2023 Process-based modelling of salt marsh development coupling Delft...
Deltares7 views
Software evolution understanding: Automatic extraction of software identifier... by Ra'Fat Al-Msie'deen
Software evolution understanding: Automatic extraction of software identifier...Software evolution understanding: Automatic extraction of software identifier...
Software evolution understanding: Automatic extraction of software identifier...
DSD-INT 2023 Delft3D FM Suite 2024.01 2D3D - New features + Improvements - Ge... by Deltares
DSD-INT 2023 Delft3D FM Suite 2024.01 2D3D - New features + Improvements - Ge...DSD-INT 2023 Delft3D FM Suite 2024.01 2D3D - New features + Improvements - Ge...
DSD-INT 2023 Delft3D FM Suite 2024.01 2D3D - New features + Improvements - Ge...
Deltares17 views
DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J... by Deltares
DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J...DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J...
DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J...
Deltares9 views
Fleet Management Software in India by Fleetable
Fleet Management Software in India Fleet Management Software in India
Fleet Management Software in India
Fleetable11 views
Dapr Unleashed: Accelerating Microservice Development by Miroslav Janeski
Dapr Unleashed: Accelerating Microservice DevelopmentDapr Unleashed: Accelerating Microservice Development
Dapr Unleashed: Accelerating Microservice Development
Miroslav Janeski10 views
DSD-INT 2023 Machine learning in hydraulic engineering - Exploring unseen fut... by Deltares
DSD-INT 2023 Machine learning in hydraulic engineering - Exploring unseen fut...DSD-INT 2023 Machine learning in hydraulic engineering - Exploring unseen fut...
DSD-INT 2023 Machine learning in hydraulic engineering - Exploring unseen fut...
Deltares7 views
DSD-INT 2023 Delft3D FM Suite 2024.01 1D2D - Beta testing programme - Geertsema by Deltares
DSD-INT 2023 Delft3D FM Suite 2024.01 1D2D - Beta testing programme - GeertsemaDSD-INT 2023 Delft3D FM Suite 2024.01 1D2D - Beta testing programme - Geertsema
DSD-INT 2023 Delft3D FM Suite 2024.01 1D2D - Beta testing programme - Geertsema
Deltares17 views
AI and Ml presentation .pptx by FayazAli87
AI and Ml presentation .pptxAI and Ml presentation .pptx
AI and Ml presentation .pptx
FayazAli8711 views
Navigating container technology for enhanced security by Niklas Saari by Metosin Oy
Navigating container technology for enhanced security by Niklas SaariNavigating container technology for enhanced security by Niklas Saari
Navigating container technology for enhanced security by Niklas Saari
Metosin Oy14 views
Myths and Facts About Hospice Care: Busting Common Misconceptions by Care Coordinations
Myths and Facts About Hospice Care: Busting Common MisconceptionsMyths and Facts About Hospice Care: Busting Common Misconceptions
Myths and Facts About Hospice Care: Busting Common Misconceptions
Team Transformation Tactics for Holistic Testing and Quality (Japan Symposium... by Lisi Hocke
Team Transformation Tactics for Holistic Testing and Quality (Japan Symposium...Team Transformation Tactics for Holistic Testing and Quality (Japan Symposium...
Team Transformation Tactics for Holistic Testing and Quality (Japan Symposium...
Lisi Hocke30 views

Refactoring: the good, the bad and the ugly.

  • 1. REFACTORING THE GOOD, THE BAD AND THE UGLY Jeroen Thora | @jeroenthora acrobat
  • 2. JEROEN THORA ▸ Backend developer @ Kunstmaan | Accenture interactive ▸ Opensource contributor and maintainer ▸ Knplabs/php-github-api, Bernard, Bootstrap- datepicker, ...
  • 3. WHAT WE WILL TALK ABOUT ▸ Introduction to legacy code ▸ Steps to refactor ▸ Code smells (and solutions) ▸ Tools & resources ▸ Summary
  • 6. TECHNICAL DEBT ▸ bit by bit the code is corrupted ▸ Accumulation of technical debts ▸ Any healthy project will collect some technical debt ▸ Forced to pay the debt ▸ Paying this debt is called refactoring
  • 7. REFACTORING "It's a process of restructuring existing code, altering it's internal structure without changing it's external behaviour"
  • 8. BENEFITS - CODE ▸ Will look clean ▸ Easy to understand ▸ Easy to change / maintain
  • 9. BENEFITS - YOU ▸ Will code faster ▸ Spot bugs easier ▸ More confident to develop the code
  • 10. CONVINCING YOUR CLIENT/BOSS ▸ Explain what technical debt means ▸ Use actual examples like cost to on-board team members, development of features, ... ▸ Integrate cost in the offer ▸ Refactor with clear short-term goals in mind ▸ Point out that you want the best for your customer ONCE OWNER CONVINCED
 FREEDOM TO MOVE THE PROJECT IN THE RIGHT DIRECTION.
  • 11. WARNINGS (DON'TS) ▸ The big red button switch (Green field approach) ▸ Migration of data ▸ Mistakes old and new ▸ Keeping up with the business GENERAL ADVICE
 DON'T START OVER. IMPROVE WHAT YOU HAVE! 
  • 12. HOW TO START ▸ Make problems visible ▸ Monitor your environment ▸ Visualise errors ▸ Sentry / grafana dashboard / ...
  • 13. *insert screenshot sentry + grafana dashboard*
  • 14. TEAM ▸ The boyscout rule - Robert C. Martin (Uncle Bob) ▸ "Leaving the camping ground cleaner than you found it" ▸ You have to care ▸ The whole team has to care ▸ Discipline ▸ Keep taking small steps ▸ Small victories generate momentum
  • 15. BALL OF MUD ▸ Isolate the messy part ▸ Push dependencies out from the outside to the core ▸ Controllers -> services -> libraries -> ... ▸ Making code  self-contained ▸ Thus making it testable
  • 16. BALL OF MUD ▸ Small iterations ▸ Easy to detect regressions ▸ Almost risk free refactoring ▸ Together with tests
  • 18. BUILD CONFIDENCE ▸ High level tests of business critical logic ▸ Checkout flow, member registration, .... ▸ Unit tests of small components ▸ Clear input and a clear output. ▸ Make sure that bit of code keeps behaving as expected. GENERAL ADVICE
 DON'T (TRY TO) TEST EVERYTHING.
  • 19. YOU (OR MY REFLECTIONS) ▸ Becoming a better developer ▸ Learning to recognise bad code ▸ Back to basics ▸ People are generating shiny new legacy constantly ▸ Fun and rewarding
  • 20. CODE SMELLS ▸ Bloaters ▸ Unneeded stuff ▸ Change preventers ▸ Complex if statements ▸ Bad naming IF IT STINKS, CHANGE IT! An indication of a deeper problem in the system
  • 21. BLOATERS Long method Long parameter list Large class Image source: refactoring.guru
  • 22. BLOATERS Long method Long parameter list Large class ▸ Move logic to private methods ▸ Group common logic ▸ Keep class hierarchy small ▸ Avoid classes with same behaviour ▸ Enforce single responsibility principle ▸ Group in object ▸ Remove method call result params
  • 23. UNNEEDED STUFF Dead code Useless comments Future code 
 not in use Image source: refactoring.guru
  • 24. CHANGE PREVENTERS Shotgun Surgery Image source: refactoring.guru
  • 25. CHANGE PREVENTERS ▸ Avoid classes with same behaviour ▸ Move duplicated code ▸ Don't over engineer Shotgun Surgery
  • 28. RESOURCES / TOOLS ▸ Phpmd ▸ Phpcpd ▸ Scrutinizer ▸ Php-cs-fixer ▸ Php Inspections EA (ultimate)
  • 30. SUMMARY ▸ Live by the boyscout rule ▸ Get your boss/client in the refactoring mindset ▸ Small steps at a time ▸ Monitor your application ▸ To improve yourself, improve your code